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:
2023-08-02 18:31:36 -07:00
parent d3f6e6f69e
commit f0cc2dd1bd
7 changed files with 62 additions and 37 deletions

View File

@@ -27,3 +27,4 @@ THUMBS_DIR = Path(default("THUMBS_DIR", "thumbs"))
CACHE_DIR = Path(default("CACHE_DIR", "cache")) CACHE_DIR = Path(default("CACHE_DIR", "cache"))
HTTP_DOMAIN = required("HTTP_DOMAIN") HTTP_DOMAIN = required("HTTP_DOMAIN")
HTTP_ROOT = default("HTTP_ROOT", r"/")

View File

@@ -6,15 +6,33 @@ from typing import Any, MutableMapping, Optional, Sequence
from aiohttp import web from aiohttp import web
from jinja2 import Environment, PackageLoader, select_autoescape from jinja2 import Environment, PackageLoader, select_autoescape
from . import config
from .db import get_db, search_db from .db import get_db, search_db
# 2023-07-24 - eating stirfry tonight. It's pretty bad. # 2023-07-24 - eating stirfry tonight. It's pretty bad.
# 2023-07-31 - In a meeting. Hope I don't get caught # 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( _env = Environment(
loader=PackageLoader("chanbans"), loader=PackageLoader("chanbans"),
autoescape=select_autoescape(), autoescape=select_autoescape(),
) )
_env.globals.update(
{
"route_url": route_url,
}
)
TemplateContext = MutableMapping[str, Any] TemplateContext = MutableMapping[str, Any]
@@ -49,7 +67,9 @@ class TemplateView(web.View):
def get_context(self) -> TemplateContext: def get_context(self) -> TemplateContext:
"Gets the context for this template" "Gets the context for this template"
return dict() return {
"config": config,
}
def template_render(self) -> str: def template_render(self) -> str:
"Renders the template on this object." "Renders the template on this object."
@@ -131,31 +151,6 @@ class IndexView(TemplateView):
return HtmlResponse(text=html) 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 # Routes
################################################################################ ################################################################################
@@ -164,11 +159,12 @@ class BansJson(web.View):
app = web.Application() app = web.Application()
app.add_routes( app.add_routes(
[ [
web.get(r"/bans", IndexView), web.get(route_url(r"/"), IndexView),
web.get(r"/bans/faq", template_view_factory("faq.html")), web.get(route_url(r"/faq"), template_view_factory("faq.html")),
#web.get(r"/api/v1/bans/{page:\d+}", BansJson), web.get(route_url(r"/news"), template_view_factory("news.html")),
] ]
) )
def run_app(): def run_app():
web.run_app(app) web.run_app(app)

View File

@@ -4,8 +4,10 @@
{% block head %} {% block head %}
{% endblock head %} {% endblock head %}
<title> <title>
4chan bans archive -
{% block title %} {% block title %}
{% endblock title %} {% endblock title %} -
{{ config.HTTP_DOMAIN }}
</title> </title>
<style> <style>
@@ -50,7 +52,7 @@ header {
width: 100%; width: 100%;
} }
#bans-table { table {
border-top: 1px solid black; border-top: 1px solid black;
border-left: 1px solid black; border-left: 1px solid black;
border-right: 1px solid black; border-right: 1px solid black;
@@ -62,13 +64,13 @@ header {
color: black; color: black;
} }
#bans-table td { table td {
border-left: 1px solid black; border-left: 1px solid black;
padding: 5px; padding: 5px;
vertical-align: top; vertical-align: top;
} }
#bans-table th { table th {
width: auto; width: auto;
border-left: 1px solid black; border-left: 1px solid black;
background-color: #fca; background-color: #fca;
@@ -77,7 +79,7 @@ header {
text-align: center; text-align: center;
} }
#bans-table tr { table tr {
border-bottom: 1px solid black; border-bottom: 1px solid black;
} }
@@ -171,7 +173,7 @@ header {
{% endblock header %} {% endblock header %}
</header> </header>
<nav> <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> </nav>
<main> <main>
{% block main %} {% block main %}

View File

@@ -1,6 +1,6 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block title %}4chan bans archive FAQ - hiddenservice.cc{% endblock title %} {% block title %}FAQ{% endblock title %}
{% block style %} {% block style %}
<style> <style>
@@ -19,6 +19,7 @@
{% endblock style %} {% endblock style %}
{% block main %} {% block main %}
<h2>FAQ</h2>
<div class="infobox"> <div class="infobox">
<h4>What is this place?</h4> <h4>What is this place?</h4>
<p> <p>

View File

@@ -1,6 +1,6 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block title %}4chan bans archive - hiddenservice.cc{% endblock title %} {% block title %}Bans{% endblock title %}
{% block main %} {% block main %}
<div id="search"> <div id="search">

View 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 %}

View File

@@ -6,3 +6,7 @@
# Domain name of the server # Domain name of the server
HTTP_DOMAIN=domain.com 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=/