From f0cc2dd1bd79afce856204dfa6a450d493f61800 Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Wed, 2 Aug 2023 18:31:36 -0700 Subject: [PATCH] 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 --- chanbans/config.py | 1 + chanbans/http.py | 54 ++++++++++++++++------------------- chanbans/templates/base.html | 14 +++++---- chanbans/templates/faq.html | 3 +- chanbans/templates/index.html | 2 +- chanbans/templates/news.html | 21 ++++++++++++++ example.env | 4 +++ 7 files changed, 62 insertions(+), 37 deletions(-) create mode 100644 chanbans/templates/news.html diff --git a/chanbans/config.py b/chanbans/config.py index d54a40d..294efb0 100644 --- a/chanbans/config.py +++ b/chanbans/config.py @@ -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"/") diff --git a/chanbans/http.py b/chanbans/http.py index 9178050..570f10d 100644 --- a/chanbans/http.py +++ b/chanbans/http.py @@ -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) diff --git a/chanbans/templates/base.html b/chanbans/templates/base.html index 7bd3097..c115d2c 100644 --- a/chanbans/templates/base.html +++ b/chanbans/templates/base.html @@ -4,8 +4,10 @@ {% block head %} {% endblock head %} + 4chan bans archive - {% block title %} - {% endblock title %} + {% endblock title %} - + {{ config.HTTP_DOMAIN }}