fixed Lua Runtime. Plugins now have priority for hooks and POST for routes if requested. also changed the CSS for main.css to color #1e1e1e for darkmode body
This commit is contained in:
64
webserver.py
64
webserver.py
@@ -4,6 +4,7 @@ import threading
|
||||
import subprocess
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
import mimetypes
|
||||
import json
|
||||
from jsmin import jsmin # pip install jsmin
|
||||
from pathlib import Path
|
||||
|
||||
@@ -107,6 +108,69 @@ def index_footer():
|
||||
"""
|
||||
|
||||
class MyHandler(BaseHTTPRequestHandler):
|
||||
# This is a Helper Function for the POST Endpoints
|
||||
def _parse_post_data(self):
|
||||
"""Parse POST request body"""
|
||||
import json
|
||||
content_length = int(self.headers.get('Content-Length', 0))
|
||||
if content_length == 0:
|
||||
return {}
|
||||
|
||||
post_data = self.rfile.read(content_length)
|
||||
content_type = self.headers.get('Content-Type', '')
|
||||
|
||||
try:
|
||||
if 'application/json' in content_type:
|
||||
return json.loads(post_data.decode('utf-8'))
|
||||
elif 'application/x-www-form-urlencoded' in content_type:
|
||||
from urllib.parse import parse_qs
|
||||
parsed = parse_qs(post_data.decode('utf-8'))
|
||||
return {k: v[0] if len(v) == 1 else v for k, v in parsed.items()}
|
||||
else:
|
||||
return {"raw": post_data}
|
||||
except Exception as e:
|
||||
logger.log_error(f"Error parsing POST data: {e}")
|
||||
return {"raw": post_data}
|
||||
|
||||
def do_POST(self):
|
||||
"""Handle POST requests - primarily for plugin routes"""
|
||||
req_path = self.path.lstrip("/")
|
||||
|
||||
# Parse POST data
|
||||
post_data = self._parse_post_data()
|
||||
|
||||
# Add additional request info
|
||||
request_data = {
|
||||
"path": self.path,
|
||||
"headers": dict(self.headers),
|
||||
"data": post_data,
|
||||
"method": "POST"
|
||||
}
|
||||
|
||||
# Check plugin routes
|
||||
plugin_result = plugin_manager.handle_request("/" + req_path, request_data, method="POST")
|
||||
if plugin_result is not None:
|
||||
status, headers, body = plugin_result
|
||||
self.send_response(status)
|
||||
for key, value in headers.items():
|
||||
self.send_header(key, value)
|
||||
self.end_headers()
|
||||
|
||||
if isinstance(body, str):
|
||||
self.wfile.write(body.encode("utf-8"))
|
||||
elif isinstance(body, bytes):
|
||||
self.wfile.write(body)
|
||||
else:
|
||||
self.wfile.write(str(body).encode("utf-8"))
|
||||
return
|
||||
|
||||
# No plugin handled this POST request
|
||||
self.send_response(404)
|
||||
self.send_header("Content-type", "application/json")
|
||||
self.end_headers()
|
||||
error_response = json.dumps({"error": "Route not found"})
|
||||
self.wfile.write(error_response.encode("utf-8"))
|
||||
|
||||
def do_GET(self):
|
||||
req_path = self.path.lstrip("/") # normalize leading /
|
||||
|
||||
|
||||
Reference in New Issue
Block a user