2023-02-16 06:06:42 +01:00
#!/usr/bin/env python3
import argparse
import yaml
2024-01-09 09:07:04 +01:00
from os import path , mkdir , access , W_OK , environ
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 (
" core.allnet:app " ,
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 ,
ssl_keyfile = cfg . billing . ssl_key
)
server = uvicorn . Server ( server_cfg )
if cfg . billing . standalone :
await server . serve ( )
else :
while True :
pass
2023-03-09 17:38:58 +01:00
2024-01-09 09:07:04 +01:00
async def launcher ( cfg : CoreConfig , ssl : bool ) - > None :
AimedbServlette ( cfg ) . start ( )
done , pending = await asyncio . wait (
[
asyncio . create_task ( launch_main ( cfg , ssl ) ) ,
asyncio . create_task ( launch_billing ( cfg ) ) ,
2023-03-09 17:38:58 +01:00
2024-01-09 09:07:04 +01: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 17:38:58 +01:00
2023-02-16 06:06:42 +01:00
if __name__ == " __main__ " :
2024-01-09 09:07:04 +01:00
parser = argparse . ArgumentParser ( description = " Artemis main entry point " )
2023-03-09 17:38:58 +01:00
parser . add_argument (
" --config " , " -c " , type = str , default = " config " , help = " Configuration folder "
)
2024-01-09 09:07:04 +01: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 06:06:42 +01:00
args = parser . parse_args ( )
if not path . exists ( f " { args . config } /core.yaml " ) :
2023-03-09 17:38:58 +01:00
print (
2024-01-09 09:07:04 +01: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 17:38:58 +01:00
)
2024-01-09 09:07:04 +01:00
2023-02-16 06:06:42 +01:00
cfg : CoreConfig = CoreConfig ( )
2023-03-05 03:58:51 +01:00
if path . exists ( f " { args . config } /core.yaml " ) :
cfg . update ( yaml . safe_load ( open ( f " { args . config } /core.yaml " ) ) )
2023-02-16 06:06:42 +01:00
2024-01-09 09:07:04 +01:00
environ [ " ARTEMIS_CFG_DIR " ] = args . config
2023-02-24 20:07:54 +01:00
2024-01-09 09:07:04 +01:00
asyncio . run ( launcher ( cfg , args . ssl ) )