2022-03-16 12:11:30 +01:00
#!/usr/bin/env python3
from os import environ
from os import getcwd
from shlex import quote
from signal import SIGINT
from subprocess import run
from subprocess import Popen
from sys import argv
from sys import stdout
from sys import stderr
2022-04-13 20:36:24 +02:00
# some vars
2022-03-16 12:11:30 +01:00
devel = False
2022-04-13 20:36:24 +02:00
caddy_process = None
scs_process = None
app_process = None
2022-03-16 12:11:30 +01:00
2022-04-13 20:36:24 +02:00
def stop ( ) :
print ( " \n \n Stopping services. \n \n " )
caddy_process . send_signal ( SIGINT )
scs_process . send_signal ( SIGINT )
app_process . send_signal ( SIGINT )
print ( f " Caddy stopped with exit code { caddy_process . wait ( ) } . " )
print ( f " session-clear-scheduler stopped with exit code { scs_process . wait ( ) } . " )
if devel :
print ( f " Django stopped with exit code { app_process . wait ( ) } . " )
else :
print ( f " Django/Uvicorn stopped with exit code { app_process . wait ( ) } . " )
if caddy_process . returncode != 0 or scs_process . returncode != 0 or app_process . returncode != 0 :
exit ( 1 )
else :
exit ( 0 )
2022-03-16 12:11:30 +01:00
2022-04-13 20:36:24 +02:00
if __name__ == " __main__ " :
# development or production environment?
try :
if argv [ 1 ] == " devel " :
devel = True
except IndexError :
pass
# vars
pwd = getcwd ( )
APPLICATION_LOG = environ [ " APPLICATION_LOG " ]
CADDY_ACCESS_LOG = environ [ " CADDY_ACCESS_LOG " ]
CADDY_LOG = environ [ " CADDY_LOG " ]
DJANGO_PORT = environ [ " DJANGO_PORT " ]
HTTPS_PORT = environ [ " HTTPS_PORT " ]
2022-03-16 12:11:30 +01:00
if devel :
2022-04-13 20:36:24 +02:00
environ [ " DJANGO_DEBUG " ] = " true "
2022-03-16 12:11:30 +01:00
else :
2022-04-13 20:36:24 +02:00
environ [ " DJANGO_DEBUG " ] = " false "
# info
2023-01-10 23:41:01 +01:00
print ( f " \n \n Starting server on port { HTTPS_PORT } ... \n You should be able to access the application locally at https://127.0.0.1: { HTTPS_PORT } / \n \n Press Ctrl+C to stop all services. \n \n " )
2022-04-13 20:36:24 +02:00
if not devel :
print ( f " All further messages will be written to { APPLICATION_LOG } and { CADDY_LOG } " )
print ( f " HTTP Access Log will be written to { CADDY_ACCESS_LOG } " )
try :
# start django/uvicorn
if devel :
run (
[ " python3 " , f " { pwd } /application/manage.py " , " collectstatic " , " --noinput " ] ,
stdout = stdout ,
stderr = stderr ,
env = environ
)
app_process = Popen (
2023-01-10 23:41:01 +01:00
[ " python3 " , f " { pwd } /application/manage.py " , " runserver " , f " 127.0.0.1: { DJANGO_PORT } " ] ,
2022-04-13 20:36:24 +02:00
stdout = stdout ,
stderr = stderr ,
env = environ
)
else :
application_log_file = open ( APPLICATION_LOG , " a " )
run (
[ " python3 " , f " { pwd } /application/manage.py " , " collectstatic " , " --noinput " ] ,
stdout = application_log_file ,
stderr = application_log_file ,
env = environ
)
app_process = Popen (
[
" python3 " , " -m " , " uvicorn " ,
2023-01-10 23:41:01 +01:00
" --host " , " 127.0.0.1 " ,
2022-04-13 20:36:24 +02:00
" --port " , quote ( DJANGO_PORT ) ,
" drinks_manager.asgi:application "
] ,
stdout = application_log_file ,
stderr = application_log_file ,
cwd = f " { pwd } /application/ " ,
env = environ
)
# start caddy
if devel :
caddy_log_file = stdout
caddy_log_file_stderr = stderr
else :
caddy_log_file = caddy_log_file_stderr = open ( CADDY_LOG , " a " )
caddy_process = Popen (
[ " caddy " , " run " , " --config " , f " { pwd } /config/Caddyfile " ] ,
stdout = caddy_log_file ,
stderr = caddy_log_file_stderr ,
env = environ
)
# start session-clear-scheduler
if devel :
clear_sched_log_file = stdout
clear_sched_log_file_stderr = stderr
else :
clear_sched_log_file = clear_sched_log_file_stderr = open ( APPLICATION_LOG , " a " )
scs_process = Popen (
[ " python3 " , f " { pwd } /lib/session-clear-scheduler.py " ] ,
stdout = clear_sched_log_file ,
stderr = clear_sched_log_file_stderr
)
caddy_process . wait ( )
scs_process . wait ( )
app_process . wait ( )
except KeyboardInterrupt :
stop ( )