2023-02-16 05:06:42 +00:00
#!/usr/bin/env python3
import argparse
import yaml
2024-01-11 17:08:22 +00:00
from os import path , environ
2024-01-09 08:07:04 +00:00
import uvicorn
import logging
import asyncio
from core import CoreConfig , AimedbServlette
async def launch_main ( cfg : CoreConfig , ssl : bool ) - > None :
if ssl :
server_cfg = uvicorn . Config (
" core.app:app " ,
host = cfg . server . listen_address ,
port = cfg . server . port if args . port == 0 else args . port ,
reload = cfg . server . is_develop ,
log_level = " info " if cfg . server . is_develop else " critical " ,
ssl_version = 3 ,
ssl_certfile = cfg . server . ssl_cert ,
ssl_keyfile = cfg . server . ssl_key
)
else :
server_cfg = uvicorn . Config (
" core.app:app " ,
host = cfg . server . listen_address ,
port = cfg . server . port if args . port == 0 else args . port ,
reload = cfg . server . is_develop ,
log_level = " info " if cfg . server . is_develop else " critical "
)
server = uvicorn . Server ( server_cfg )
await server . serve ( )
async def launch_billing ( cfg : CoreConfig ) - > None :
server_cfg = uvicorn . Config (
2024-01-11 17:08:22 +00:00
" core.allnet:app_billing " ,
host = cfg . server . listen_address ,
port = cfg . billing . port ,
reload = cfg . server . is_develop ,
log_level = " info " if cfg . server . is_develop else " critical " ,
ssl_version = 3 ,
ssl_certfile = cfg . billing . ssl_cert ,
2024-02-09 14:56:38 +00:00
ssl_keyfile = cfg . billing . ssl_key ,
ssl_ciphers = " DEFAULT:!aNULL:!eNULL:!MD5:!3DES:!DES:!RC4:!IDEA:!SEED:!aDSS:!SRP:!PSK " ,
2024-01-11 17:08:22 +00:00
)
2024-01-09 08:07:04 +00:00
server = uvicorn . Server ( server_cfg )
2024-01-11 17:08:22 +00:00
await server . serve ( )
async def launch_frontend ( cfg : CoreConfig ) - > None :
server_cfg = uvicorn . Config (
" core.frontend:app " ,
host = cfg . server . listen_address ,
port = cfg . frontend . port ,
reload = cfg . server . is_develop ,
log_level = " info " if cfg . server . is_develop else " critical " ,
)
server = uvicorn . Server ( server_cfg )
await server . serve ( )
async def launch_allnet ( cfg : CoreConfig ) - > None :
server_cfg = uvicorn . Config (
" core.allnet:app_allnet " ,
host = cfg . server . listen_address ,
port = cfg . allnet . port ,
reload = cfg . server . is_develop ,
log_level = " info " if cfg . server . is_develop else " critical " ,
)
server = uvicorn . Server ( server_cfg )
await server . serve ( )
2023-03-09 16:38:58 +00:00
2024-01-09 08:07:04 +00:00
async def launcher ( cfg : CoreConfig , ssl : bool ) - > None :
2024-01-11 17:08:22 +00:00
task_list = [ asyncio . create_task ( launch_main ( cfg , ssl ) ) ]
2024-01-11 17:25:35 +00:00
2024-01-11 17:08:22 +00:00
if cfg . billing . standalone :
task_list . append ( asyncio . create_task ( launch_billing ( cfg ) ) )
if cfg . frontend . enable :
task_list . append ( asyncio . create_task ( launch_frontend ( cfg ) ) )
if cfg . allnet . standalone :
task_list . append ( asyncio . create_task ( launch_allnet ( cfg ) ) )
2024-01-11 17:25:35 +00:00
if cfg . aimedb . enable :
AimedbServlette ( cfg ) . start ( )
2024-01-11 17:08:22 +00:00
2024-01-09 08:07:04 +00:00
done , pending = await asyncio . wait (
2024-01-11 17:08:22 +00:00
task_list ,
2024-01-09 08:07:04 +00:00
return_when = asyncio . FIRST_COMPLETED ,
)
logging . getLogger ( " core " ) . info ( " Shutdown " )
for pending_task in pending :
pending_task . cancel ( " Another service died, server is shutting down " )
2023-03-09 16:38:58 +00:00
2023-02-16 05:06:42 +00:00
if __name__ == " __main__ " :
2024-01-09 08:07:04 +00:00
parser = argparse . ArgumentParser ( description = " Artemis main entry point " )
2023-03-09 16:38:58 +00:00
parser . add_argument (
" --config " , " -c " , type = str , default = " config " , help = " Configuration folder "
)
2024-01-09 08:07:04 +00:00
parser . add_argument (
" --port " , " -p " , type = int , default = 0 , help = " Port override "
)
parser . add_argument (
" --ssl " , " -s " , type = bool , help = " Launch with SSL "
)
2023-02-16 05:06:42 +00:00
args = parser . parse_args ( )
if not path . exists ( f " { args . config } /core.yaml " ) :
2023-03-09 16:38:58 +00:00
print (
2024-01-09 08:07:04 +00:00
f " The config folder you specified ( { args . config } ) does not exist or does not contain core.yaml. Defaults will be used. \n Did you copy the example folder? "
2023-03-09 16:38:58 +00:00
)
2024-01-09 08:07:04 +00:00
2023-02-16 05:06:42 +00:00
cfg : CoreConfig = CoreConfig ( )
2023-03-05 02:58:51 +00:00
if path . exists ( f " { args . config } /core.yaml " ) :
cfg . update ( yaml . safe_load ( open ( f " { args . config } /core.yaml " ) ) )
2023-02-16 05:06:42 +00:00
2024-01-09 08:07:04 +00:00
environ [ " ARTEMIS_CFG_DIR " ] = args . config
2023-02-24 19:07:54 +00:00
2024-01-09 08:07:04 +00:00
asyncio . run ( launcher ( cfg , args . ssl ) )