Skip to content

Commit 36cde63

Browse files
committed
Add maint mode
1 parent e36f4fe commit 36cde63

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

config.template.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ host = "localhost"
33
port = 8181
44
domain = "https://mystb.in"
55
session_secret = "" # Run: import secrets; print(secrets.token_urlsafe(64))
6+
maintenance = false
67

78
[DATABASE]
89
dsn = "postgres://mystbin:mystbin@database:5432/mystbin"

core/server.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import starlette_plus
2222
from starlette.middleware import Middleware
23+
from starlette.routing import Mount, Route
2324
from starlette.schemas import SchemaGenerator
2425
from starlette.staticfiles import StaticFiles
2526

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

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

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

63+
if CONFIG["SERVER"]["maintenance"]:
64+
# inject a catch all before any route...
65+
routes.insert(
66+
0, Route("/{path:path}", self.maint_mode, methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"])
67+
)
68+
routes.insert(0, Route("/", self.maint_mode, methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]))
69+
6270
super().__init__(on_startup=[self.event_ready], views=views, routes=routes, middleware=middleware)
6371

72+
@staticmethod
73+
async def maint_mode(request: starlette_plus.Request) -> starlette_plus.Response:
74+
return starlette_plus.FileResponse("web/maint.html")
75+
6476
@starlette_plus.route("/docs")
6577
@starlette_plus.route("/documentation")
6678
async def documentation_redirect(self, request: starlette_plus.Request) -> starlette_plus.Response:

types_/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Server(TypedDict):
2626
port: int
2727
domain: str
2828
session_secret: str
29+
maintenance: bool
2930

3031

3132
class Database(TypedDict):

web/maint.html

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!DOCTYPE html>
2+
<html lang="en" data-theme="dark">
3+
4+
<head>
5+
<meta charset="UTF-8" />
6+
<title>MystBin - Maintenance</title>
7+
8+
<meta name="viewport" content="width=device-width,initial-scale=1" />
9+
<meta name="description" content="Easily share code and text." />
10+
11+
<!-- PACKAGES -->
12+
<!-- SCRIPTS -->
13+
<script src="static/scripts/themes.js" defer></script>
14+
15+
<!-- STYLESHEETS -->
16+
<link rel="preload" href="static/styles/global.css" as="style" />
17+
<link rel="stylesheet" type="text/css" href="static/styles/global.css" />
18+
19+
20+
<!-- FONTS -->
21+
<link rel="preconnect" href="https://fonts.googleapis.com">
22+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
23+
<link
24+
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"
25+
rel="stylesheet">
26+
<link rel="preconnect" href="https://fonts.googleapis.com">
27+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
28+
<link
29+
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"
30+
rel="stylesheet">
31+
32+
<link rel="icon" href="static/images/favicon.png" />
33+
</head>
34+
35+
<body>
36+
<div class="header">
37+
<a class="headerSection" href="/">
38+
<img src="/static/images/logo.svg" class="logo" />
39+
MystBin
40+
</a>
41+
<div>
42+
<input id="themeSwitch" class="themeSwitch" type="checkbox" />
43+
<label for="themeSwitch"></label>
44+
</div>
45+
</div>
46+
47+
<form id="content" class="content">
48+
<h2>We are currently undergoing maintenance, be back soon!</h2>
49+
</form>
50+
51+
<div class="footer">
52+
<div class="footerSection">
53+
<img src="/static/images/logo.svg" class="logo" />
54+
<span class="footerText">
55+
MystBin - Copyright © 2020-current PythonistaGuild
56+
</span>
57+
</div>
58+
<div class="footerSection">
59+
<a href="vscode:extension/PythonistaGuild.mystbin">Install on <img src="/static/images/vsc.svg" class="vsc" /></a>
60+
<a href="/api/documentation">Documentation</a>
61+
<a href="https://discord.gg/RAKc3HF">Discord</a>
62+
<a href="https://github.com/PythonistaGuild">GitHub</a>
63+
</div>
64+
</div>
65+
</body>
66+
67+
</html>

0 commit comments

Comments
 (0)