Bot commited on
Commit
860ad93
·
1 Parent(s): 1f70d93

Deploy backend Phase 2

Browse files
Files changed (1) hide show
  1. main.py +29 -16
main.py CHANGED
@@ -11,6 +11,7 @@ Features:
11
  """
12
 
13
  import logging
 
14
  from fastapi import FastAPI
15
  from fastapi.middleware.cors import CORSMiddleware
16
  from routes import todos, auth
@@ -22,13 +23,28 @@ logging.basicConfig(
22
  )
23
  logger = logging.getLogger(__name__)
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  # Create FastAPI application instance
26
  app = FastAPI(
27
  title="Todo API",
28
  description="RESTful API for managing todo items with JWT authentication",
29
  version="1.0.0",
30
  docs_url="/docs",
31
- redoc_url="/redoc"
 
32
  )
33
 
34
 
@@ -57,21 +73,6 @@ app.include_router(auth.router)
57
  # Include todo routes (protected)
58
  app.include_router(todos.router)
59
 
60
- @app.on_event("startup")
61
- async def startup_event():
62
- """Application startup event handler"""
63
- logger.info("Todo API starting up with JWT authentication...")
64
- logger.info("Server running on http://localhost:8000")
65
- logger.info("Interactive API docs available at http://localhost:8000/docs")
66
- logger.info("API endpoints available at /api")
67
-
68
-
69
- @app.on_event("shutdown")
70
- async def shutdown_event():
71
- """Application shutdown event handler"""
72
- logger.info("Todo API shutting down...")
73
-
74
-
75
  @app.get("/health", tags=["health"], summary="Health check")
76
  async def health_check():
77
  """
@@ -84,3 +85,15 @@ async def health_check():
84
  to verify the API server is running and responsive.
85
  """
86
  return {"status": "healthy"}
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  """
12
 
13
  import logging
14
+ from contextlib import asynccontextmanager
15
  from fastapi import FastAPI
16
  from fastapi.middleware.cors import CORSMiddleware
17
  from routes import todos, auth
 
23
  )
24
  logger = logging.getLogger(__name__)
25
 
26
+
27
+ @asynccontextmanager
28
+ async def lifespan(app: FastAPI):
29
+ """Application lifespan event handler"""
30
+ # Startup
31
+ logger.info("Todo API starting up with JWT authentication...")
32
+ logger.info("Server running on http://localhost:8000")
33
+ logger.info("Interactive API docs available at http://localhost:8000/docs")
34
+ logger.info("API endpoints available at /api")
35
+ yield
36
+ # Shutdown
37
+ logger.info("Todo API shutting down...")
38
+
39
+
40
  # Create FastAPI application instance
41
  app = FastAPI(
42
  title="Todo API",
43
  description="RESTful API for managing todo items with JWT authentication",
44
  version="1.0.0",
45
  docs_url="/docs",
46
+ redoc_url="/redoc",
47
+ lifespan=lifespan
48
  )
49
 
50
 
 
73
  # Include todo routes (protected)
74
  app.include_router(todos.router)
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  @app.get("/health", tags=["health"], summary="Health check")
77
  async def health_check():
78
  """
 
85
  to verify the API server is running and responsive.
86
  """
87
  return {"status": "healthy"}
88
+
89
+
90
+ if __name__ == "__main__":
91
+ import uvicorn
92
+
93
+ uvicorn.run(
94
+ "main:app",
95
+ host="0.0.0.0",
96
+ port=8000,
97
+ reload=True,
98
+ log_level="info"
99
+ )