alessandro trinca tornidor
commited on
Commit
·
9f7342c
1
Parent(s):
5b67a75
feat: add STATIC_FOLDER, API_MODE env variables
Browse files- my_ghost_writer/app.py +24 -11
- my_ghost_writer/constants.py +2 -0
my_ghost_writer/app.py
CHANGED
@@ -7,7 +7,7 @@ from fastapi.responses import FileResponse, JSONResponse
|
|
7 |
from fastapi.staticfiles import StaticFiles
|
8 |
import uvicorn
|
9 |
|
10 |
-
from my_ghost_writer.constants import ALLOWED_ORIGIN_LIST, DOMAIN, IS_TESTING, LOG_LEVEL, PORT, STATIC_FOLDER, app_logger
|
11 |
from my_ghost_writer.type_hints import RequestTextFrequencyBody
|
12 |
|
13 |
|
@@ -60,22 +60,35 @@ def get_words_frequency(body: RequestTextFrequencyBody | str) -> JSONResponse:
|
|
60 |
return JSONResponse(status_code=200, content=content_response)
|
61 |
|
62 |
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
# add the CorrelationIdMiddleware AFTER the @app.middleware("http") decorated function to avoid missing request id
|
66 |
app.add_middleware(CorrelationIdMiddleware)
|
67 |
|
68 |
|
69 |
-
|
70 |
-
@app.get("/
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
|
75 |
if __name__ == "__main__":
|
76 |
try:
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
7 |
from fastapi.staticfiles import StaticFiles
|
8 |
import uvicorn
|
9 |
|
10 |
+
from my_ghost_writer.constants import ALLOWED_ORIGIN_LIST, API_MODE, DOMAIN, IS_TESTING, LOG_LEVEL, PORT, STATIC_FOLDER, app_logger
|
11 |
from my_ghost_writer.type_hints import RequestTextFrequencyBody
|
12 |
|
13 |
|
|
|
60 |
return JSONResponse(status_code=200, content=content_response)
|
61 |
|
62 |
|
63 |
+
try:
|
64 |
+
app.mount("/static", StaticFiles(directory=STATIC_FOLDER, html=True), name="static")
|
65 |
+
except Exception as ex_mount_static:
|
66 |
+
app_logger.error(f"Failed to mount static folder: {STATIC_FOLDER}, exception: {ex_mount_static}!")
|
67 |
+
if not API_MODE:
|
68 |
+
app_logger.exception(f"since API_MODE is {API_MODE} we will raise the exception!")
|
69 |
+
raise ex_mount_static
|
70 |
|
71 |
# add the CorrelationIdMiddleware AFTER the @app.middleware("http") decorated function to avoid missing request id
|
72 |
app.add_middleware(CorrelationIdMiddleware)
|
73 |
|
74 |
|
75 |
+
try:
|
76 |
+
@app.get("/")
|
77 |
+
@app.get("/static/")
|
78 |
+
def index() -> FileResponse:
|
79 |
+
return FileResponse(path=STATIC_FOLDER / "index.html", media_type="text/html")
|
80 |
+
except Exception as ex_route_main:
|
81 |
+
app_logger.error(f"Failed to prepare the main route, exception: {ex_route_main}!")
|
82 |
+
if not API_MODE:
|
83 |
+
app_logger.exception(f"since API_MODE is {API_MODE} we will raise the exception!")
|
84 |
+
raise ex_route_main
|
85 |
|
86 |
|
87 |
if __name__ == "__main__":
|
88 |
try:
|
89 |
+
app_logger.info(f"Starting fastapi/gradio application {fastapi_title}, run in api mode: {API_MODE}...")
|
90 |
+
uvicorn.run("my_ghost_writer.app:app", host=DOMAIN, port=PORT, reload=bool(IS_TESTING))
|
91 |
+
except Exception as ex_run:
|
92 |
+
print(f"fastapi/gradio application {fastapi_title}, exception:{ex_run}!")
|
93 |
+
app_logger.exception(f"fastapi/gradio application {fastapi_title}, exception:{ex_run}!")
|
94 |
+
raise ex_run
|
my_ghost_writer/constants.py
CHANGED
@@ -9,11 +9,13 @@ from my_ghost_writer import session_logger
|
|
9 |
load_dotenv()
|
10 |
PROJECT_ROOT_FOLDER = Path(__file__).parent.parent
|
11 |
STATIC_FOLDER = PROJECT_ROOT_FOLDER / "static"
|
|
|
12 |
DOMAIN=os.getenv("DOMAIN", "localhost")
|
13 |
PORT=int(os.getenv("PORT", 7860))
|
14 |
ALLOWED_ORIGIN_LIST = os.getenv('ALLOWED_ORIGIN', f'http://{DOMAIN}:{PORT}').split(",")
|
15 |
LOG_JSON_FORMAT = bool(os.getenv("LOG_JSON_FORMAT"))
|
16 |
IS_TESTING = bool(os.getenv('IS_TESTING', ""))
|
17 |
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
|
|
|
18 |
session_logger.setup_logging(json_logs=LOG_JSON_FORMAT, log_level=LOG_LEVEL)
|
19 |
app_logger = structlog.stdlib.get_logger(__name__)
|
|
|
9 |
load_dotenv()
|
10 |
PROJECT_ROOT_FOLDER = Path(__file__).parent.parent
|
11 |
STATIC_FOLDER = PROJECT_ROOT_FOLDER / "static"
|
12 |
+
STATIC_FOLDER = Path(os.getenv("STATIC_FOLDER", str(STATIC_FOLDER)))
|
13 |
DOMAIN=os.getenv("DOMAIN", "localhost")
|
14 |
PORT=int(os.getenv("PORT", 7860))
|
15 |
ALLOWED_ORIGIN_LIST = os.getenv('ALLOWED_ORIGIN', f'http://{DOMAIN}:{PORT}').split(",")
|
16 |
LOG_JSON_FORMAT = bool(os.getenv("LOG_JSON_FORMAT"))
|
17 |
IS_TESTING = bool(os.getenv('IS_TESTING', ""))
|
18 |
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
|
19 |
+
API_MODE = bool(os.getenv("API_MODE", ""))
|
20 |
session_logger.setup_logging(json_logs=LOG_JSON_FORMAT, log_level=LOG_LEVEL)
|
21 |
app_logger = structlog.stdlib.get_logger(__name__)
|