-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
132 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"server": { | ||
"port" : 8000, | ||
"ip_address" : "localhost" | ||
}, | ||
"frontend": { | ||
"website" : "True", | ||
"port": 9000 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,63 @@ | ||
""" | ||
HTTP Server Module | ||
""" | ||
|
||
import os | ||
import http.server | ||
from flask import Flask, send_file, send_from_directory, request | ||
import subprocess | ||
import json | ||
import helper | ||
from urllib.parse import urlparse, parse_qs | ||
from dotenv import load_dotenv | ||
from helper import query_to_args_list | ||
|
||
# Load environment variables from .env file | ||
load_dotenv(override=True) | ||
port_env = int(os.getenv("PORT")) | ||
website = bool(os.getenv("WEBSITE")) | ||
import sys | ||
import asyncio | ||
import urllib.parse | ||
|
||
#Starts website if website==True | ||
helper.start_website(website) | ||
app = Flask(__name__) | ||
|
||
with open('../../config.json') as f: | ||
json_config = json.load(f) | ||
|
||
class MyHTTPRequestHandler(http.server.BaseHTTPRequestHandler): | ||
""" | ||
Handles HTTP requests | ||
""" | ||
port_json = int(json_config["server"]["port"]) | ||
website = bool(json_config["frontend"]["website"]) | ||
|
||
# pylint: disable=C0103 | ||
def do_GET(self): | ||
""" | ||
Function for GET requests | ||
""" | ||
# Parse the query parameters | ||
parsed_url = urlparse(self.path) | ||
@app.route('/config.json') | ||
def serve_config(): | ||
return send_from_directory('../../', 'config.json') | ||
|
||
# Extract the arguments from the query parameters | ||
args = query_to_args_list(parsed_url.query) | ||
|
||
if "help" in args: | ||
# If 'help' is in the args, read and return the content of help.txt | ||
try: | ||
with open( | ||
"../../help.txt", "r", encoding="utf-8", errors="ignore" | ||
) as file: | ||
help_text = file.read() | ||
self.send_response(200) | ||
self.send_header("Content-type", "text/plain") | ||
self.send_header( | ||
"Access-Control-Allow-Origin", "*" | ||
) # Allow requests from any origin | ||
self.end_headers() | ||
self.wfile.write(help_text.encode()) | ||
except FileNotFoundError: | ||
self.send_response(404) | ||
self.send_header("Content-type", "text/plain") | ||
self.end_headers() | ||
self.wfile.write(b"help.txt not found") | ||
else: | ||
@app.route('/help') | ||
def serve_help(): | ||
return send_from_directory('../../', 'help.txt') | ||
|
||
# Otherwise, run the main.py script with the provided arguments | ||
result = subprocess.run( | ||
["python3", "main.py"] + args, | ||
capture_output=True, | ||
text=True, | ||
check=True, | ||
) | ||
self.send_response(200) | ||
self.send_header("Content-type", "text/plain") | ||
self.send_header( | ||
"Access-Control-Allow-Origin", "*" | ||
) # Allow requests from any origin | ||
self.end_headers() | ||
self.wfile.write(result.stdout.encode()) | ||
@app.route('/home') | ||
def serve_index(): | ||
return send_file('../frontend/index.html') | ||
|
||
@app.route('/script.js') | ||
def serve_script(): | ||
return send_file('../frontend/script.js') | ||
|
||
def run( | ||
server_class=http.server.HTTPServer, handler_class=MyHTTPRequestHandler, port=8000 | ||
): | ||
""" | ||
Run the server! | ||
""" | ||
server_address = ("", port_env) | ||
httpd = server_class(server_address, handler_class) | ||
print(f"Server running on port {port}") | ||
@app.route('/') | ||
def default_route(): | ||
query_parameters = urllib.parse.parse_qsl(request.query_string.decode(), keep_blank_values=True) | ||
parsed_parameters = [] | ||
|
||
httpd.serve_forever() | ||
|
||
|
||
if __name__ == "__main__": | ||
run(port=port_env) | ||
for key, value in query_parameters: | ||
if value: | ||
parsed_parameters.append(f"{key}={value}") | ||
else: | ||
parsed_parameters.append(key) | ||
|
||
# Join the parsed parameters list into a single string | ||
args = ','.join(parsed_parameters) | ||
|
||
async def run_subprocess(): | ||
result = subprocess.run( | ||
["python3", "main.py", args], | ||
capture_output=True, | ||
text=True, | ||
check=True, | ||
) | ||
return result.stdout | ||
|
||
# Run subprocess asynchronously | ||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
result = loop.run_until_complete(run_subprocess()) | ||
return result | ||
|
||
if __name__ == '__main__': | ||
app.run(port=port_json) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters