|
@ -1,11 +1,13 @@ |
|
|
#! /usr/bin/python3 |
|
|
#! /usr/bin/python3 |
|
|
from functools import partial |
|
|
from functools import partial |
|
|
|
|
|
from collections import defaultdict |
|
|
|
|
|
|
|
|
from flask import abort, Flask, render_template, flash, request, send_from_directory, jsonify, g |
|
|
from flask import abort, Flask, render_template, flash, request, send_from_directory, jsonify, g |
|
|
from werkzeug.local import Local, LocalProxy, LocalManager |
|
|
from werkzeug.local import Local, LocalProxy, LocalManager |
|
|
from flask_appconfig import AppConfig |
|
|
from flask_appconfig import AppConfig |
|
|
from flask_login import LoginManager, login_required, login_user |
|
|
from flask_login import LoginManager, login_required, login_user |
|
|
from flask_wtf.csrf import CSRFProtect |
|
|
from flask_wtf.csrf import CSRFProtect |
|
|
|
|
|
from flask.ext.cache import Cache |
|
|
|
|
|
|
|
|
from urllib.parse import unquote |
|
|
from urllib.parse import unquote |
|
|
from urllib.parse import quote, unquote |
|
|
from urllib.parse import quote, unquote |
|
@ -14,13 +16,15 @@ from json import dumps, loads |
|
|
from admin import Admin |
|
|
from admin import Admin |
|
|
|
|
|
|
|
|
from werkzeug.contrib.cache import MemcachedCache |
|
|
from werkzeug.contrib.cache import MemcachedCache |
|
|
cache = MemcachedCache(['127.0.0.1:11211']) |
|
|
memcache = MemcachedCache(['127.0.0.1:11211']) |
|
|
|
|
|
|
|
|
import os |
|
|
import os |
|
|
|
|
|
|
|
|
from posts import Posts |
|
|
from posts import Posts |
|
|
from projects import getProjects |
|
|
from projects import getProjects |
|
|
|
|
|
|
|
|
|
|
|
cache = Cache(config={'CACHE_TYPE': 'memcached'}) |
|
|
|
|
|
|
|
|
login_manager = LoginManager() |
|
|
login_manager = LoginManager() |
|
|
|
|
|
|
|
|
def cacheit(key, thunk): |
|
|
def cacheit(key, thunk): |
|
@ -29,11 +33,11 @@ def cacheit(key, thunk): |
|
|
If there is no cached version then it will |
|
|
If there is no cached version then it will |
|
|
evaluate thunk and cache that |
|
|
evaluate thunk and cache that |
|
|
""" |
|
|
""" |
|
|
cached = cache.get(quote(key)) |
|
|
cached = memcache.get(quote(key)) |
|
|
if cached is None: |
|
|
if cached is None: |
|
|
print("cache miss for %s" % key) |
|
|
print("cache miss for %s" % key) |
|
|
result = thunk() |
|
|
result = thunk() |
|
|
cache.set(quote(key), result) |
|
|
memcache.set(quote(key), result) |
|
|
return result |
|
|
return result |
|
|
print("cache hit for %s" % key) |
|
|
print("cache hit for %s" % key) |
|
|
return cached |
|
|
return cached |
|
@ -82,35 +86,43 @@ def NeverWhere(configfile=None): |
|
|
def projects(): |
|
|
def projects(): |
|
|
return jsonify(loads(cacheit("projects", getProjects))) |
|
|
return jsonify(loads(cacheit("projects", getProjects))) |
|
|
|
|
|
|
|
|
@app.route("/blog/stuff", methods=("GET",)) |
|
|
|
|
|
def stuff(): |
|
|
|
|
|
return render_template("projects.html") |
|
|
|
|
|
|
|
|
|
|
|
# blog post routes |
|
|
# page routes |
|
|
|
|
|
@cache.cached(timeout=50) |
|
|
@app.route("/blog/posts/", methods=("GET",)) |
|
|
@app.route("/blog/posts/", methods=("GET",)) |
|
|
def renderInitial(): |
|
|
def renderInitial(): |
|
|
return render_template("index.html", quote=quote, postcontent=dict(initial_post)) |
|
|
return render_template("index.html", postid=initial_post["_id"], page="posts", quote=quote, postcontent=dict(initial_post)) |
|
|
|
|
|
|
|
|
@app.route("/blog/posts/<_id>", methods=("GET",)) |
|
|
@app.route("/blog/posts/<_id>", methods=("GET",)) |
|
|
def renderPost(_id): |
|
|
def renderPost(_id): |
|
|
post_content = loads(cacheit(_id, lambda: dumps(posts.getpost(_id, json=False)))) |
|
|
post_content = loads(cacheit(_id, lambda: dumps(posts.getpost(_id, json=False)))) |
|
|
return render_template("index.html", quote=quote, postcontent=dict(post_content)) |
|
|
return render_template("index.html", postid=initial_post["_id"], page="posts", quote=quote, postcontent=dict(post_content)) |
|
|
|
|
|
|
|
|
|
|
|
@cache.cached(timeout=50) |
|
|
|
|
|
@app.route("/blog/projects", methods=("GET",)) |
|
|
|
|
|
def showProjects(): |
|
|
|
|
|
return render_template("index.html", postid=initial_post["_id"], page="projects", quote=quote, postcontent=defaultdict(str)) |
|
|
|
|
|
|
|
|
|
|
|
@cache.cached(timeout=50) |
|
|
@app.route("/blog/", methods=("GET", "POST")) |
|
|
@app.route("/blog/", methods=("GET", "POST")) |
|
|
def index(): |
|
|
def index(): |
|
|
return renderInitial() |
|
|
return renderInitial() |
|
|
|
|
|
|
|
|
# get the next post |
|
|
# get the next post |
|
|
|
|
|
|
|
|
|
|
|
@cache.cached(timeout=50) |
|
|
@app.route("/blog/switchpost/<pid>/<category>") |
|
|
@app.route("/blog/switchpost/<pid>/<category>") |
|
|
def getpostid(pid, category): |
|
|
def getpostid(pid, category): |
|
|
return posts.iterpost(startkey=pid, category=category) |
|
|
return posts.iterpost(startkey=pid, category=category) |
|
|
|
|
|
|
|
|
# get the post previous to this one |
|
|
# get the post previous to this one |
|
|
|
|
|
@cache.cached(timeout=50) |
|
|
@app.route("/blog/prevpost/<pid>/<category>") |
|
|
@app.route("/blog/prevpost/<pid>/<category>") |
|
|
def prevpost(pid, category): |
|
|
def prevpost(pid, category): |
|
|
return posts.iterpost(endkey=pid, category=category) |
|
|
return posts.iterpost(endkey=pid, category=category) |
|
|
|
|
|
|
|
|
# get the contents of any post |
|
|
# get the contents of any post |
|
|
|
|
|
@cache.cached(timeout=50) |
|
|
@app.route("/blog/getpost/<_id>/<category>") |
|
|
@app.route("/blog/getpost/<_id>/<category>") |
|
|
def getpost(_id, category): |
|
|
def getpost(_id, category): |
|
|
return posts.getpost(_id, category=category) |
|
|
return posts.getpost(_id, category=category) |
|
@ -120,6 +132,7 @@ def NeverWhere(configfile=None): |
|
|
def allposts(): |
|
|
def allposts(): |
|
|
return posts.allposts() |
|
|
return posts.allposts() |
|
|
|
|
|
|
|
|
|
|
|
@cache.cached(timeout=50) |
|
|
@app.route("/blog/categories") |
|
|
@app.route("/blog/categories") |
|
|
def categories(): |
|
|
def categories(): |
|
|
return posts.categories() |
|
|
return posts.categories() |
|
@ -187,5 +200,7 @@ csrf = CSRFProtect() |
|
|
|
|
|
|
|
|
csrf.init_app(app) |
|
|
csrf.init_app(app) |
|
|
|
|
|
|
|
|
|
|
|
cache.init_app(app) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
if __name__ == "__main__": |
|
|
NeverWhere().run(host="localhost", port=8001, debug=True) |
|
|
NeverWhere().run(host="localhost", port=8001, debug=True) |
|
|