main.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """
  2. FastAPI application entry point.
  3. Provides RESTful API for the annotation platform with JWT authentication.
  4. """
  5. from fastapi import FastAPI
  6. from fastapi.middleware.cors import CORSMiddleware
  7. from contextlib import asynccontextmanager
  8. from database import init_database
  9. from routers import project, task, annotation, auth, oauth
  10. from middleware.auth_middleware import AuthMiddleware
  11. @asynccontextmanager
  12. async def lifespan(app: FastAPI):
  13. """
  14. Application lifespan manager.
  15. Initializes database on startup.
  16. """
  17. # Startup: Initialize database
  18. init_database()
  19. yield
  20. # Shutdown: cleanup if needed
  21. # Create FastAPI application instance
  22. app = FastAPI(
  23. title="Annotation Platform API",
  24. description="RESTful API for data annotation management with JWT authentication",
  25. version="1.0.0",
  26. lifespan=lifespan
  27. )
  28. # Configure CORS middleware (must be added first)
  29. app.add_middleware(
  30. CORSMiddleware,
  31. allow_origins=[
  32. "http://localhost:4200", # Frontend dev server
  33. "http://localhost:3000", # Alternative frontend port
  34. ],
  35. allow_credentials=True,
  36. allow_methods=["*"],
  37. allow_headers=["*"],
  38. )
  39. # Add authentication middleware (after CORS)
  40. app.add_middleware(AuthMiddleware)
  41. # Include routers
  42. app.include_router(auth.router)
  43. app.include_router(oauth.router)
  44. app.include_router(project.router)
  45. app.include_router(task.router)
  46. app.include_router(annotation.router)
  47. @app.get("/")
  48. async def root():
  49. """Root endpoint - health check"""
  50. return {
  51. "message": "Annotation Platform API",
  52. "status": "running",
  53. "version": "1.0.0"
  54. }
  55. @app.get("/health")
  56. async def health_check():
  57. """Health check endpoint"""
  58. return {"status": "healthy"}
  59. if __name__ == "__main__":
  60. import uvicorn
  61. uvicorn.run(app, host="0.0.0.0", port=8003)