Read config from a config file
This commit is contained in:
parent
9e2a7d338d
commit
759e160e25
|
@ -1,2 +1,3 @@
|
|||
__pycache__
|
||||
*.swp
|
||||
buranun.conf
|
||||
|
|
|
@ -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
|
|
@ -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']
|
|
@ -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):
|
||||
|
|
12
server.py
12
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__':
|
||||
|
|
Loading…
Reference in New Issue