api/base.py

110 lines
2.6 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
2025-01-24 19:26:07 -05:00
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()
app = FastAPI(
title="codey.lol API",
version="1.0",
contact={"name": "codey"},
redirect_slashes=False,
loop=loop,
)
2024-10-02 20:54:34 -04:00
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)
origins = ["https://codey.lol", "https://api.codey.lol"]
2024-08-10 22:49:00 -04:00
app.add_middleware(
CORSMiddleware, # type: ignore
allow_origins=origins,
allow_credentials=True,
allow_methods=["POST", "GET", "HEAD"],
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()
2024-08-10 22:49:00 -04:00
"""
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),
"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),
"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),
2025-02-18 13:59:17 -05:00
}
# Misc endpoint depends on radio endpoint instance
radio_endpoint = routes.get("radio")
2025-02-18 13:59:17 -05:00
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-03-19 11:18:44 -04:00
2025-02-14 16:07:24 -05:00
redis = redis_cache.RedisCache()
loop.create_task(redis.create_index())