Skip to content

Commit

Permalink
lint the fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmTomahawkx committed Oct 5, 2023
1 parent bd306ab commit 7579215
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions mystbin/fallback/fallback.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import os
import json
import os

from aiohttp import web


app = web.Application()
app.config = {}
router = web.RouteTableDef()


def load_config():
if os.path.exists("./config.json"):
with open("./config.json", "r") as __f:
Expand All @@ -14,21 +17,24 @@ def load_config():
elif os.path.exists("../../config.json"):
with open("../../config.json", "r") as __f:
config = json.load(__f)

else:
raise RuntimeError("Cannot find config file")

app.config = config


load_config()


@router.post("/reload")
async def router_reload(request: web.Request) -> web.Response:
if request.remote == "127.0.0.1" and "X-Forwarded-For" not in request.headers: # systemd reloads will only ever come from localhost using curl
if (
request.remote == "127.0.0.1" and "X-Forwarded-For" not in request.headers
): # systemd reloads will only ever come from localhost using curl
load_config()
return web.Response(body="Reloaded", status=200)

raise web.HTTPPermanentRedirect("/")


Expand All @@ -39,11 +45,12 @@ async def router_reload(request: web.Request) -> web.Response:
@router.patch("/")
@router.patch("/{pth:.*}")
async def fallback_route(request: web.Request) -> web.Response | web.FileResponse:
if app.config.get("maintenance", False): # type: ignore
if app.config.get("maintenance", False): # type: ignore
return web.FileResponse("maintenance.html")

return web.FileResponse("service_failure.html")


app.add_routes(router)

web.run_app(app, port=app.config["site"]["fallback_port"]) # type: ignore
web.run_app(app, port=app.config["site"]["fallback_port"]) # type: ignore

0 comments on commit 7579215

Please sign in to comment.