nothing works, shits broken
This commit is contained in:
67
server.py
67
server.py
@@ -1,4 +1,4 @@
|
||||
from os import environ
|
||||
# from os import environ
|
||||
import argparse
|
||||
from flask import Flask, request, make_response, send_from_directory, jsonify
|
||||
from config import PrinterConfig
|
||||
@@ -9,6 +9,8 @@ argument_parser.add_argument("--port", type=int, required=False, default=8005)
|
||||
|
||||
args = argument_parser.parse_args()
|
||||
|
||||
# todo read parameters from env
|
||||
|
||||
printer_config = PrinterConfig()
|
||||
app = Flask(__name__)
|
||||
|
||||
@@ -20,21 +22,22 @@ def index_route():
|
||||
|
||||
@app.route("/config", methods=["GET", "POST"])
|
||||
def config_route():
|
||||
request_type = None
|
||||
if "Accept" in request.headers:
|
||||
if "text/html" in request.headers["Accept"]:
|
||||
request_type = "html"
|
||||
elif "application/json" in request.headers["Accept"]:
|
||||
request_type = "json"
|
||||
json_request = request.args.get("json", False)
|
||||
print(f"{json_request=}")
|
||||
if not json_request and json_request != "":
|
||||
json_request = False
|
||||
elif json_request == "":
|
||||
json_request = True
|
||||
|
||||
print(request_type)
|
||||
if request.method == "GET" and request_type == "html":
|
||||
if request.method == "GET" and not json_request:
|
||||
# return config html page
|
||||
return send_from_directory("static", "config.html")
|
||||
elif request.method == "GET" and request_type == "json":
|
||||
|
||||
elif request.method == "GET" and json_request:
|
||||
# render config as JSON
|
||||
print(get_config())
|
||||
return jsonify(get_config())
|
||||
|
||||
elif request.method == "POST" and request.is_json:
|
||||
# accept JSON form with new parameters
|
||||
errors = {"critical": False, "errors": {}}
|
||||
@@ -44,18 +47,21 @@ def config_route():
|
||||
except Exception:
|
||||
errors["errors"]["json"] = "Could not decode message as json"
|
||||
errors["critical"] = True
|
||||
|
||||
|
||||
request_params = {}
|
||||
if "address" in request_data:
|
||||
request_params["address"] = request_data["address"]
|
||||
if "port" in request_data:
|
||||
port = request_data["port"]
|
||||
# todo remove redundant checks
|
||||
if isinstance(port, int) and 0 < port <= 65535:
|
||||
request_params["port"] = port
|
||||
else:
|
||||
errors["errors"]["parseint"] = "Could not parse port number as an integer"
|
||||
errors["errors"][
|
||||
"parseint"
|
||||
] = "Could not parse port number as an integer"
|
||||
errors["critical"] = True
|
||||
|
||||
|
||||
response = jsonify(errors)
|
||||
if errors["critical"]:
|
||||
response.status_code = 422
|
||||
@@ -67,36 +73,43 @@ def config_route():
|
||||
|
||||
def get_config():
|
||||
return {
|
||||
"address": printer_config.printer_address,
|
||||
"port": printer_config.printer_port,
|
||||
"profile": printer_config.printer_profile,
|
||||
"can_print": can_print(),
|
||||
"address": printer_config.address,
|
||||
"port": printer_config.port,
|
||||
"profile": printer_config.profile,
|
||||
}
|
||||
|
||||
|
||||
def set_config(address: str = None, port: int = None):
|
||||
if address is None:
|
||||
printer_config.printer_address = None
|
||||
else:
|
||||
printer_config.printer_address = address
|
||||
if port is None:
|
||||
printer_config.printer_port = 9100
|
||||
else:
|
||||
printer_config.printer_port = port
|
||||
printer_config.address = address
|
||||
printer_config.port = port
|
||||
# printer_config.profile = profile # fixed default in config.py
|
||||
printer_config.save()
|
||||
|
||||
|
||||
def can_print():
|
||||
return printer_config.complete
|
||||
|
||||
|
||||
@app.route("/status", methods=["GET"])
|
||||
def printer_status_route():
|
||||
return jsonify(
|
||||
{
|
||||
"configured": can_print(),
|
||||
"connected": printer_config.printer.test_connection(),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
printer_config.load()
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
printer_config.address = "localhost"
|
||||
printer_config.port = 9100
|
||||
# printer_config.profile = ""
|
||||
|
||||
try:
|
||||
app.run(host=args.address, port=args.port)
|
||||
app.run(host=args.address, port=args.port, threaded=False)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user