-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
203 lines (170 loc) · 7.61 KB
/
main.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from config import env_result
import uvicorn
import os
import json
from redis import asyncio as aioredis
from fastapi import FastAPI, APIRouter, Request
from fastapi.responses import RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from starlette.responses import PlainTextResponse
from apitally.fastapi import ApitallyMiddleware
from datetime import datetime
from contextlib import asynccontextmanager
from routers import (enka_network, metadata, patch_next, static, net, wallpaper, strategy, crowdin, system_email,
client_feature, mgnt)
from base_logger import logger
from config import (MAIN_SERVER_DESCRIPTION, TOS_URL, CONTACT_INFO, LICENSE_INFO, VALID_PROJECT_KEYS, IMAGE_NAME, DEBUG)
from mysql_app.database import SessionLocal
from utils.redis_tools import init_redis_data
@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info("enter lifespan")
# Create cache folder
os.makedirs("cache", exist_ok=True)
# Redis connection
redis_host = os.getenv("REDIS_HOST", "redis")
redis_pool = aioredis.ConnectionPool.from_url(f"redis://{redis_host}", db=0)
app.state.redis = redis_pool
redis_client = aioredis.Redis.from_pool(connection_pool=redis_pool)
logger.info("Redis connection established")
# MySQL connection
app.state.mysql = SessionLocal()
# Patch module lifespan
try:
redis_cached_version = await redis_client.get("snap-hutao:version")
redis_cached_version = redis_cached_version.decode("utf-8")
logger.info(f"Got mirrors from Redis: {redis_cached_version}")
except (TypeError, AttributeError):
for key in VALID_PROJECT_KEYS:
r = await redis_client.set(f"{key}:version", json.dumps({"version": None}))
logger.info(f"Set [{key}:mirrors] to Redis: {r}")
# Initial patch metadata
from routers.patch_next import (update_snap_hutao_latest_version, update_snap_hutao_deployment_version,
fetch_snap_hutao_alpha_latest_version)
await update_snap_hutao_latest_version(redis_client)
await update_snap_hutao_deployment_version(redis_client)
await fetch_snap_hutao_alpha_latest_version(redis_client)
# Initial Redis data
await init_redis_data(redis_client)
logger.info("ending lifespan startup")
yield
logger.info("entering lifespan shutdown")
def get_version():
if os.path.exists("build_number.txt"):
with open("build_number.txt", 'r') as f:
build_number = f"Build {f.read().strip()}"
logger.info(f"Server is running with Build number: {build_number}")
else:
build_number = f"Runtime {datetime.now().strftime('%Y.%m.%d.%H%M%S')}"
logger.info(f"Server is running with Runtime version: {build_number}")
if DEBUG:
build_number += " DEBUG"
if os.path.exists("current_commit.txt"):
with open("current_commit.txt", 'r') as f:
commit_hash = f.read().strip()
build_number += f" {commit_hash[:7]}"
return build_number
def get_commit_hash_str():
commit_desc = ""
if os.path.exists("current_commit.txt"):
with open("current_commit.txt", 'r') as f:
commit_hash = f.read().strip()
logger.info(f"Server is running with Commit hash: {commit_hash}")
commit_desc = f"Build hash: [**{commit_hash}**](https://github.com/DGP-Studio/Generic-API/commit/{commit_hash})"
if DEBUG:
commit_desc += "\n\n**Debug mode is enabled.**"
commit_desc += "\n\n![Image](https://github.com/user-attachments/assets/64ce064c-c399-4d2f-ac72-cac4379d8725)"
return commit_desc
app = FastAPI(redoc_url=None,
title="Hutao Generic API",
summary="Generic API to support various services for Snap Hutao project.",
version=get_version(),
description=MAIN_SERVER_DESCRIPTION + "\n" + get_commit_hash_str(),
terms_of_service=TOS_URL,
contact=CONTACT_INFO,
license_info=LICENSE_INFO,
openapi_url="/openapi.json",
lifespan=lifespan,
debug=DEBUG)
china_root_router = APIRouter(tags=["China Router"], prefix="/cn")
global_root_router = APIRouter(tags=["Global Router"], prefix="/global")
fujian_root_router = APIRouter(tags=["Fujian Router"], prefix="/fj")
# Enka Network API Routers
china_root_router.include_router(enka_network.china_router)
global_root_router.include_router(enka_network.global_router)
fujian_root_router.include_router(enka_network.fujian_router)
# Hutao Metadata API Routers
china_root_router.include_router(metadata.china_router)
global_root_router.include_router(metadata.global_router)
fujian_root_router.include_router(metadata.fujian_router)
# Patch API Routers
china_root_router.include_router(patch_next.china_router)
global_root_router.include_router(patch_next.global_router)
fujian_root_router.include_router(patch_next.fujian_router)
# Static API Routers
china_root_router.include_router(static.china_router)
global_root_router.include_router(static.global_router)
fujian_root_router.include_router(static.fujian_router)
# Network API Routers
china_root_router.include_router(net.china_router)
global_root_router.include_router(net.global_router)
fujian_root_router.include_router(net.fujian_router)
# Wallpaper API Routers
china_root_router.include_router(wallpaper.china_router)
global_root_router.include_router(wallpaper.global_router)
fujian_root_router.include_router(wallpaper.fujian_router)
# Strategy API Routers
china_root_router.include_router(strategy.china_router)
global_root_router.include_router(strategy.global_router)
fujian_root_router.include_router(strategy.fujian_router)
# Crowdin Localization API Routers
china_root_router.include_router(crowdin.china_router)
global_root_router.include_router(crowdin.global_router)
fujian_root_router.include_router(crowdin.fujian_router)
# Client feature routers
china_root_router.include_router(client_feature.china_router)
global_root_router.include_router(client_feature.global_router)
fujian_root_router.include_router(client_feature.fujian_router)
app.include_router(china_root_router)
app.include_router(global_root_router)
app.include_router(fujian_root_router)
# Misc
app.include_router(system_email.admin_router)
app.include_router(mgnt.router)
origins = [
"http://localhost",
"http://localhost:8080",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
if IMAGE_NAME != "" and "dev" not in os.getenv("IMAGE_NAME"):
app.add_middleware(
ApitallyMiddleware,
client_id=os.getenv("APITALLY_CLIENT_ID"),
env="dev" if "alpha" in IMAGE_NAME else "prod",
openapi_url="/openapi.json"
)
else:
logger.info("Apitally is disabled as the image is not a production image.")
@app.get("/", response_class=RedirectResponse, status_code=301)
@china_root_router.get("/", response_class=RedirectResponse, status_code=301)
@global_root_router.get("/", response_class=RedirectResponse, status_code=301)
@fujian_root_router.get("/", response_class=RedirectResponse, status_code=301)
async def root():
return "https://hut.ao"
@app.get("/error")
@china_root_router.get("/error")
@global_root_router.get("/error")
@fujian_root_router.get("/error")
async def get_sample_error():
raise RuntimeError(
"This is endpoint for debug purpose; you should receive a Runtime error with this message in debug mode, else you will only see a 500 error")
if __name__ == "__main__":
if env_result:
logger.info(".env file is loaded")
uvicorn.run(app, host="0.0.0.0", port=8080, proxy_headers=True, forwarded_allow_ips="*")