-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
101 lines (85 loc) · 2.77 KB
/
Copy pathmain.py
File metadata and controls
101 lines (85 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os, sys
import asyncio
from contextlib import asynccontextmanager
from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse, FileResponse
from fastapi.templating import Jinja2Templates
from app.core.config import settings
from app.core.database import engine, Base
from app.api.routes import router
from app.services.notification_service import Notification
from app.services.background_tasks import background_task_manager
# Create database tables
Base.metadata.create_all(bind=engine)
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup
print("🚀 Starting KleinManager...")
print("📋 Starting background monitoring tasks...")
await background_task_manager.start_all_tasks()
yield
# Shutdown
print("🛑 Stopping background monitoring tasks...")
await background_task_manager.stop_all_tasks()
print("👋 KleinManager shutdown complete")
if getattr(sys, 'frozen', False):
base_path = sys._MEIPASS
else:
base_path = os.path.abspath(".")
app = FastAPI(
title=settings.APP_NAME,
version=settings.APP_VERSION,
debug=settings.DEBUG,
lifespan=lifespan
)
static_dir = os.path.join(base_path, "static")
app.mount("/static", StaticFiles(directory=static_dir), name="static")
templates = Jinja2Templates(directory=os.path.join(base_path, "templates"))
app.include_router(router)
@app.get("/", response_class=HTMLResponse)
async def root(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.get("/images/{filename}")
async def get_image(filename: str):
file_path = os.path.join(settings.IMAGE_STORAGE_PATH, filename)
if os.path.exists(file_path):
return FileResponse(file_path)
return {"error": "Image not found"}
import logging
LOGGING_CONFIG = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"default": {
"format": "%(levelname)s | %(message)s"
},
},
"handlers": {
"default": {
"formatter": "default",
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
},
},
"root": {
"level": "INFO",
"handlers": ["default"]
},
}
if __name__ == "__main__":
import uvicorn, webbrowser, threading, time
def open_browser():
time.sleep(1)
webbrowser.open("http://localhost:8000")
threading.Thread(target=open_browser).start()
print(f"🚀 {settings.APP_NAME} v{settings.APP_VERSION} starting...")
print(f"📱 Open http://localhost:8000")
print(f"📚 API docs: http://localhost:8000/docs")
uvicorn.run(
app,
host="0.0.0.0",
port=8000,
reload=settings.DEBUG,
log_config=LOGGING_CONFIG
)