aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xapp.py43
-rw-r--r--index.html17
-rwxr-xr-xserver.py10
3 files changed, 43 insertions, 27 deletions
diff --git a/app.py b/app.py
index 36a705e..3638151 100755
--- a/app.py
+++ b/app.py
@@ -70,18 +70,23 @@ def hostname():
class Response:
- def __init__(self, data):
- self.data = data
+ DEFAULT_STATUS = http.server.HTTPStatus.OK
+
+ @staticmethod
+ def body_from_json(body):
+ return json.dumps(body, ensure_ascii=False, indent=4)
+
+ def __init__(self, body, status=None):
+ if status is None:
+ status = Response.DEFAULT_STATUS
+ self.status = status
+ self.body = body
def headers(self):
yield 'Content-Type', 'text/html; charset=utf-8'
- @staticmethod
- def dump_json(data):
- return json.dumps(data, ensure_ascii=False, indent=4)
-
- def body(self):
- return self.dump_json(self.data)
+ def encode_body(self):
+ return self.body.encode(errors='replace')
def write_as_cgi_script(self):
self.write_headers_as_cgi_script()
@@ -93,22 +98,22 @@ class Response:
print()
def write_body_as_cgi_script(self):
- if self.data is not None:
- print(self.body())
+ if self.body is not None:
+ print(self.body)
- def write_as_request_handler(self, handler):
- handler.send_response(http.server.HTTPStatus.OK)
- self.write_headers_as_request_handler(handler)
- self.write_body_as_request_handler(handler)
+ def write_to_request_handler(self, handler):
+ handler.send_response(self.status)
+ self.write_headers_to_request_handler(handler)
+ self.write_body_to_request_handler(handler)
- def write_headers_as_request_handler(self, handler):
+ def write_headers_to_request_handler(self, handler):
for name, val in self.headers():
handler.send_header(name, val)
handler.end_headers()
- def write_body_as_request_handler(self, handler):
- if self.data is not None:
- handler.wfile.write(self.body().encode(errors='replace'))
+ def write_body_to_request_handler(self, handler):
+ if self.body is not None:
+ handler.wfile.write(self.encode_body())
def run_do(*args, **kwargs):
@@ -195,7 +200,7 @@ class Loginctl(Systemd):
class Task(abc.ABC):
def complete(self):
self.run()
- return Response(self.result())
+ return Response(Response.body_from_json(self.result()))
@abc.abstractmethod
def run(self):
diff --git a/index.html b/index.html
index 52a45fc..e12adeb 100644
--- a/index.html
+++ b/index.html
@@ -64,12 +64,21 @@ h1, .h1 {
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.bundle.min.js"></script>
<script>
+function dump_fail(data) {
+ console.log('Response code was: ' + data.status + ' ' + data.statusText);
+ console.log('Response was:\n' + data.responseText);
+}
+
+function get(url, success_callback) {
+ $.get(url, success_callback).fail(dump_fail);
+}
+
function reboot() {
- $.get('reboot');
+ get('reboot');
}
function shutdown() {
- $.get('poweroff');
+ get('poweroff');
}
function set_hostname(data) {
@@ -82,7 +91,7 @@ function set_top(data) {
}
function refresh_top() {
- $.get('top', function(data) {
+ get('top', function(data) {
set_top(JSON.parse(data));
});
}
@@ -183,7 +192,7 @@ function set_users(data) {
}
function refresh_status() {
- $.get('status', function(data) {
+ get('status', function(data) {
data = JSON.parse(data);
set_hostname(data['hostname']);
set_system(data['system']);
diff --git a/server.py b/server.py
index 1b64766..0ebe5d5 100755
--- a/server.py
+++ b/server.py
@@ -12,8 +12,9 @@ import argparse
import http.server
import os
import sys
+import traceback
-from app import Request
+from app import Request, Response
DEFAULT_PORT = 18101
@@ -31,11 +32,12 @@ class RequestHandler(http.server.SimpleHTTPRequestHandler):
return super().do_GET()
try:
response = request.process()
+ response.write_to_request_handler(self)
except:
- self.send_response(http.server.HTTPStatus.INTERNAL_SERVER_ERROR)
- self.end_headers()
+ status = http.server.HTTPStatus.INTERNAL_SERVER_ERROR
+ response = Response(traceback.format_exc(), status)
+ response.write_to_request_handler(self)
return
- response.write_as_request_handler(self)
def parse_args(args=None):