-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
76 lines (58 loc) · 1.77 KB
/
main.py
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
"""
path: app/main.py
"""
import logging
import sys
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.sessions import SessionMiddleware
from app.scheduler import start as start_scheduler
from app.routers import main
def configure_logging():
"""
Configure logging for the application
"""
# Create a root logger
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
# Create a formatter for formatting log messages
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
# Create a handler for logging to stdout
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setLevel(logging.INFO)
stream_handler.setFormatter(formatter)
# Add the handlers to the root logger
root_logger.addHandler(stream_handler)
# log into a file
file_handler = logging.FileHandler("app.log")
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(formatter)
root_logger.addHandler(file_handler)
configure_logging()
@asynccontextmanager
async def startup_event(app: FastAPI):
"""
Start the scheduler
"""
start_scheduler()
yield
app = FastAPI(lifespan=startup_event)
app.include_router(main.router)
origins = [
"*", # Allow all origins
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
# Configure your secret key
app.add_middleware(SessionMiddleware, secret_key="your-secret-key-here")
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)