|
| 1 | +# Script to assist with PVEDiscordDark development |
| 2 | +# |
| 3 | +# By default serves HTTP on port 3000, any *.js request gets the JS script, any *.css request gets the CSS file and any image request gets corresponding image |
| 4 | +# Meant to be used with the "Requestly" browser extension to redirect PVEDD requests from PVE server to localhost:3000 |
| 5 | +# |
| 6 | + |
| 7 | +from http.server import HTTPServer, BaseHTTPRequestHandler |
| 8 | +import json |
| 9 | +import os |
| 10 | + |
| 11 | +PORT = 3000 |
| 12 | +DIR_SASS = os.path.join(os.path.dirname(__file__), "sass") |
| 13 | +DIR_IMAGES = os.path.join(os.path.dirname(__file__), "images") |
| 14 | +DIR_JS = os.path.join(os.path.dirname(__file__), "js") |
| 15 | + |
| 16 | + |
| 17 | +class Server(BaseHTTPRequestHandler): |
| 18 | + def log_message(self, format, *args): |
| 19 | + return |
| 20 | + |
| 21 | + def _set_headers(self, status, type): |
| 22 | + self.send_response(status) |
| 23 | + self.send_header("Content-type", type) |
| 24 | + self.end_headers() |
| 25 | + |
| 26 | + def do_GET(self): |
| 27 | + status = 200 |
| 28 | + type = "application/json" |
| 29 | + data = None |
| 30 | + |
| 31 | + file = self.path.rpartition("/")[2] |
| 32 | + ext = file.rpartition(".")[2] |
| 33 | + |
| 34 | + if ext == "css": |
| 35 | + data = open(os.path.join(DIR_SASS, "PVEDiscordDark.css"), "rb").read() |
| 36 | + type = "text/css" |
| 37 | + elif ext == "js": |
| 38 | + data = open(os.path.join(DIR_JS, "PVEDiscordDark.js"), "rb").read() |
| 39 | + type = "application/javascript" |
| 40 | + elif ext == "png" or ext == "jpg" or ext == "jpeg": |
| 41 | + try: |
| 42 | + data = open(os.path.join(DIR_IMAGES, file), "rb").read() |
| 43 | + type = f"image/{ext}" |
| 44 | + except FileNotFoundError: |
| 45 | + status = 404 |
| 46 | + else: |
| 47 | + status = 400 |
| 48 | + self._set_headers(status, type) |
| 49 | + if status == 200: |
| 50 | + self.wfile.write(data) |
| 51 | + else: |
| 52 | + self.wfile.write(json.dumps({"error": status}).encode()) |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + print(f"Serving on localhost:{PORT}") |
| 57 | + server = HTTPServer(server_address=("", PORT), RequestHandlerClass=Server) |
| 58 | + try: |
| 59 | + server.serve_forever() |
| 60 | + except KeyboardInterrupt: |
| 61 | + quit() |
0 commit comments