|
|
|
@ -1,4 +1,7 @@
|
|
|
|
|
import sys
|
|
|
|
|
import traceback
|
|
|
|
|
from config import Config
|
|
|
|
|
|
|
|
|
|
Config.load_config("config.toml")
|
|
|
|
|
|
|
|
|
|
from local import loop, noawait
|
|
|
|
@ -21,8 +24,46 @@ from api.model.embedding_search.title_index import TitleIndexModel as _
|
|
|
|
|
|
|
|
|
|
from service.tiktoken import TikTokenService
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def index(request: web.Request):
|
|
|
|
|
return await utils.web.api_response(1, data={"message": "Isekai toolkit API"}, request=request)
|
|
|
|
|
return await utils.web.api_response(
|
|
|
|
|
1, data={"message": "Isekai toolkit API"}, request=request
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@web.middleware
|
|
|
|
|
async def error_handler(request, handler):
|
|
|
|
|
try:
|
|
|
|
|
response = await handler(request)
|
|
|
|
|
return response
|
|
|
|
|
except utils.web.ParamInvalidException as ex:
|
|
|
|
|
return await utils.web.api_response(
|
|
|
|
|
-1,
|
|
|
|
|
error={"code": "invalid-params", "message": "Invalid params.", "invalid_params": ex.param_list},
|
|
|
|
|
http_status=400,
|
|
|
|
|
request=request,
|
|
|
|
|
)
|
|
|
|
|
except web.HTTPException as ex:
|
|
|
|
|
return await utils.web.api_response(
|
|
|
|
|
-1,
|
|
|
|
|
error={"code": f"http_{ex.status}", "message": ex.reason},
|
|
|
|
|
http_status=ex.status,
|
|
|
|
|
request=request,
|
|
|
|
|
)
|
|
|
|
|
except Exception as ex:
|
|
|
|
|
error_id = utils.web.generate_uuid()
|
|
|
|
|
err_msg = f"Server error [{error_id}]: {ex}"
|
|
|
|
|
print(err_msg, file=sys.stderr)
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
print(f"End of error [{error_id}]", file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
return await utils.web.api_response(
|
|
|
|
|
-1,
|
|
|
|
|
error={"code": "internal-server-error", "message": err_msg},
|
|
|
|
|
http_status=500,
|
|
|
|
|
request=request,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def init_mw_api(app: web.Application):
|
|
|
|
|
mw_api = MediaWikiApi.create()
|
|
|
|
@ -38,7 +79,11 @@ async def init_mw_api(app: web.Application):
|
|
|
|
|
|
|
|
|
|
site_meta = await mw_api.get_site_meta()
|
|
|
|
|
|
|
|
|
|
print("Connected to Wiki %s, Robot username: %s" % (site_meta["sitename"], site_meta["user"]))
|
|
|
|
|
print(
|
|
|
|
|
"Connected to Wiki %s, Robot username: %s"
|
|
|
|
|
% (site_meta["sitename"], site_meta["user"])
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def init_database(app: web.Application):
|
|
|
|
|
dbs = await DatabaseService.create(app)
|
|
|
|
@ -47,20 +92,30 @@ async def init_database(app: web.Application):
|
|
|
|
|
async with dbs.engine.begin() as conn:
|
|
|
|
|
await conn.run_sync(BaseModel.metadata.create_all)
|
|
|
|
|
|
|
|
|
|
async def close_database(app: web.Application):
|
|
|
|
|
dbs = await DatabaseService.create(app)
|
|
|
|
|
await dbs.close()
|
|
|
|
|
|
|
|
|
|
async def init_tiktoken(app: web.Application):
|
|
|
|
|
await TikTokenService.create()
|
|
|
|
|
print("Tiktoken model loaded.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def stop_noawait_pool(app: web.Application):
|
|
|
|
|
await noawait.end()
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
local.debug = Config.get("server.debug", False, bool)
|
|
|
|
|
|
|
|
|
|
app = web.Application()
|
|
|
|
|
app = web.Application(
|
|
|
|
|
middlewares=[
|
|
|
|
|
error_handler,
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if Config.get("database.host"):
|
|
|
|
|
app.on_startup.append(init_database)
|
|
|
|
|
app.on_cleanup.append(close_database)
|
|
|
|
|
|
|
|
|
|
if Config.get("mediawiki.api_endpoint"):
|
|
|
|
|
app.on_startup.append(init_mw_api)
|
|
|
|
@ -70,9 +125,14 @@ if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
app.on_shutdown.append(stop_noawait_pool)
|
|
|
|
|
|
|
|
|
|
app.router.add_route('*', '/', index)
|
|
|
|
|
app.router.add_route("*", "/", index)
|
|
|
|
|
api.route.init(app)
|
|
|
|
|
|
|
|
|
|
server_port = Config.get("port", 8144, int)
|
|
|
|
|
|
|
|
|
|
web.run_app(app, host='0.0.0.0', port=server_port, loop=loop)
|
|
|
|
|
web.run_app(
|
|
|
|
|
app,
|
|
|
|
|
host="0.0.0.0",
|
|
|
|
|
port=server_port,
|
|
|
|
|
loop=loop,
|
|
|
|
|
)
|
|
|
|
|