From 15376a105258836e57da506ea67252ddcdef9ae7 Mon Sep 17 00:00:00 2001 From: boyska Date: Wed, 25 Aug 2021 00:53:38 +0200 Subject: [PATCH] initialization as fastcgi event --- techrec/cli.py | 3 ++- techrec/server.py | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/techrec/cli.py b/techrec/cli.py index c190cb4..df95c16 100644 --- a/techrec/cli.py +++ b/techrec/cli.py @@ -60,6 +60,7 @@ class DateTimeAction(Action): raise ValueError("'%s' is not a valid datetime" % values) setattr(namespace, self.dest, parsed_val) +code_dir = os.path.dirname(os.path.realpath(__file__)) def common_pre(): prechecks = [pre_check_user, pre_check_permissions, pre_check_ffmpeg] @@ -76,7 +77,7 @@ def common_pre(): if getattr(sys, "frozen", False): os.chdir(sys._MEIPASS) else: - os.chdir(os.path.dirname(os.path.realpath(__file__))) + os.chdir(code_dir) for conf in configs: get_config().from_pyfile(conf) diff --git a/techrec/server.py b/techrec/server.py index bb9c378..73948a1 100644 --- a/techrec/server.py +++ b/techrec/server.py @@ -21,7 +21,7 @@ logger = logging.getLogger("server") common_pre() app = FastAPI() -db = RecDB(get_config()["DB_URI"]) +db = None def date_read(s): @@ -38,6 +38,11 @@ def rec_sanitize(rec): d["endtime"] = date_write(d["endtime"]) return d +@app.on_event("startup") +async def startup_event(): + global db + common_pre() + db = RecDB(get_config()["DB_URI"]) @app.get("/date/date") def date():