From 759e160e258ce6ba202b95d27deab860ed121ee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sat, 21 Apr 2018 23:09:14 +0300 Subject: [PATCH] Read config from a config file --- .gitignore | 1 + buranun.conf.example | 17 +++++++++++++++++ config.py | 16 ++++++++++++++++ generate_html.py | 7 ++++--- server.py | 12 ++++++++---- 5 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 buranun.conf.example create mode 100644 config.py diff --git a/.gitignore b/.gitignore index fffc64d..89d2742 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ *.swp +buranun.conf diff --git a/buranun.conf.example b/buranun.conf.example new file mode 100644 index 0000000..d685680 --- /dev/null +++ b/buranun.conf.example @@ -0,0 +1,17 @@ +[server] +# The port the server will bind on +port = 4000 +# This is meant for when Buranun is behind a reverse proxy +# url_prefix is added to all generated links, but nothing is done +# for requested path. It is is also used for the Path attribute for cookies +# Buranun sets +url_prefix = /board +# Buranun doesn't support encrypted connections itself, and this is meant for +# when it is behind a reverse proxy +# ssl controls whether cookies set the Secure attribute, meaning they are only +# sent over an encrypted connection +ssl = True + +[site] +# This is the site name displayed on e.g. the index page +name = A random Buranun-based textboard diff --git a/config.py b/config.py new file mode 100644 index 0000000..05da309 --- /dev/null +++ b/config.py @@ -0,0 +1,16 @@ +import configparser + +# load(filename) +# Populate the config variables +def load(filename): + global port, ssl, url_prefix + global site_name + + config = configparser.ConfigParser() + config.read(filename) + + port = int(config['server']['port']) + ssl = bool(config['server']['ssl']) + url_prefix = config['server']['url_prefix'] + + site_name = config['site']['name'] diff --git a/generate_html.py b/generate_html.py index a8f0724..90b4f7a 100644 --- a/generate_html.py +++ b/generate_html.py @@ -2,14 +2,15 @@ import urllib.parse import bs4 +import config + # generate_nav(*, soup) → nav_tag def generate_nav(*, soup): # TODO: Don't generate link to a board if we're at the index nav_tag = soup.new_tag('nav') for board in ['a', 'b', 'his', 'g']: - # FIXME: Read this from a config file - url = '/board/' + urllib.parse.quote(board, safe = '') + '/' + url = config.url_prefix + '/' + urllib.parse.quote(board, safe = '') + '/' a_tag = soup.new_tag('a', href = url) a_tag.string = '/' + board + '/' nav_tag.append(a_tag) @@ -86,7 +87,7 @@ def board(board_name): def index(): # TODO: Create an index page soup = new_soup() - return page_skeleton(page_title = 'Buranun', contents = [], soup = soup) + return page_skeleton(page_title = config.site_name, contents = [], soup = soup) # error_404(path) → html def error_404(path): diff --git a/server.py b/server.py index a43e558..b1c8753 100644 --- a/server.py +++ b/server.py @@ -2,6 +2,7 @@ import http.cookies import http.server import urllib.parse +import config import generate_html class HTTPRequestHandler(http.server.BaseHTTPRequestHandler): @@ -15,10 +16,9 @@ class HTTPRequestHandler(http.server.BaseHTTPRequestHandler): # TODO: Make this more sensical sent_cookies = http.cookies.SimpleCookie() sent_cookies['buranun_session'] = 'dihutenosa' - sent_cookies['buranun_session']['domain'] = 'ahti-saarelainen.zgrep.org' - sent_cookies['buranun_session']['path'] = '/board' + sent_cookies['buranun_session']['path'] = config.url_prefix if config.url_prefix != '' else '/' sent_cookies['buranun_session']['max-age'] = 60 - sent_cookies['buranun_session']['secure'] = True + sent_cookies['buranun_session']['secure'] = config.ssl sent_cookies['buranun_session']['httponly'] = True self.send_response(status_code) @@ -33,6 +33,7 @@ class HTTPRequestHandler(http.server.BaseHTTPRequestHandler): self.wfile.write(encoded) + def __send_404(self, path): html = generate_html.error_404(path) self.__send_html(html, status_code = 404) @@ -75,7 +76,10 @@ class HTTPRequestHandler(http.server.BaseHTTPRequestHandler): self.__send_404(path) def main(): - httpd = http.server.HTTPServer(('', 4000), HTTPRequestHandler) + # TODO: Don't hardcode + config.load('buranun.conf') + + httpd = http.server.HTTPServer(('', config.port), HTTPRequestHandler) httpd.serve_forever() if __name__ == '__main__':