import os from sanic import Sanic from sanic_compress import Compress from sanic.log import logger from sanic.response import json from handlers.sqs_handler import SqsHandler import message_validator as validater app = Sanic('limbo') Compress(app) @app.listener('before_server_start') async def setup_sqs(app, loop): app.sqs = SqsHandler() @app.listener('before_server_stop') async def notify_server_stopping(app, loop): logger.info('Server shutting down!') @app.get('/') async def hello_world(request): return json({'hello': 'world'}) @app.get('/health') async def health(request): return json({'health': 'ok'})
def init_ax(): """Initiate all modules of Ax""" # Load settings from app.yaml to os.environ ax_misc.load_configuration() # Misc functions used throw all application. Nothing specific. ax_misc.init_misc( timezone_name=str(os.environ.get('AX_TIMEZONE') or 'UTC'), tmp_absolute_path=os.environ.get('AX_TMP_ABSOLUTE_PATH') or None, uploads_absolute_path=os.environ.get('AX_UPLOADS_ABSOLUTE_PATH') or None) # Logger (loguru) used in all modules - console + file + sentry ax_logger.init_logger( logs_filename=os.environ.get('AX_LOGS_FILENAME') or None, logs_absolute_path=os.environ.get('AX_LOGS_ABSOLUTE_PATH') or None, logs_level=os.environ.get('AX_LOGS_LEVEL') or 'INFO') # for item, value in os.environ.items(): # logger.info('ENV: {}: {}'.format(item, value)) # Initiate SqlAlchemy. Made with separate function for alembic. # It initiates database connection on migration. init_model() # Init cache module (aiocache). It is not yet used. ax_cache.init_cache( mode=str(os.environ.get('AX_CACHE_MODE') or 'default'), redis_endpoint=str(os.environ.get('AX_REDIS_ENDPOINT') or '127.0.0.1'), redis_port=int(os.environ.get('AX_REDIS_PORT') or 6379), redis_timeout=int(os.environ.get('AX_REDIS_TIMEOUT') or 1), ) # Initiate pub-sub module. Used for web-socket subscriptions. # Notification on workflow action ax_pubsub.init_pubsub() # Check if database schema needs update ax_migration.init_migration() # Initiate gql schema. Depends on cache and pubsub ax_schema.init_schema_standalone() # cors = CORS(app, resources={r"/api/*": {"origins": "*"}}) CORS(app, automatic_options=True) # TODO limit CORS to api folder # Enable gzip compression for responses # 'text/html','text/css','text/xml','application/json','application/java Compress(app) @app.listener('before_server_start') async def initialize_scheduler(_app, _loop): # pylint: disable=unused-variable """Initiate scheduler. It used for cron-like jobs""" ax_scheduler.init_scheduler() @app.listener('before_server_start') def save_loop(_app, _loop): # pylint: disable=unused-variable """Initiate graphene graphql server""" ax_routes.loop = _loop ax_routes.app = _app ax_routes.init_graphql_view() # _app.add_route(ax_routes.graphql_view, '/api/graphql') ssl_cert = os.environ.get('SSL_CERT_ABSOLUTE_PATH', None) ssl_key = os.environ.get('SSL_KEY_ABSOLUTE_PATH', None) ssl_enabled = False if ssl_cert and ssl_key: ssl_enabled = True if ssl_enabled: @app.middleware('request') async def force_ssl(request): # pylint: disable=unused-variable if request.scheme == 'ws': return response.redirect(request.url.replace( 'wss://', 'https://', 1), status=301) elif request.scheme == 'http': return response.redirect(request.url.replace( 'http://', 'https://', 1), status=301) # Initiate all sanic server routes ax_routes.init_routes(sanic_app=app, pages_path=os.environ.get('AX_PAGES_ABSOLUTE_PATH'), ssl_enabled=ssl_enabled) # Initiate SQL dialects module. Different SQL queries for differents DBs ax_dialects.init_dialects(os.environ.get('AX_DB_DIALECT') or 'sqlite') # Initiate email sender ax_emails.init_email(smtp_host=os.environ.get('AX_SMTP_HOST'), smtp_login=os.environ.get('AX_SMTP_LOGIN'), smtp_password=os.environ.get('AX_SMTP_PASSWORD'), port=os.environ.get('AX_SMTP_PORT'), ssl=os.environ.get('AX_SMTP_SSL')) # Initiate auth module ax_auth.init_auth(sanic_app=app, secret=os.environ.get('AX_AUTH_SECRET')) init_fields()