import os import sys HERE = os.path.abspath(os.path.dirname(__file__)) ROOT = os.path.join(HERE, '..') sys.path.append(ROOT) from jobs.send_email import PySendPulse # noqa from server.utils import generate_token # noqa from server.settings import config # noqa from server.auth.user import User # noqa config.configure(False) user = User() user.name = '' # CHANGEME user.email = '' # CHANGEME # SEND EMAIL VALIDATION TOKEN email_validation_token = generate_token(20) user.email_validation_token = email_validation_token # FORMAT EMAIL TEMPLATE email = config.get('email_confirmation_email') email['text'] = email['text'].format( email_validation_token=email_validation_token ) email['html'] = email['html'].format( email_validation_token=email_validation_token ) email['to'][0]['email'] = email['to'][0]['email'].format( user_email=user.email
import os from time import sleep import asyncio HERE = os.path.abspath(os.path.dirname(__file__)) ROOT = os.path.join(HERE, '..') sys.path.append(ROOT) from server.utils import drop_database # noqa from server.model.user import User # noqa from server.settings import config # noqa from server.model.notification import Notification # noqa from server.utils import DbSessionContext # noqa config.configure() loop = asyncio.get_event_loop() asyncio.set_event_loop(loop) if config.get('env', 'production') != 'development': print('The "env" variable is not set to development') sys.exit(1) DB_NAME = config.get('mongo_database_name') drop_database(DB_NAME, config.get('redis_database')) with DbSessionContext(DB_NAME) as session: # INSERT DUMMY DATA users = [ { 'name': 'test',
async def init(loop, config_args=None): # CONFIG config.configure(config_args) logger.info('Env: {env}'.format(env=config.get('env'))) # SESSION redis_db = config.get('redis_database', 0) redis_pool = await aioredis.create_pool(('localhost', 6379), db=redis_db) storage = redis_storage.RedisStorage( redis_pool, cookie_name="AIOHTTP_SESSION-{redis_db}".format(redis_db=redis_db)) app = web.Application( loop=loop, middlewares=[session_middleware(storage), db_handler]) app.redis_pool = redis_pool # QUEUE app.queue = Queue(connection=Redis()) # WEBSOCKET """ app['websockets'] = [] """ handler = app.make_handler() # ROUTES for route in routes: app.router.add_route(route[0], route[1], route[2], name=route[3]) if config.get('env', 'production') == 'development': static_path = os.path.join(ROOT, 'dist-dev') else: if config.get('release', 'latest') == 'latest': latest_version_path = os.path.join(ROOT, 'releases', 'latest.txt') if os.path.exists(latest_version_path): with open(latest_version_path, 'r') as fd: release_version = fd.read() else: raise Exception("The latest.txt file doesn't exists") else: release_version = config.get('release') static_path = os.path.join(ROOT, 'releases', release_version) app.router.add_static('/', static_path, name='static') logger.info( "Serving static: {static_path}".format(static_path=static_path)) aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader(static_path)) # PREPARE HOOK async def after_request(request, response): if hasattr(request, 'db_session'): request.db_session.end() request.db_session.db.client.close() app.on_response_prepare.append(after_request) # SHUTDOWN app.on_shutdown.append(on_shutdown) serv_generator = loop.create_server(handler, config.get('server_host'), config.get('server_port')) return serv_generator, handler, app
import os import sys HERE = os.path.abspath(os.path.dirname(__file__)) ROOT = os.path.join(HERE, '..') sys.path.append(ROOT) from jobs.send_email import PySendPulse # noqa from server.utils import generate_token # noqa from server.settings import config # noqa from server.auth.user import User # noqa config.configure(False) user = User() user.name = '' # CHANGEME user.email = '' # CHANGEME # SEND EMAIL VALIDATION TOKEN email_validation_token = generate_token(20) user.email_validation_token = email_validation_token # FORMAT EMAIL TEMPLATE email = config.get('email_confirmation_email') email['text'] = email['text'].format( email_validation_token=email_validation_token) email['html'] = email['html'].format( email_validation_token=email_validation_token) email['to'][0]['email'] = email['to'][0]['email'].format(user_email=user.email) email['to'][0]['name'] = email['to'][0]['name'].format(user_name=user.name) SPApiProxy = PySendPulse(config.get('REST_API_ID'),
async def init(loop, config_args=None): # CONFIG config.configure(config_args) logger.info('Env: {env}'.format(env=config.get('env'))) # SESSION redis_db = config.get('redis_database', 0) redis_pool = await aioredis.create_pool( ('localhost', 6379), db=redis_db ) storage = redis_storage.RedisStorage( redis_pool, cookie_name="AIOHTTP_SESSION-{redis_db}" .format(redis_db=redis_db) ) app = web.Application(loop=loop, middlewares=[ session_middleware(storage), db_handler ]) app.redis_pool = redis_pool # QUEUE app.queue = Queue(connection=Redis()) # WEBSOCKET """ app['websockets'] = [] """ handler = app.make_handler() # ROUTES for route in routes: app.router.add_route(route[0], route[1], route[2], name=route[3]) if config.get('env', 'production') == 'development': static_path = os.path.join(ROOT, 'dist-dev') else: if config.get('release', 'latest') == 'latest': latest_version_path = os.path.join( ROOT, 'releases', 'latest.txt' ) if os.path.exists(latest_version_path): with open(latest_version_path, 'r') as fd: release_version = fd.read() else: raise Exception("The latest.txt file doesn't exists") else: release_version = config.get('release') static_path = os.path.join(ROOT, 'releases', release_version) app.router.add_static('/', static_path, name='static') logger.info( "Serving static: {static_path}" .format( static_path=static_path ) ) aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader(static_path)) # PREPARE HOOK async def after_request(request, response): if hasattr(request, 'db_session'): request.db_session.end() request.db_session.db.client.close() app.on_response_prepare.append(after_request) # SHUTDOWN app.on_shutdown.append(on_shutdown) serv_generator = loop.create_server( handler, config.get('server_host'), config.get('server_port') ) return serv_generator, handler, app
sys.path.append(ROOT) from server.model.user import User # noqa from server.utils import DbSessionContext # noqa from server.settings import config # noqa from server import exceptions # noqa # LOOP loop = asyncio.get_event_loop() asyncio.set_event_loop(loop) # CONFIG config_path = os.path.join(ROOT, 'configs', 'server.json') config.configure(config_path) # LOGGING # DISABLE werkzeug werkzeug_logger = logging.getLogger('werkzeug') werkzeug_logger.setLevel(logging.ERROR) # {{cookiecutter.project_name|upper}} ADMIN logger = logging.getLogger('{{cookiecutter.project_name|lower}}_admin') logger.setLevel(getattr(logging, config.get('admin').get('log_level', 'INFO'))) formatter = logging.Formatter( '[L:%(lineno)d]# %(levelname)-8s [%(asctime)s] %(message)s', datefmt='%d-%m-%Y %H:%M:%S' )