Distopia22 commited on
Commit
f2ce243
·
1 Parent(s): ccd9b6a

deprecated on event error

Browse files
Files changed (1) hide show
  1. src/main.py +31 -17
src/main.py CHANGED
@@ -1,4 +1,5 @@
1
  import logging
 
2
  from fastapi import FastAPI
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from api.routes import router
@@ -12,11 +13,38 @@ logging.basicConfig(
12
 
13
  logger = logging.getLogger(__name__)
14
 
15
- # Create FastAPI app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  app = FastAPI(
17
  title=settings.api_title,
18
  version=settings.api_version,
19
- description=settings.api_description
 
20
  )
21
 
22
  # CORS configuration
@@ -31,21 +59,6 @@ app.add_middleware(
31
  # Include routes
32
  app.include_router(router, prefix="/api")
33
 
34
- @app.on_event("startup")
35
- async def startup_event():
36
- """Log startup information"""
37
- logger.info("=" * 50)
38
- logger.info(f"🚀 {settings.api_title} v{settings.api_version}")
39
- logger.info("=" * 50)
40
-
41
- if settings.groq_api_key:
42
- logger.info(f"✅ Groq API Key: Configured (length: {len(settings.groq_api_key)})")
43
- logger.info(f"✅ Groq Model: {settings.groq_model}")
44
- else:
45
- logger.error("❌ GROQ_API_KEY is NOT set!")
46
- logger.error("⚠️ API will NOT function without valid API key")
47
-
48
- logger.info("=" * 50)
49
 
50
  @app.get("/")
51
  async def root():
@@ -62,6 +75,7 @@ async def root():
62
  }
63
  }
64
 
 
65
  @app.get("/api/v1/health")
66
  async def health_check():
67
  """Health check endpoint"""
 
1
  import logging
2
+ from contextlib import asynccontextmanager
3
  from fastapi import FastAPI
4
  from fastapi.middleware.cors import CORSMiddleware
5
  from api.routes import router
 
13
 
14
  logger = logging.getLogger(__name__)
15
 
16
+
17
+ @asynccontextmanager
18
+ async def lifespan(app: FastAPI):
19
+ """
20
+ Lifespan event handler for startup and shutdown
21
+ """
22
+ # Startup
23
+ logger.info("=" * 50)
24
+ logger.info(f"🚀 {settings.api_title} v{settings.api_version}")
25
+ logger.info("=" * 50)
26
+
27
+ if settings.groq_api_key:
28
+ logger.info(f"✅ Groq API Key: Configured (length: {len(settings.groq_api_key)})")
29
+ logger.info(f"✅ Groq Model: {settings.groq_model}")
30
+ else:
31
+ logger.error("❌ GROQ_API_KEY is NOT set!")
32
+ logger.error("⚠️ API will NOT function without valid API key")
33
+
34
+ logger.info("=" * 50)
35
+
36
+ yield
37
+
38
+ # Shutdown
39
+ logger.info("👋 Shutting down API...")
40
+
41
+
42
+ # Create FastAPI app with lifespan
43
  app = FastAPI(
44
  title=settings.api_title,
45
  version=settings.api_version,
46
+ description=settings.api_description,
47
+ lifespan=lifespan
48
  )
49
 
50
  # CORS configuration
 
59
  # Include routes
60
  app.include_router(router, prefix="/api")
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  @app.get("/")
64
  async def root():
 
75
  }
76
  }
77
 
78
+
79
  @app.get("/api/v1/health")
80
  async def health_check():
81
  """Health check endpoint"""