1- import os
2- import re
3- import firebase_admin
4- import logging
1+ from collections .abc import AsyncGenerator
52from contextlib import asynccontextmanager
6- from typing import AsyncGenerator
3+ from logging . config import dictConfig
74
5+ import firebase_admin
86from fastapi import FastAPI
97from fastapi .middleware .cors import CORSMiddleware
10- from logging .config import dictConfig
118
129from .config import settings
1310from .models import init_app as init_models
1411from .routers import init_app as init_routers
1512
1613
17- def configure_logging ():
14+ def configure_logging () -> None :
1815 """Configure application logging based on environment"""
19-
16+
2017 # Base configuration that applies to all environments
2118 base_config = {
2219 "version" : 1 ,
@@ -25,14 +22,12 @@ def configure_logging():
2522 "detailed" : {
2623 "format" : "%(asctime)s - %(name)s - %(levelname)s - %(module)s:%(lineno)d - %(message)s"
2724 },
28- "simple" : {
29- "format" : "%(levelname)s - %(message)s"
30- },
25+ "simple" : {"format" : "%(levelname)s - %(message)s" },
3126 },
3227 "handlers" : {},
3328 "root" : {},
3429 }
35-
30+
3631 if settings .is_development :
3732 # Development: Log to console with INFO level, and errors to file
3833 base_config ["handlers" ] = {
@@ -49,19 +44,18 @@ def configure_logging():
4944 "formatter" : "detailed" ,
5045 },
5146 }
52- base_config ["root" ] = {
53- "level" : "INFO" ,
54- "handlers" : ["console" , "file" ]
55- }
56-
47+ base_config ["root" ] = {"level" : "INFO" , "handlers" : ["console" , "file" ]}
48+
5749 # Set specific loggers to appropriate levels
5850 base_config ["loggers" ] = {
5951 "uvicorn" : {"level" : "INFO" },
6052 "uvicorn.access" : {"level" : "INFO" },
61- "sqlalchemy.engine" : {"level" : "INFO" }, # Use "WARNING" to avoid SQL query noise
53+ "sqlalchemy.engine" : {
54+ "level" : "INFO"
55+ }, # Use "WARNING" to avoid SQL query noise
6256 "app" : {"level" : "DEBUG" }, # Your app logs at DEBUG level
6357 }
64-
58+
6559 elif settings .is_testing :
6660 # Testing: Minimal logging to avoid test output noise
6761 base_config ["handlers" ] = {
@@ -72,11 +66,8 @@ def configure_logging():
7266 "stream" : "ext://sys.stdout" ,
7367 },
7468 }
75- base_config ["root" ] = {
76- "level" : "WARNING" ,
77- "handlers" : ["console" ]
78- }
79-
69+ base_config ["root" ] = {"level" : "WARNING" , "handlers" : ["console" ]}
70+
8071 else : # Production
8172 # Production: Only errors to file, warnings and above to console
8273 base_config ["handlers" ] = {
@@ -93,23 +84,22 @@ def configure_logging():
9384 "formatter" : "detailed" ,
9485 },
9586 }
96- base_config ["root" ] = {
97- "level" : "WARNING" ,
98- "handlers" : ["console" , "file" ]
99- }
100-
87+ base_config ["root" ] = {"level" : "WARNING" , "handlers" : ["console" , "file" ]}
88+
10189 dictConfig (base_config )
10290
10391
104- def initialize_firebase ():
92+ def initialize_firebase () -> None :
10593 """Initialize Firebase Admin SDK"""
10694 firebase_admin .initialize_app (
10795 firebase_admin .credentials .Certificate (
10896 {
10997 "type" : "service_account" ,
11098 "project_id" : settings .firebase_project_id ,
11199 "private_key_id" : settings .firebase_svc_account_private_key_id ,
112- "private_key" : settings .firebase_svc_account_private_key .replace ("\\ n" , "\n " ),
100+ "private_key" : settings .firebase_svc_account_private_key .replace (
101+ "\\ n" , "\n "
102+ ),
113103 "client_email" : settings .firebase_svc_account_client_email ,
114104 "client_id" : settings .firebase_svc_account_client_id ,
115105 "auth_uri" : settings .firebase_svc_account_auth_uri ,
@@ -122,23 +112,23 @@ def initialize_firebase():
122112
123113
124114@asynccontextmanager
125- async def lifespan (app : FastAPI ) -> AsyncGenerator [None , None ]:
115+ async def lifespan (_app : FastAPI ) -> AsyncGenerator [None , None ]:
126116 """Application lifespan management"""
127117 # Startup
128118 configure_logging ()
129119 initialize_firebase ()
130120 init_models ()
131-
121+
132122 yield
133-
123+
134124 # Shutdown
135125 # Add any cleanup code here if needed
136126 pass
137127
138128
139129def create_app () -> FastAPI :
140130 """Create and configure FastAPI application"""
141-
131+
142132 app = FastAPI (
143133 title = "Food4Kids API" ,
144134 description = "Backend API for the Food4Kids application" ,
@@ -151,11 +141,13 @@ def create_app() -> FastAPI:
151141 # Configure CORS
152142 cors_origins = settings .cors_origins .copy ()
153143 if settings .is_development :
154- cors_origins .extend ([
155- "http://localhost:3000" ,
156- "http://127.0.0.1:3000" ,
157- ])
158-
144+ cors_origins .extend (
145+ [
146+ "http://localhost:3000" ,
147+ "http://127.0.0.1:3000" ,
148+ ]
149+ )
150+
159151 # Add regex pattern for preview deployments
160152 cors_origins .append ("https://uw-blueprint-starter-code--pr.*\\ .web\\ .app" )
161153
0 commit comments