Merge pull request #2 from mxmehl/fix-gunicorn

Use gunicorn programmatically
This commit is contained in:
Max Mehl
2025-04-01 22:35:26 +02:00
committed by GitHub
2 changed files with 17 additions and 15 deletions

View File

@@ -148,6 +148,7 @@ def main():
) )
parser.add_argument("--host", default="localhost", help="Hostname of the server") parser.add_argument("--host", default="localhost", help="Hostname of the server")
parser.add_argument("-p", "--port", type=int, default=8000, help="Port of the server") parser.add_argument("-p", "--port", type=int, default=8000, help="Port of the server")
parser.add_argument("-w", "--workers", type=int, default=4, help="Gunicorn webserver workers")
parser.add_argument( parser.add_argument(
"-vv", "-vv",
"--debug", "--debug",
@@ -162,7 +163,9 @@ def main():
app = create_app(config_path=os.path.abspath(args.config_file)) app = create_app(config_path=os.path.abspath(args.config_file))
app.run(debug=args.debug, host=args.host, port=args.port) app.run(debug=args.debug, host=args.host, port=args.port)
else: else:
serve_via_gunicorn(config_file=args.config_file) serve_via_gunicorn(
config_file=args.config_file, host=args.host, port=args.port, workers=args.workers
)
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -7,11 +7,12 @@
import hashlib import hashlib
import hmac import hmac
import os import os
import subprocess import sys
import yaml import yaml
from bcrypt import checkpw from bcrypt import checkpw
from flask import Flask, abort, current_app, request from flask import Flask, abort, current_app, request
from gunicorn.app.wsgiapp import WSGIApplication
def load_config(app: Flask, filename: str) -> None: def load_config(app: Flask, filename: str) -> None:
@@ -71,16 +72,14 @@ def get_stream_token(username: str) -> str:
return hmac.new(secret.encode(), username.encode(), hashlib.sha256).hexdigest()[:16] return hmac.new(secret.encode(), username.encode(), hashlib.sha256).hexdigest()[:16]
def serve_via_gunicorn(config_file: str) -> None: def serve_via_gunicorn(config_file: str, host: str, port: int, workers: int) -> None:
"""Serve the application using Gunicorn.""" """Serve the application using Gunicorn programmatically."""
subprocess.run( sys.argv = [
[
"gunicorn", "gunicorn",
"-w", "-w",
"4", str(workers),
"-b", "-b",
"0.0.0.0:8000", f"{host}:{port}",
f"home_stream.wsgi:create_app('{config_file}')", f"home_stream.wsgi:create_app('{config_file}')",
], ]
check=True, WSGIApplication().run()
)