api/base.py

107 lines
3.1 KiB
Python
Raw Normal View History

2024-08-10 22:49:00 -04:00
import importlib
2025-01-24 19:26:07 -05:00
import sys
sys.path.insert(0, ".")
2024-08-10 22:49:00 -04:00
import logging
2024-10-02 20:54:34 -04:00
import asyncio
2024-08-11 13:55:11 -04:00
from typing import Any
from fastapi import FastAPI, Request
2024-08-10 22:49:00 -04:00
from fastapi.middleware.cors import CORSMiddleware
from lyric_search.sources import redis_cache
2024-10-02 20:54:34 -04:00
2024-08-10 22:49:00 -04:00
2025-02-18 06:55:47 -05:00
2024-08-10 22:49:00 -04:00
logger = logging.getLogger()
2025-01-12 08:08:06 -05:00
logger.setLevel(logging.INFO)
2024-08-10 22:49:00 -04:00
2024-10-02 20:54:34 -04:00
loop = asyncio.get_event_loop()
2024-08-15 20:29:41 -04:00
app = FastAPI(title="codey.lol API",
2025-02-05 20:23:06 -05:00
version="1.0",
2024-08-15 20:29:41 -04:00
contact={
'name': 'codey'
2024-10-02 20:54:34 -04:00
},
2025-02-05 20:23:06 -05:00
redirect_slashes=False,
2024-10-02 20:54:34 -04:00
loop=loop)
2025-02-18 06:55:47 -05:00
2024-08-10 22:49:00 -04:00
constants = importlib.import_module("constants").Constants()
2024-08-14 22:43:20 -04:00
util = importlib.import_module("util").Utilities(app, constants)
2024-08-10 22:49:00 -04:00
origins = [
"https://codey.lol",
2024-11-29 15:33:12 -05:00
"https://api.codey.lol"
2024-08-10 22:49:00 -04:00
]
2025-02-18 06:55:47 -05:00
app.add_middleware(CORSMiddleware, # type: ignore
2024-08-10 22:49:00 -04:00
allow_origins=origins,
allow_credentials=True,
allow_methods=["POST", "GET", "HEAD"],
2025-02-18 06:55:47 -05:00
allow_headers=["*"]) # type: ignore
2024-08-10 22:49:00 -04:00
"""
Blacklisted routes
"""
2025-01-29 16:03:33 -05:00
@app.get("/", include_in_schema=False)
2024-08-10 22:49:00 -04:00
def disallow_get():
return util.get_blocked_response()
2025-01-29 16:03:33 -05:00
@app.head("/", include_in_schema=False)
2025-01-23 13:02:03 -05:00
def base_head():
return
2025-01-29 16:03:33 -05:00
@app.get("/{path}", include_in_schema=False)
2025-02-16 08:50:53 -05:00
def disallow_get_any(request: Request, var: Any = None):
path = request.path_params['path']
if not (
isinstance(path, str)
and
path.split("/", maxsplit=1) == "widget"
):
return util.get_blocked_response()
else:
logging.info("OK, %s",
path)
2024-08-10 22:49:00 -04:00
2025-01-29 16:03:33 -05:00
@app.post("/", include_in_schema=False)
2024-08-10 22:49:00 -04:00
def disallow_base_post():
return util.get_blocked_response()
"""
End Blacklisted Routes
"""
"""
Actionable Routes
"""
2025-02-05 20:23:06 -05:00
2025-02-14 16:07:24 -05:00
randmsg_endpoint = importlib.import_module("endpoints.rand_msg").RandMsg(app, util, constants)
transcription_endpoints = importlib.import_module("endpoints.transcriptions").Transcriptions(app, util, constants)
ai_endpoints = importlib.import_module("endpoints.ai").AI(app, util, constants)
2024-08-11 17:04:06 -04:00
# Below also provides: /lyric_cache_list/ (in addition to /lyric_search/)
2025-02-14 16:07:24 -05:00
lyric_search_endpoint = importlib.import_module("endpoints.lyric_search").LyricSearch(app, util, constants)
2024-08-11 17:28:43 -04:00
# Below provides numerous LastFM-fed endpoints
2025-02-14 16:07:24 -05:00
lastfm_endpoints = importlib.import_module("endpoints.lastfm").LastFM(app, util, constants)
2024-08-13 10:50:11 -04:00
# Below: YT endpoint(s)
2025-02-14 16:07:24 -05:00
yt_endpoints = importlib.import_module("endpoints.yt").YT(app, util, constants)
2024-08-19 11:42:23 -04:00
# Below: XC endpoint(s)
2025-02-14 16:07:24 -05:00
xc_endpoints = importlib.import_module("endpoints.xc").XC(app, util, constants)
2024-11-14 14:37:32 -05:00
# Below: Karma endpoint(s)
2025-02-14 16:07:24 -05:00
karma_endpoints = importlib.import_module("endpoints.karma").Karma(app, util, constants)
2025-01-30 19:33:04 -05:00
# Below: Radio endpoint(s) - in development, sporadically loaded as needed
2025-02-14 16:07:24 -05:00
radio_endpoints = importlib.import_module("endpoints.radio").Radio(app, util, constants)
# Below: Misc endpoints
2025-02-14 16:07:24 -05:00
misc_endpoints = importlib.import_module("endpoints.misc").Misc(app, util, constants, radio_endpoints)
2025-02-18 06:55:47 -05:00
# Below: Mgr Endpoints
mgr_endpoints = importlib.import_module("endpoints.mgr.mgr_test").Mgr(app, util, constants)
2024-08-13 19:21:48 -04:00
2025-01-19 07:01:07 -05:00
"""
End Actionable Routes
"""
2024-08-10 22:49:00 -04:00
2024-08-11 12:46:24 -04:00
2024-08-10 22:49:00 -04:00
"""
2025-01-19 07:01:07 -05:00
Startup
"""
2025-02-14 16:07:24 -05:00
redis = redis_cache.RedisCache()
2025-01-19 07:01:07 -05:00
asyncio.get_event_loop().create_task(
2025-02-14 16:07:24 -05:00
redis.create_index())