2020-07-01 19:39:13 +00:00
|
|
|
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
|
|
|
|
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: 0BSD
|
|
|
|
|
2020-07-11 10:54:07 +00:00
|
|
|
""" Entrypoint of Kibicara. """
|
|
|
|
|
2020-07-01 19:39:13 +00:00
|
|
|
from asyncio import run as asyncio_run
|
|
|
|
from fastapi import FastAPI
|
2020-07-25 11:34:18 +00:00
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
2020-07-01 19:39:13 +00:00
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from hypercorn.config import Config
|
|
|
|
from hypercorn.asyncio import serve
|
2020-07-15 14:46:13 +00:00
|
|
|
from kibicara.config import args, config
|
2020-07-01 19:39:13 +00:00
|
|
|
from kibicara.model import Mapping
|
|
|
|
from kibicara.platformapi import Spawner
|
|
|
|
from kibicara.webapi import router
|
2020-07-15 14:46:13 +00:00
|
|
|
from logging import basicConfig, DEBUG, getLogger, INFO, WARNING
|
2020-07-01 19:39:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Main:
|
2020-09-04 23:39:24 +00:00
|
|
|
"""Entrypoint for Kibicara.
|
2020-07-11 10:54:07 +00:00
|
|
|
|
|
|
|
Initializes the platform bots and starts the hypercorn webserver serving the
|
|
|
|
Kibicara application and the specified frontend on port 8000.
|
|
|
|
"""
|
|
|
|
|
2020-07-01 19:39:13 +00:00
|
|
|
def __init__(self):
|
2020-07-11 10:54:07 +00:00
|
|
|
asyncio_run(self.__run())
|
2020-07-01 19:39:13 +00:00
|
|
|
|
2020-07-11 10:54:07 +00:00
|
|
|
async def __run(self):
|
2020-07-15 14:46:13 +00:00
|
|
|
LOGLEVELS = {
|
|
|
|
None: WARNING,
|
|
|
|
1: INFO,
|
|
|
|
2: DEBUG,
|
|
|
|
}
|
|
|
|
basicConfig(
|
|
|
|
level=LOGLEVELS.get(args.verbose, DEBUG),
|
|
|
|
format="%(asctime)s %(name)s %(message)s",
|
|
|
|
)
|
2020-07-06 12:50:52 +00:00
|
|
|
getLogger('aiosqlite').setLevel(WARNING)
|
2020-07-01 19:39:13 +00:00
|
|
|
Mapping.create_all()
|
|
|
|
await Spawner.init_all()
|
2020-07-11 10:54:07 +00:00
|
|
|
await self.__start_webserver()
|
2020-07-01 19:39:13 +00:00
|
|
|
|
2020-07-11 10:54:07 +00:00
|
|
|
async def __start_webserver(self):
|
2020-07-01 19:39:13 +00:00
|
|
|
class SinglePageApplication(StaticFiles):
|
|
|
|
async def get_response(self, path, scope):
|
|
|
|
response = await super().get_response(path, scope)
|
|
|
|
if response.status_code == 404:
|
|
|
|
response = await super().get_response('.', scope)
|
|
|
|
return response
|
2020-07-02 12:31:53 +00:00
|
|
|
|
2020-07-01 19:39:13 +00:00
|
|
|
app = FastAPI()
|
|
|
|
server_config = Config()
|
|
|
|
server_config.accesslog = '-'
|
2020-09-11 02:05:33 +00:00
|
|
|
server_config.behind_proxy = config['behind_proxy']
|
2020-09-11 16:21:24 +00:00
|
|
|
server_config.keyfile = config['keyfile']
|
|
|
|
server_config.certfile = config['certfile']
|
2020-09-10 17:29:57 +00:00
|
|
|
if config['production']:
|
|
|
|
server_config.bind = ['0.0.0.0:8000', '[::]:8000']
|
2020-09-10 20:57:05 +00:00
|
|
|
api = FastAPI()
|
|
|
|
api.include_router(router)
|
|
|
|
app.mount('/api', api)
|
2020-07-25 11:34:18 +00:00
|
|
|
if not config['production'] and config['cors_allow_origin']:
|
|
|
|
app.add_middleware(
|
|
|
|
CORSMiddleware,
|
|
|
|
allow_origins=config['cors_allow_origin'],
|
|
|
|
allow_credentials=True,
|
|
|
|
allow_methods=["*"],
|
|
|
|
allow_headers=["*"],
|
|
|
|
)
|
2020-07-01 19:39:13 +00:00
|
|
|
if config['frontend_path'] is not None:
|
2020-07-02 12:31:53 +00:00
|
|
|
app.mount(
|
|
|
|
'/',
|
|
|
|
app=SinglePageApplication(directory=config['frontend_path'], html=True),
|
|
|
|
)
|
2020-07-01 19:39:13 +00:00
|
|
|
await serve(app, server_config)
|