-
Notifications
You must be signed in to change notification settings - Fork 4
/
server
executable file
·61 lines (46 loc) · 1.78 KB
/
server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
import sys
import json
from datetime import datetime
from flask import Flask, render_template, redirect
### Config
app = Flask(__name__)
CONFIG_FILE = 'content.json'
HOST = 'http://127.0.0.1:5000'
def load_config(fname=CONFIG_FILE):
"""read the content.json file and load it as a dictionary"""
with open(fname, 'r') as f:
lines = (l for l in f if not l.strip().startswith('//')) # dont include comment lines with //
return json.loads('\n'.join(lines))
CONFIG = load_config(CONFIG_FILE)
PAGES = {page['url']: page for page in list(CONFIG['PAGES'].values())} # {url: {page_data}}
POSTS = {post['url']: post for post in list(CONFIG['POSTS'].values())} # {url: {post_data}}
### Routes
# Similar to wordpress, pages and posts are separate. Every page has its own template
# in templates/page.html, but all posts use the same template + an iframe URL for the
# post content
@app.route('/')
def index():
return redirect("/index.html")
@app.route('/favicon.ico')
def favicon():
return redirect("/static/favicon.ico")
@app.route('/<path>')
def render_page(path):
page = PAGES[f'/{path}']
return render_template(page['template'], now=datetime.now(), **CONFIG, **page)
@app.route('/posts/<path>')
def render_post(path):
print(path)
post = POSTS[f'/posts/{path}']
return render_template('post.html', now=datetime.now(), **CONFIG, **post)
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == '--pages':
# just print list of page urls
print('\n'.join(HOST + url for url in PAGES.keys()))
elif len(sys.argv) > 1 and sys.argv[1] == '--posts':
# just print list of post urls
print('\n'.join(HOST + url for url in POSTS.keys()))
else:
# run the flask http server
app.run()