From f672c6c70619c9148fbb57a203e85777c2363059 Mon Sep 17 00:00:00 2001 From: Prunebutt Date: Fri, 26 Sep 2025 00:26:43 +0200 Subject: [PATCH] add a flask server --- .gitignore | 1 + flake.nix | 49 ++++++++++++++++++++++++++++++++++++++++++++----- server.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 5 deletions(-) create mode 100755 server.py diff --git a/.gitignore b/.gitignore index a136337..079dad0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.pdf +__pycache__ diff --git a/flake.nix b/flake.nix index 9b979ec..ef747b6 100644 --- a/flake.nix +++ b/flake.nix @@ -7,14 +7,53 @@ nixpkgs, } @ inputs: let system = "x86_64-linux"; - pkgs = import nixpkgs { inherit system; config.allowUnfree = true;}; + pkgs = import nixpkgs { + inherit system; + config.allowUnfree = true; + }; in { - packages."${system}".default = pkgs.writeShellApplication { - name = "hello_world"; + devShells.${system}.default = pkgs.mkShell { + packages = with pkgs; [ + python313 + python313Packages.flask + python313Packages.waitress + ]; - runtimeInputs = with pkgs; [sane-backends brscan5 ghostscript]; + shellHook = '' + python --version + exec zsh + ''; + }; + packages.${system} = let + scanbuddy = pkgs.writeShellApplication { + name = "scanbuddy-script"; - text = builtins.readFile ./scanbuddy.bash; + runtimeInputs = with pkgs; [sane-backends brscan5 ghostscript]; + + text = builtins.readFile ./scanbuddy.bash; + }; + wrapper = pkgs.writeShellApplication { + name = "scanbuddy-wrapper"; + + text = + /* + bash + */ + '' + #!/usr/bin/env bash + readarray -d '_' args < <(printf "%s" "$1") + + scanbuddy "''${args[@]}" + ''; + }; + package = pkgs.symlinkJoin { + name = "scanbuddy"; + paths = [scanbuddy wrapper]; + }; + in { + default = package; + inherit scanbuddy; + inherit wrapper; }; }; } diff --git a/server.py b/server.py new file mode 100755 index 0000000..0259f6f --- /dev/null +++ b/server.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +# Run this stuff with `waitress-serve --port=5000 --call server:create_app` + +import os +import subprocess +from flask import Flask, request + +app = Flask(__name__) + +@app.route("/") +def hello_world(): + return "Hello, World! Yoyoyo" + +@app.route("/scan") +def scan(): + command = ["bash", f"{os.getcwd()}/scanbuddy.bash", "scan"] + + command += ["-d"] if (request.args.get("duplex", "n") == 'y') else [] + + result = subprocess.run(command, stdout=subprocess.PIPE, text=True) + output = result.stdout.strip() + + print("Scanbuddy output: ") + print(output) + + return output + # return f"Scanning {'duplex' if duplex == 'y' else ''}..." + +@app.route("/dispatch") +def dispatch(): + return "Dispatching..." + +@app.route("/unscan") +def unscan(): + return "Unscanning..." + +def create_app(): + return app + +if __name__=='__main__': + from waitress import serve + serve(app, host="0.0.0.0", port=8080)