-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
53 lines (43 loc) · 1.62 KB
/
main.py
File metadata and controls
53 lines (43 loc) · 1.62 KB
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
"""
An API interface that provide two functionalities
(1) Accent Marker (/api/MarkAccent/)
(2) Furigana Marker (/api/MarkFurigana/)
(3) Usage Query (/api/UsageQuery/)
(4) Dictionary Query (/api/DictQuery/)
(5) Sentence Query (/api/SentenceQuery/)
"""
import logging
from contextlib import asynccontextmanager
from typing import AsyncGenerator
import httpx
from fastapi import FastAPI
from api import dict_query, sentence_query, usage_query
from api.accent import accent_router, furigana_router
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
"""
Lifespan context manager for FastAPI application.
This function is used to manage the lifespan of the FastAPI application.
It can be used to set up resources before the application starts and
clean up resources after the application stops.
Args:
app (FastAPI): The FastAPI application instance.
"""
# Set up resources before the application starts
app.state.http_client = httpx.AsyncClient(timeout=10.0)
yield
# Clean up resources after the application stops
await app.state.http_client.aclose()
app = FastAPI(lifespan=lifespan)
# Include routers from different modules
app.include_router(accent_router, prefix="/api")
app.include_router(furigana_router, prefix="/api")
app.include_router(usage_query.router, prefix="/api")
app.include_router(dict_query.router, prefix="/api")
app.include_router(sentence_query.router, prefix="/api")
logging.basicConfig(
level=logging.INFO,
format="{asctime} [{levelname:^8s}] {message} ({name}.{module}:{lineno})",
datefmt="%Y-%m-%d %H:%M:%S",
style="{",
)