cic-internal-integration/apps/cic-eth/cic_eth/runnable/daemons/server.py

41 lines
1.2 KiB
Python
Raw Normal View History

2021-10-25 16:51:45 +02:00
# standard imports
import logging
2021-12-02 13:07:49 +01:00
from urllib.parse import urlparse
2021-10-25 16:51:45 +02:00
2021-12-02 13:07:49 +01:00
import redis
from cic_eth.server import args, cache, config, openapi, routes
2021-11-19 14:50:13 +01:00
2021-12-02 13:07:49 +01:00
# define log levels
if args.vv:
logging.getLogger().setLevel(logging.DEBUG)
elif args.v:
logging.getLogger().setLevel(logging.INFO)
2021-11-18 13:38:20 +01:00
2021-12-02 13:07:49 +01:00
log = logging.getLogger(__name__)
2021-10-25 16:51:45 +02:00
2021-12-02 13:07:49 +01:00
# TODO Censor relevant things
log.debug(f"{config}")
2021-11-18 13:38:20 +01:00
2021-12-02 13:07:49 +01:00
# define universal redis cache access
cache.Cache.store = redis.StrictRedis(host=config.get('REDIS_HOST'),
port=config.get('REDIS_PORT'),
db=config.get('REDIS_DB'),
decode_responses=True)
2021-10-25 16:51:45 +02:00
# uwsgi application
2021-10-25 16:51:45 +02:00
def application(env, start_response):
# Validate incoming request against the open api spec
2021-12-02 13:07:49 +01:00
errors, request = openapi.validate.request(env, start_response)
if errors:
return errors
2021-11-23 13:17:13 +01:00
parsed_url = urlparse(env.get('REQUEST_URI'))
2021-10-25 16:51:45 +02:00
path = parsed_url.path
2021-12-02 13:07:49 +01:00
query = dict(request.parameters.query)
handler = routes.get(path)
if handler:
return handler(start_response, query)
start_response('404 This is not the path you\'re looking for', [])
return []