PenningLabs / lxconsole

LXD Graphical Web Console
GNU Affero General Public License v3.0
161 stars 19 forks source link

[Enhancement] : Allow setting (overriding) host & port #13

Open marcus-j-davies opened 10 months ago

marcus-j-davies commented 10 months ago

I am using this on a MAC, where port 5000 was in use. So, to allow control over this. I think allowing overrides with Environment Vars or Arguments.

Arguments should take priority over Environment Vars

I don't have the means to PR this at the moment, so here is a code snippet (run.py)

from lxconsole import app
import os
import argparse

parser=argparse.ArgumentParser(description="LXConsole Args Parser")
parser.add_argument('--port', type=int) 
parser.add_argument('--host', type=str) 
args=parser.parse_args()

# Use environment vars for the server (defaulting if not provided)
port = os.environ.get("PORT") or 5000
host = os.environ.get("HOST") or '0.0.0.0'

# Last chance to override the server address
port = args.port or port
host = args.host or host

if __name__ == '__main__':
    app.run(debug=False, host=host, port=port)

Examples:

# Args
python3 run.py --host 0.0.0.0 --port 9090
# Environment Vars
export PORT=6000 HOST=127.0.0.1 && python3 run.py 
# Args take priority over Environment Vars
export PORT=6000 HOST=127.0.0.1 && python3 run.py --port 7000
matthewalanpenning commented 9 months ago

Thank you for this suggestion. This is a good idea to alllow args and env vars and I will look to implement this in an upcoming update. I am also glad to hear that it works in on the Mac platform. I have not tested it there.

skug67 commented 8 months ago

In a similar vein, it would be great to an env var (or otherwise) to run the application at a suburl (e.g., https://site.example.com/lxconsole) -- whether directly or as a wsgi application under nginx or apache. It looks like the templates have a whole bunch of hard coded links to "../static" which currently prevent that even if you change or wrap the flask routes themselves.

UPDATE -- I messed around with setting "SCRIPT_NAME" environment variable and running the app via gunicorn. And, indeed, all those "../static" links seem to be preventing that from working. Per my very tenuous understanding gleaned from Google (e.g., https://wlog.viltstigen.se/articles/2021/09/13/flask-application-behind-a-reverse-proxy/ and https://github.com/openanalytics/shinyproxy-flask-demo), it seems like that should be solvable by using "url_for" in all of the templates. Unfortunately, when I tried to head down that path via sed, I couldn't quite pull it all together (I made some progress by replacing the "link rel href" lines for stylesheets, but when I tried to move on to .js scripts and img/svg references everything sort of fell apart on me.)

matthewalanpenning commented 8 months ago

@marcus-j-davies the latest update now allows for FLASK_RUN_PORT and FLASK_RUN_HOST environment variables as well as --host and --port command-line args.

matthewalanpenning commented 8 months ago

@skug67 I can update the url_for options and am looking into getting this redirect option working on my end.

matthewalanpenning commented 7 months ago

@skug67 Version 0.4.7 should now work for you. Here is what I used in my NGINX config.

server {
...
  location /test/ {
    proxy_pass http://127.0.0.1:8001;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Prefix /;
  }
}

And then I ran the application with gunicorn passing in the SCRIPT_NAME var to match the location gunicorn -e SCRIPT_NAME=/test -b 0.0.0.0:8001 run:app