Skip to content

Commit

Permalink
merging PR Chaosthebot#515: Fully deprecate old web server and fix ne…
Browse files Browse the repository at this point in the history
…w one

Chaosthebot#515: Fully deprecate old web server and fix new one

Description:
* Move API endpoints to `/api/`
* Reintroduce error page rendering
* Kill old web server, just in case it's still running
* Reload nginx when updating ChaosBot to ensure that configs are reloaded

:white_check_mark: PR passed with a vote of 10 for and 0 against, a weighted total of 9.5 and a threshold of 6.5, and a current meritocracy review.

Vote record:
@PlasmaPower: 1
@Smittyvb: 1
@andrewda: 1
@e-beach: 1
@JustynC7: 1
@md678685: 1
@phil-r: 1
@rhengles: 1
@rohanrk: 1
@rudehn: 1
  • Loading branch information
mdcfe authored and chaosbot committed Jun 4, 2017
1 parent efc3ace commit 8caf06c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion chaos.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def main():
"--socket", "127.0.0.1:8085",
"--wsgi-file", "webserver.py",
"--callable", "__hug_wsgi__",
"--check-static", "/root/workspace/Chaos/server",
"--check-static", "/root/workspace/Chaos/server/",
"--daemonize", "/root/workspace/Chaos/log/uwsgi.log"])

# Schedule all cron jobs to be run
Expand Down
5 changes: 4 additions & 1 deletion etc/nginx/chaos_uwsgi
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ server {
server_name 0.0.0.0;

root /root/workspace/Chaos/server;
index index.html index.htm;

error_page 400 403 404 407 500 502 503 504 /api/errorpage?code=$status;

location / {
include uwsgi_params;
index index.html;
uwsgi_intercept_errors on;
uwsgi_pass 127.0.0.1:8085;
}
}
2 changes: 1 addition & 1 deletion server/voters.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ <h1>Voters on Chaos</h1>
var result = document.getElementById("result");
// read text from URL location
var request = new XMLHttpRequest();
request.open('GET', 'voters', true);
request.open('GET', 'api/voters', true);
request.send(null);
request.onreadystatechange = function() {
if (request.readyState === 4 && request.status === 200) {
Expand Down
9 changes: 9 additions & 0 deletions startup.d/70-webserver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

pkill chaos_server

ln -sf /var/log/nginx/access.log /root/workspace/Chaos/log/nginx-access.log
ln -sf /var/log/nginx/error.log /root/workspace/Chaos/log/nginx-error.log

nginx -t
service nginx force-reload
20 changes: 17 additions & 3 deletions webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@
import json
import linecache
import logging
from http import server


__log = logging.getLogger("webserver")
responses = server.BaseHTTPRequestHandler.responses

index_content = open("/root/workspace/Chaos/server/index.html", "r").read()
errorpage_content = open("/root/workspace/Chaos/server/error.html", "r").read()


@hug.get("/", output=hug.output_format.html)
def get_index():
def render_index():
return index_content


@hug.get("/voters", examples=["amount=20", "amount=0"])
@hug.get("/api/errorpage", output=hug.output_format.html)
def render_error(code: hug.types.number = 500):
status = responses[code]
return errorpage_content % {
"code": code,
"message": status[0],
"explain": status[1]
}


@hug.get("/api/voters", examples=["amount=20", "amount=0"])
def get_voters(amount: hug.types.number = 20):
try:
voters = sorted(json.loads(
Expand All @@ -26,7 +40,7 @@ def get_voters(amount: hug.types.number = 20):
__log.exception("Failed to read voters!")


@hug.get("/meritocracy", examples=["amount=5", "amount=0"])
@hug.get("/api/meritocracy", examples=["amount=5", "amount=0"])
def get_meritocracy(amount: hug.types.number = 20):
try:
meritocracy = json.loads(
Expand Down

0 comments on commit 8caf06c

Please sign in to comment.