Skip to content

Commit

Permalink
Add maint mode
Browse files Browse the repository at this point in the history
  • Loading branch information
EvieePy committed May 6, 2024
1 parent e36f4fe commit 36cde63
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions config.template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ host = "localhost"
port = 8181
domain = "https://mystb.in"
session_secret = "" # Run: import secrets; print(secrets.token_urlsafe(64))
maintenance = false

[DATABASE]
dsn = "postgres://mystbin:mystbin@database:5432/mystbin"
Expand Down
14 changes: 13 additions & 1 deletion core/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import starlette_plus
from starlette.middleware import Middleware
from starlette.routing import Mount, Route
from starlette.schemas import SchemaGenerator
from starlette.staticfiles import StaticFiles

Expand All @@ -38,7 +39,7 @@ def __init__(self, *, database: Database) -> None:
self.schemas: SchemaGenerator | None = None

views: list[starlette_plus.View] = [HTMXView(self), APIView(self), DocsView(self)]
routes = [starlette_plus.Mount("/static", app=StaticFiles(directory="web/static"), name="static")]
routes: list[Mount | Route] = [Mount("/static", app=StaticFiles(directory="web/static"), name="static")]

limit_redis = starlette_plus.Redis(url=CONFIG["REDIS"]["limiter"]) if CONFIG["REDIS"]["limiter"] else None
sess_redis = starlette_plus.Redis(url=CONFIG["REDIS"]["sessions"]) if CONFIG["REDIS"]["sessions"] else None
Expand All @@ -59,8 +60,19 @@ def __init__(self, *, database: Database) -> None:
),
]

if CONFIG["SERVER"]["maintenance"]:
# inject a catch all before any route...
routes.insert(
0, Route("/{path:path}", self.maint_mode, methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"])
)
routes.insert(0, Route("/", self.maint_mode, methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]))

super().__init__(on_startup=[self.event_ready], views=views, routes=routes, middleware=middleware)

@staticmethod
async def maint_mode(request: starlette_plus.Request) -> starlette_plus.Response:
return starlette_plus.FileResponse("web/maint.html")

@starlette_plus.route("/docs")
@starlette_plus.route("/documentation")
async def documentation_redirect(self, request: starlette_plus.Request) -> starlette_plus.Response:
Expand Down
1 change: 1 addition & 0 deletions types_/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Server(TypedDict):
port: int
domain: str
session_secret: str
maintenance: bool


class Database(TypedDict):
Expand Down
67 changes: 67 additions & 0 deletions web/maint.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!DOCTYPE html>
<html lang="en" data-theme="dark">

<head>
<meta charset="UTF-8" />
<title>MystBin - Maintenance</title>

<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta name="description" content="Easily share code and text." />

<!-- PACKAGES -->
<!-- SCRIPTS -->
<script src="static/scripts/themes.js" defer></script>

<!-- STYLESHEETS -->
<link rel="preload" href="static/styles/global.css" as="style" />
<link rel="stylesheet" type="text/css" href="static/styles/global.css" />


<!-- FONTS -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,100;0,300;0,400;0,700;0,900;1,100;1,300;1,400;1,700;1,900&display=swap"
rel="stylesheet">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:ital,wght@0,100..800;1,100..800&family=Lato:ital,wght@0,100;0,300;0,400;0,700;0,900;1,100;1,300;1,400;1,700;1,900&display=swap"
rel="stylesheet">

<link rel="icon" href="static/images/favicon.png" />
</head>

<body>
<div class="header">
<a class="headerSection" href="/">
<img src="/static/images/logo.svg" class="logo" />
MystBin
</a>
<div>
<input id="themeSwitch" class="themeSwitch" type="checkbox" />
<label for="themeSwitch"></label>
</div>
</div>

<form id="content" class="content">
<h2>We are currently undergoing maintenance, be back soon!</h2>
</form>

<div class="footer">
<div class="footerSection">
<img src="/static/images/logo.svg" class="logo" />
<span class="footerText">
MystBin - Copyright © 2020-current PythonistaGuild
</span>
</div>
<div class="footerSection">
<a href="vscode:extension/PythonistaGuild.mystbin">Install on <img src="/static/images/vsc.svg" class="vsc" /></a>
<a href="/api/documentation">Documentation</a>
<a href="https://discord.gg/RAKc3HF">Discord</a>
<a href="https://github.com/PythonistaGuild">GitHub</a>
</div>
</div>
</body>

</html>

0 comments on commit 36cde63

Please sign in to comment.