api/base.py

103 lines
2.8 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
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-18 13:59:17 -05:00
routes: dict = {
'randmsg': importlib.import_module("endpoints.rand_msg").RandMsg(app, util, constants),
'transcriptions': importlib.import_module("endpoints.transcriptions").Transcriptions(app, util, constants),
'ai': importlib.import_module("endpoints.ai").AI(app, util, constants),
'lyrics': importlib.import_module("endpoints.lyric_search").LyricSearch(app, util, constants),
'lastfm': importlib.import_module("endpoints.lastfm").LastFM(app, util, constants),
'yt': importlib.import_module("endpoints.yt").YT(app, util, constants),
'xc': importlib.import_module("endpoints.xc").XC(app, util, constants),
'karma': importlib.import_module("endpoints.karma").Karma(app, util, constants),
'radio': importlib.import_module("endpoints.radio").Radio(app, util, constants),
'mgr': importlib.import_module("endpoints.mgr.mgr_test").Mgr(app, util, constants),
}
# Misc endpoint depends on radio endpoint instance
radio_endpoint = routes.get('radio')
if radio_endpoint:
routes['misc'] = importlib.import_module("endpoints.misc").Misc(app, util, constants, radio_endpoint)
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-02-18 13:59:17 -05:00
loop.create_task(
2025-02-14 16:07:24 -05:00
redis.create_index())