shii.moe

Source for `shii.moe' website.
git clone git://git.knutsen.co/shii.moe
Log | Files | Refs

commit 9d7ec715d1f2c6bda0bcc51ef22aaddaab12b8a9
parent a30a88abdec5ea1fda7cab8bc25004a65a97b694
Author: Demonstrandum <moi@knutsen.co>
Date:   Fri, 18 Sep 2020 21:00:57 +0100

Update to uWSGI and get local time instead.

Diffstat:
M.gitignore | 5+++--
Dserver.py | 58----------------------------------------------------------
Ashiimoe.ini | 11+++++++++++
Ashiimoe.py | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mstart.sh | 5++---
Mstop.sh | 0
Mtemplates/guestbook.html | 55+++++++++++++++++++++++++++++++++++++++++++++++++++----
Awsgi.py | 8++++++++
8 files changed, 143 insertions(+), 67 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,4 +1,6 @@ __pycache__/ *.pyc desktop.ini -comments.json- \ No newline at end of file +comments.json +.pid +.log diff --git a/server.py b/server.py @@ -1,57 +0,0 @@ -from flask import Flask, render_template, send_from_directory, request, redirect -from datetime import datetime -from os import path -app = Flask(__name__, static_url_path="", static_folder="./") - -import json - -COMMENT_FILE = "comments.json" -DATE_FORMAT = "%Y-%m-%d %H:%M" - -def read_comments(): - comments = None - try: - with open(COMMENT_FILE, 'r') as f: - comments = json.load(f) - except FileNotFoundError: - comments = [] - - return comments - - -@app.route('/') -def home(): - return send_from_directory('./', 'index.html') - -@app.route('/guestbook') -def guestbook(): - comments = read_comments() - return render_template('guestbook.html', comments=reversed(comments)) - -@app.route('/postcomment', methods=['POST']) -def postcomment(): - name = request.form['name'] - comment = request.form['comment'] - date = datetime.now() - ip = request.environ.get('HTTP_X_REAL_IP') - - comments = read_comments() - comments.append({ - 'name': name, - 'comment': comment, - 'date': datetime.strftime(date, DATE_FORMAT), - 'ip': ip - }) - - with open(COMMENT_FILE, 'w') as f: - json.dump(comments, f, indent=4, separators=(",", ": ")) - - return redirect('/guestbook', code=302) - - -@app.route('/<p>') -def serve_static(p): - print("testets") - if path.exists(p + '.html'): - return send_from_directory('./', p + '.html') - return send_from_directory('./', p)- \ No newline at end of file diff --git a/shiimoe.ini b/shiimoe.ini @@ -0,0 +1,11 @@ +[uwsgi] +module = wsgi:app + +master = true +processes = 5 + +socket = shiimoe.sock +chmod-socket = 660 +vacuum = true + +die-on-term = true diff --git a/shiimoe.py b/shiimoe.py @@ -0,0 +1,68 @@ +from flask import Flask, render_template, send_from_directory, request, redirect +from datetime import datetime +from os import path +app = Flask(__name__, static_url_path="", static_folder="./") + +import json + +COMMENT_FILE = "comments.json" +DATE_FORMAT = "%Y-%m-%d %H:%M" + +def read_comments(): + comments = None + try: + with open(COMMENT_FILE, 'r') as f: + comments = json.load(f) + except FileNotFoundError: + comments = [] + + return comments + + +@app.route('/') +def home(): + return send_from_directory('./', 'index.html') + +def render_guestbook(**kw): + comments = read_comments() + + kw['comments'] = reversed(comments) + return render_template('guestbook.html', **kw) + +@app.route('/guestbook') +def guestbook(): + return render_guestbook() + +@app.route('/postcomment', methods=['POST']) +def postcomment(): + now = datetime.now() + + name = request.form.get('name') + comment = request.form.get('comment') + date = request.form.get('date') or datetime.strftime(now, DATE_FORMAT) + ip = request.environ.get('HTTP_X_REAL_IP') + + # Comment or name left empty, send error message. + if not (name or "").strip() or not (comment or "").strip(): + return render_guestbook(error_msg="Please provide a name and comment.") + + comments = read_comments() + comments.append({ + 'name': name, + 'comment': comment, + 'date': date, + 'ip': ip + }) + + with open(COMMENT_FILE, 'w') as f: + json.dump(comments, f, indent=4, separators=(",", ": ")) + + return redirect('/guestbook', code=302) + + +@app.route('/<p>') +def serve_static(p): + print("testets") + if path.exists(p + '.html'): + return send_from_directory('./', p + '.html') + return send_from_directory('./', p) diff --git a/start.sh b/start.sh @@ -1,6 +1,6 @@ #!/bin/sh -FLASK_APP=server.py flask run --host=0.0.0.0 --port=5000 & +uwsgi --ini shiimoe.ini & echo "$!" > ./.pid -echo "Shii.moe is online!"- \ No newline at end of file +echo "Shii.moe is online!" diff --git a/stop.sh b/stop.sh diff --git a/templates/guestbook.html b/templates/guestbook.html @@ -20,7 +20,7 @@ margin-top: 3em; } .comment-input { - width: 30%; + width: 30%; height: 8em; margin: 0.5em 0em; } @@ -34,6 +34,13 @@ .comment { margin: 1.5em 0; } + .error { + margin: 1em 0 2em 0; + padding: 1em 1.5em; + border: 2pt solid red; + border-radius: 7pt; + background: #f464; + } </style> <link rel='stylesheet' type='text/css' href="style.css"/> @@ -52,16 +59,56 @@ <div class="titleButton"><a href="/books">B√łker</a></div> </div> - <form action="/postcomment" method="POST"> + {% if error_msg is defined %} + <p class="error">{{ error_msg }}</p> + {% endif %} + + <form action="/postcomment" method="POST" name="commentBox"> <input class="name-input input" name="name" placeholder="Your name" required> <br> <textarea class="comment-input input" name="comment" placeholder="Your comment" required></textarea> <br> - <input type="submit" value="Submit"> + <input type="submit" value="Post"> </form> <!-- add gender radio boxes (femboy) --> - {% if comments %} + <script> + // Script to get local time, will just fall back on server time + // if the script does not run. + const form = document.forms.namedItem("commentBox"); + form.addEventListener('submit', ev => { + ev.preventDefault(); + + const formData = new FormData(form); + + const now = new Date(); + const { year, month, day } = { + year: String(now.getFullYear()), + month: String(now.getMonth() + 1).padStart(2, '0'), + day: String(now.getDate()).padStart(2, '0') + }; + const { hour, minute } = { + hour: String(now.getHours()).padStart(2, '0'), + minute: String(now.getMinutes()).padStart(2, '0') + }; + const dateString = `${year}-${month}-${day} ${hour}:${minute}`; + + // Append to form: + formData.append('date', dateString); + + // Send form: + const req = new XMLHttpRequest(); + req.open(form.method, form.action, true); + req.onload = event => { + document.write(req.response); + }; + req.send(formData); + + window.location.reload(); + }, false); + </script> + + {% if comments is defined %} <div class="comments"> {% for comment in comments %} <div class="comment"> diff --git a/wsgi.py b/wsgi.py @@ -0,0 +1,8 @@ +#!/usr/bin/env python3 + +from shiimoe import app + +if __name__ == "__main__": + app.run() + +