Fixed CSRF-related issues and profilepictures by starting caddy for development server too. Also improved boostrap script. #15 #16

This commit is contained in:
ChaoticByte 2023-02-11 18:04:52 +01:00
parent 5572fec9c1
commit 01aa31a8a1
8 changed files with 99 additions and 41 deletions

View file

@ -9,6 +9,7 @@ from pathlib import Path
from signal import SIGINT
from subprocess import Popen
from sys import path as sys_path
from sys import stdout, stderr
from time import sleep
from yaml import safe_load
@ -45,12 +46,19 @@ class MonitoredSubprocess:
if self._tries < self.max_tries:
self._tries += 1
print(f"Starting {self.name}...")
with self.logfile.open("ab") as l:
if self.logfile is None:
self.s = Popen(
self.commandline,
stdout=l,
stderr=l,
stdout=stdout.buffer,
stderr=stderr.buffer,
env=self.environment)
else:
with self.logfile.open("ab") as l:
self.s = Popen(
self.commandline,
stdout=l,
stderr=l,
env=self.environment)
return True
else:
print(f"Max. tries exceeded ({self.name})!")
@ -71,6 +79,30 @@ def cleanup_procs(processes):
p.stop()
def start_and_monitor(monitored_subprocesses: list):
# start processes
for p in monitored_subprocesses:
p.try_start()
register_exithandler(cleanup_procs, monitored_subprocesses)
# monitor processes
try:
while True:
sleep(1)
for p in monitored_subprocesses:
returncode = p.s.poll()
if returncode is None:
continue
else:
print(f"{p.name} stopped with exit code {returncode}.")
if p.try_start() is False:
# stop everything if the process
# has exceeded max. tries
exit()
except KeyboardInterrupt:
print("Received KeyboardInterrupt, exiting...")
exit()
if __name__ == "__main__":
argp = ArgumentParser()
argp.add_argument("--devel", help="Start development server", action="store_true")
@ -84,22 +116,31 @@ if __name__ == "__main__":
["./venv/bin/python3", "./manage.py", "collectstatic", "--noinput"], env=os.environ).wait()
Popen(
["./venv/bin/python3", "./manage.py", "migrate", "--noinput"], env=os.environ).wait()
# Caddy configuration via env
environment_caddy = os.environ
environment_caddy["DATADIR"] = str(data_directory.absolute())
environment_caddy["CADDY_HOST"] = str(config["caddy"]["host"])
environment_caddy["HTTP_PORT"] = str(config["caddy"]["http_port"])
environment_caddy["HTTPS_PORT"] = str(config["caddy"]["https_port"])
environment_caddy["APPLICATION_PORT"] = str(config["app"]["application_port"])
environment_caddy["ACCESS_LOG"] = config["logs"]["http_access"]
# Start
if args.devel:
p = None
try:
p = Popen(["./venv/bin/python3", "./manage.py", "runserver"], env=os.environ).wait()
except KeyboardInterrupt:
if p is not None:
p.send_signal(SIGINT)
procs = [
MonitoredSubprocess(
"Caddy Webserver",
["caddy", "run", "--config", str(caddyfile)],
None,
environment=environment_caddy
),
MonitoredSubprocess(
"Django Development Server",
["./venv/bin/python3", "./manage.py", "runserver", str(config["app"]["application_port"])],
None
),
]
start_and_monitor(procs)
else:
# Caddy configuration via env
environment_caddy = os.environ
environment_caddy["DATADIR"] = str(data_directory.absolute())
environment_caddy["HTTP_PORT"] = str(config["caddy"]["http_port"])
environment_caddy["HTTPS_PORT"] = str(config["caddy"]["https_port"])
environment_caddy["APPLICATION_PORT"] = str(config["app"]["application_port"])
environment_caddy["ACCESS_LOG"] = config["logs"]["http_access"]
# Application configuration via env
environment_app = os.environ
environment_app["APP_PROD"] = "1"
@ -128,24 +169,4 @@ if __name__ == "__main__":
environment=environment_app
),
]
# start processes
for p in procs:
p.try_start()
register_exithandler(cleanup_procs, procs)
# monitor processes
try:
while True:
sleep(1)
for p in procs:
returncode = p.s.poll()
if returncode is None:
continue
else:
print(f"{p.name} stopped with exit code {returncode}.")
if p.try_start() is False:
# stop everything if the process
# has exceeded max. tries
exit()
except KeyboardInterrupt:
print("Received KeyboardInterrupt, exiting...")
exit()
start_and_monitor(procs)