mirror of https://github.com/ekimekim/wubloader
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.4 KiB
Python
81 lines
2.4 KiB
Python
3 years ago
|
import logging
|
||
|
import os
|
||
3 years ago
|
from time import sleep
|
||
3 years ago
|
|
||
|
import argh
|
||
3 years ago
|
import gevent.event
|
||
3 years ago
|
from common import dateutil
|
||
|
from common.database import DBManager
|
||
|
from dateutil.parser import ParserError
|
||
3 years ago
|
from gevent import signal
|
||
3 years ago
|
from gevent.pywsgi import WSGIServer
|
||
|
|
||
|
from buscribeapi.buscribeapi import app
|
||
|
|
||
|
|
||
|
def cors(app):
|
||
|
"""WSGI middleware that sets CORS headers"""
|
||
|
HEADERS = [
|
||
|
("Access-Control-Allow-Credentials", "false"),
|
||
|
("Access-Control-Allow-Headers", "*"),
|
||
|
("Access-Control-Allow-Methods", "GET,HEAD"),
|
||
|
("Access-Control-Allow-Origin", "*"),
|
||
|
("Access-Control-Max-Age", "86400"),
|
||
|
]
|
||
|
|
||
|
def handle(environ, start_response):
|
||
|
def _start_response(status, headers, exc_info=None):
|
||
|
headers += HEADERS
|
||
|
return start_response(status, headers, exc_info)
|
||
|
|
||
|
return app(environ, _start_response)
|
||
|
|
||
|
return handle
|
||
|
|
||
|
|
||
3 years ago
|
def servelet(server):
|
||
|
logging.info('Starting WSGI server.')
|
||
|
server.serve_forever()
|
||
|
|
||
3 years ago
|
@argh.arg('--host',
|
||
|
help='Address or socket server will listen to. Default is 0.0.0.0 (everything on the local machine).')
|
||
|
@argh.arg('--port',
|
||
2 months ago
|
help='Port server will listen on.')
|
||
3 years ago
|
@argh.arg('--database',
|
||
|
help='Postgres connection string, which is either a space-separated list of key=value pairs, or a URI like: '
|
||
|
'postgresql://USER:PASSWORD@HOST/DBNAME?KEY=VALUE')
|
||
|
@argh.arg('--bustime-start',
|
||
|
help='The start time in UTC for the event, for UTC-Bustime conversion')
|
||
2 months ago
|
def main(database="", host='0.0.0.0', port=8010, bustime_start=None):
|
||
3 years ago
|
if bustime_start is None:
|
||
|
logging.error("Missing --bustime-start!")
|
||
|
exit(1)
|
||
|
|
||
|
server = WSGIServer((host, port), cors(app))
|
||
|
|
||
|
try:
|
||
|
app.bustime_start = dateutil.parse(bustime_start)
|
||
|
except ParserError:
|
||
|
logging.error("Invalid --bustime-start!")
|
||
|
exit(1)
|
||
|
|
||
1 month ago
|
app.db_manager = DBManager(dsn=database, register_types=False)
|
||
3 years ago
|
|
||
3 years ago
|
stopping = gevent.event.Event()
|
||
|
|
||
|
def stop():
|
||
|
logging.info("Shutting down")
|
||
|
stopping.set()
|
||
|
|
||
|
gevent.signal_handler(signal.SIGTERM, stop)
|
||
|
|
||
|
serve = gevent.spawn(servelet, server)
|
||
|
|
||
|
# Wait for either the stop signal or the server to oops out.
|
||
|
gevent.wait([serve, stopping], count=1)
|
||
|
|
||
|
server.stop()
|
||
|
serve.get() # Wait for server to shut down and/or re-raise if serve_forever() errored
|
||
|
|
||
3 years ago
|
logging.info("Gracefully shut down")
|