http: Add the HTTP_ROOT config option and a function that uses it
HTTP_ROOT is the server's root directory to serve from, in case your domain uses something else. This is also added to the HTML templates. Also added a news.html template for any sitewide news. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -27,3 +27,4 @@ THUMBS_DIR = Path(default("THUMBS_DIR", "thumbs"))
|
||||
CACHE_DIR = Path(default("CACHE_DIR", "cache"))
|
||||
|
||||
HTTP_DOMAIN = required("HTTP_DOMAIN")
|
||||
HTTP_ROOT = default("HTTP_ROOT", r"/")
|
||||
|
||||
@@ -6,15 +6,33 @@ from typing import Any, MutableMapping, Optional, Sequence
|
||||
from aiohttp import web
|
||||
from jinja2 import Environment, PackageLoader, select_autoescape
|
||||
|
||||
from . import config
|
||||
from .db import get_db, search_db
|
||||
|
||||
# 2023-07-24 - eating stirfry tonight. It's pretty bad.
|
||||
# 2023-07-31 - In a meeting. Hope I don't get caught
|
||||
|
||||
|
||||
def route_url(url: str):
|
||||
if url in ("/" or ""):
|
||||
return config.HTTP_ROOT
|
||||
elif config.HTTP_ROOT in ("/" or ""):
|
||||
return url
|
||||
elif url.startswith("/"):
|
||||
return f"{config.HTTP_ROOT}{url}"
|
||||
else:
|
||||
return f"{config.HTTP_ROOT}/{url}"
|
||||
|
||||
|
||||
_env = Environment(
|
||||
loader=PackageLoader("chanbans"),
|
||||
autoescape=select_autoescape(),
|
||||
)
|
||||
_env.globals.update(
|
||||
{
|
||||
"route_url": route_url,
|
||||
}
|
||||
)
|
||||
|
||||
TemplateContext = MutableMapping[str, Any]
|
||||
|
||||
@@ -49,7 +67,9 @@ class TemplateView(web.View):
|
||||
|
||||
def get_context(self) -> TemplateContext:
|
||||
"Gets the context for this template"
|
||||
return dict()
|
||||
return {
|
||||
"config": config,
|
||||
}
|
||||
|
||||
def template_render(self) -> str:
|
||||
"Renders the template on this object."
|
||||
@@ -131,31 +151,6 @@ class IndexView(TemplateView):
|
||||
return HtmlResponse(text=html)
|
||||
|
||||
|
||||
################################################################################
|
||||
# API views
|
||||
################################################################################
|
||||
|
||||
|
||||
class BansJson(web.View):
|
||||
async def get(self):
|
||||
db = get_db()
|
||||
page = self.request.match_info["page"]
|
||||
|
||||
# TODO - configure the number of bans per page in settings
|
||||
BANS_PER_PAGE = 10
|
||||
|
||||
result = db.execute(
|
||||
"""
|
||||
SELECT *
|
||||
FROM bans
|
||||
ORDER BY id
|
||||
DESC LIMIT :limit
|
||||
""",
|
||||
{"limit": BANS_PER_PAGE},
|
||||
)
|
||||
return web.json_response(list(result.fetchall()))
|
||||
|
||||
|
||||
################################################################################
|
||||
# Routes
|
||||
################################################################################
|
||||
@@ -164,11 +159,12 @@ class BansJson(web.View):
|
||||
app = web.Application()
|
||||
app.add_routes(
|
||||
[
|
||||
web.get(r"/bans", IndexView),
|
||||
web.get(r"/bans/faq", template_view_factory("faq.html")),
|
||||
#web.get(r"/api/v1/bans/{page:\d+}", BansJson),
|
||||
web.get(route_url(r"/"), IndexView),
|
||||
web.get(route_url(r"/faq"), template_view_factory("faq.html")),
|
||||
web.get(route_url(r"/news"), template_view_factory("news.html")),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def run_app():
|
||||
web.run_app(app)
|
||||
|
||||
@@ -4,8 +4,10 @@
|
||||
{% block head %}
|
||||
{% endblock head %}
|
||||
<title>
|
||||
4chan bans archive -
|
||||
{% block title %}
|
||||
{% endblock title %}
|
||||
{% endblock title %} -
|
||||
{{ config.HTTP_DOMAIN }}
|
||||
</title>
|
||||
<style>
|
||||
|
||||
@@ -50,7 +52,7 @@ header {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
#bans-table {
|
||||
table {
|
||||
border-top: 1px solid black;
|
||||
border-left: 1px solid black;
|
||||
border-right: 1px solid black;
|
||||
@@ -62,13 +64,13 @@ header {
|
||||
color: black;
|
||||
}
|
||||
|
||||
#bans-table td {
|
||||
table td {
|
||||
border-left: 1px solid black;
|
||||
padding: 5px;
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
#bans-table th {
|
||||
table th {
|
||||
width: auto;
|
||||
border-left: 1px solid black;
|
||||
background-color: #fca;
|
||||
@@ -77,7 +79,7 @@ header {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#bans-table tr {
|
||||
table tr {
|
||||
border-bottom: 1px solid black;
|
||||
}
|
||||
|
||||
@@ -171,7 +173,7 @@ header {
|
||||
{% endblock header %}
|
||||
</header>
|
||||
<nav>
|
||||
[ <a href="/bans">Home</a> / <a href="/bans/faq">FAQ</a> ]
|
||||
[ <a href="{{route_url("/")}}">Home</a> / <a href="{{route_url("/news")}}">News</a> / <a href="{{route_url("/faq")}}">FAQ</a> ]
|
||||
</nav>
|
||||
<main>
|
||||
{% block main %}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}4chan bans archive FAQ - hiddenservice.cc{% endblock title %}
|
||||
{% block title %}FAQ{% endblock title %}
|
||||
|
||||
{% block style %}
|
||||
<style>
|
||||
@@ -19,6 +19,7 @@
|
||||
{% endblock style %}
|
||||
|
||||
{% block main %}
|
||||
<h2>FAQ</h2>
|
||||
<div class="infobox">
|
||||
<h4>What is this place?</h4>
|
||||
<p>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}4chan bans archive - hiddenservice.cc{% endblock title %}
|
||||
{% block title %}Bans{% endblock title %}
|
||||
|
||||
{% block main %}
|
||||
<div id="search">
|
||||
|
||||
21
chanbans/templates/news.html
Normal file
21
chanbans/templates/news.html
Normal file
@@ -0,0 +1,21 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}News{% endblock title %}
|
||||
|
||||
{% block main %}
|
||||
<h2>News</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th>Item</th>
|
||||
</tr>
|
||||
</thead>
|
||||
{# More news items go here... #}
|
||||
{# 2023-08-02 #}
|
||||
<tr>
|
||||
<td>2023-08-02</td>
|
||||
<td>Site is launched. Happy to have you here.</td>
|
||||
</tr>
|
||||
</table>
|
||||
{% endblock main %}
|
||||
@@ -6,3 +6,7 @@
|
||||
|
||||
# Domain name of the server
|
||||
HTTP_DOMAIN=domain.com
|
||||
|
||||
# Root of the HTTP server. Defaults to "/"
|
||||
# If you host other things on your domain, you may want to update this to be "/bans" or something.
|
||||
#HTTP_ROOT=/
|
||||
|
||||
Reference in New Issue
Block a user