minor fixes

This commit is contained in:
Felix
2020-03-12 20:03:52 +01:00
parent 77379f5520
commit d3f71692a0
10 changed files with 130 additions and 42 deletions

View File

@@ -9,42 +9,24 @@ import json
import logging
import os
import datetime
import base64
class S(BaseHTTPRequestHandler):
def do_OPTIONS(self):
self.send_response(200, "ok")
# TODO remove in production
self.send_header('Access-Control-Allow-Origin', '*')
# TODO remove in production
self.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS')
# TODO remove in production
self.send_header("Access-Control-Allow-Headers", "X-Requested-With")
# TODO remove in production
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
def do_GET(self):
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
with open('schedule.json', 'r') as file:
with open('data/schedule.json', 'r') as file:
schedule_string = json.load(file)
self.wfile.write(json.dumps(schedule_string).encode('utf-8'))
def do_POST(self):
self.send_response(200, "ok")
# TODO remove in production
self.send_header('Access-Control-Allow-Origin', '*')
# TODO remove in production
self.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS')
# TODO remove in production
self.send_header("Access-Control-Allow-Headers", "X-Requested-With")
# TODO remove in production
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
content_length = int(self.headers['Content-Length'])
request_raw = self.rfile.read(content_length)
request_content = json.loads(request_raw)
@@ -52,8 +34,9 @@ class S(BaseHTTPRequestHandler):
pw = request_content['pw']
caption = request_content['caption']
heading = request_content['heading']
image = request_content['image']
if pw != '123':
if pw != 'kurswerk':
self.send_response(401)
self.send_header('Content-Type', 'text/html')
self.end_headers()
@@ -65,13 +48,16 @@ class S(BaseHTTPRequestHandler):
schedule_and_caption['heading'] = heading
now = datetime.datetime.now()
try:
os.rename('schedule.json', 'schedule_{}.json'.format(now.strftime("%Y%m%d")))
os.rename('data/schedule.json', 'data/schedule_{}.json'.format(now.strftime("%Y%m%d")))
except Exception:
logging.info('Backup exists, did not backup..')
pass
with open('schedule.json', 'w') as file:
with open('data/schedule.json', 'w') as file:
json.dump(schedule_and_caption, file)
with open('data/schedule.jpg', 'wb') as f:
f.write(base64.b64decode(image))
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
@@ -96,4 +82,4 @@ if __name__ == '__main__':
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
run()

58
backend/nginx.conf Normal file
View File

@@ -0,0 +1,58 @@
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
disable_symlinks off;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream backend {
server kurswerkstatt-pan_backend:8080;
}
server {
root /usr/share/nginx/html;
location /api/ {
proxy_read_timeout 300s;
proxy_connect_timeout 75s;
proxy_redirect off;
proxy_http_version 1.1;
proxy_pass http://backend;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
try_files $uri $uri/ /index.html;
}
}
include /etc/nginx/conf.d/*.conf;
}