-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathapp.py
More file actions
70 lines (55 loc) · 1.79 KB
/
app.py
File metadata and controls
70 lines (55 loc) · 1.79 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
from __future__ import annotations
import os
from contextlib import asynccontextmanager
import uvicorn
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from examples.auth.lifespan import global_lifespan
from examples.local_notification_server.fcm_service import init_firebase_app
from examples.local_notification_server.routers import (
router as notification_router,
)
load_dotenv()
@asynccontextmanager
async def notification_lifespan(app: FastAPI):
"""
Lifespan event handler for FastAPI app.
Initialise global resources (DB/Redis) and Firebase Admin SDK at startup.
"""
cred_path = os.getenv(
'FIREBASE_CRED_PATH',
'path/to/your/firebase/credentials.json',
)
project_id = os.getenv('FIREBASE_PROJECT_ID', 'your-firebase-project-id')
async with global_lifespan(app):
init_firebase_app(cred_path=cred_path, project_id=project_id)
yield
app: FastAPI = FastAPI(lifespan=notification_lifespan)
# Add Cross-Origin Resource Sharing (CORS) middleware
app.add_middleware(
CORSMiddleware,
allow_origins=['*'], # Allow all origins (adjust this in production)
allow_credentials=True,
allow_methods=['*'], # Allow all HTTP methods
allow_headers=['*'], # Allow all headers
)
# Include routers for notification services
app.include_router(notification_router)
def main() -> None:
"""
Main function to run the FastAPI application using Uvicorn.
"""
uvicorn.run(app, host='127.0.0.1', port=8003)
if __name__ == '__main__':
main()
"""
uvicorn examples.local_notification_server.app:app\
--host 127.0.0.1 \
--port 8003 \
--workers 4
uv run uvicorn examples.local_notification_server.app:app\
--host 127.0.0.1 \
--port 8003 \
--workers 4
"""