File size: 2,064 Bytes
1f3b631
 
 
 
 
 
 
c025f35
1f3b631
 
 
 
c025f35
 
 
 
 
 
 
 
 
8a39773
 
 
 
 
c025f35
 
 
8a39773
c025f35
 
 
 
 
 
 
8a39773
e568e66
c025f35
 
 
 
 
 
e568e66
 
 
 
 
 
 
 
 
 
8a39773
c025f35
8a39773
c025f35
 
 
8a39773
c025f35
 
 
8a39773
c025f35
 
e568e66
c025f35
 
 
 
 
 
 
 
e568e66
c025f35
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
from loguru import logger
import sys

from config import settings
from routes import word, health, study
from services.word_service import WordEmbeddingService
from services.visualization_service import VisualizationService
from services.study_service import StudyService

# Configure logger
logger.remove()
logger.add(
    sys.stdout,
    format="{time} | {level} | {message}",
    level="INFO",
    serialize=False
)

# Initialize services first
word_service = WordEmbeddingService(settings.model_url)
visualization_service = VisualizationService(word_service)
study_service = StudyService(word_service)

@asynccontextmanager
async def lifespan(app: FastAPI):
    # Startup
    logger.info("Starting up...")
    try:
        yield
    except Exception as e:
        logger.error(f"Error during startup: {str(e)}")
        raise
    finally:
        # Cleanup
        logger.info("Shutting down...")

# Initialize FastAPI app
app = FastAPI(
    title=settings.app_name,
    version=settings.version,
    lifespan=lifespan
)

# Configure CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Include routers with direct service injection
app.include_router(
    word.init_router(word_service),
    tags=["word operations"]
)
app.include_router(
    study.init_router(study_service),
    tags=["study operations"]
)
app.include_router(
    health.init_router(word_service),
    tags=["health"]
)

@app.get("/")
async def root():
    """Root endpoint with API information"""
    return {
        "app_name": settings.app_name,
        "version": settings.version,
        "status": "running"
    }

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=8000,
        reload=settings.debug
    )