Skip to content
Snippets Groups Projects
Commit 7edd4f1e authored by Markus Willman's avatar Markus Willman
Browse files

make the flask app work with uwsgi

parent 59d4496d
No related branches found
No related tags found
No related merge requests found
......@@ -5,18 +5,19 @@ MAINTAINER Markus Willman
SHELL ["/bin/bash", "-c"]
RUN apt-get update -qqy && apt-get upgrade -qqy --fix-missing \
&& apt-get install -qqy --no-install-recommends python3 python3-dev python3-pip python3-wheel python3-setuptools supervisor \
&& pip3 install virtualenv
&& apt-get install -qqy --no-install-recommends build-essential python3 python3-dev python3-pip python3-wheel python3-setuptools supervisor \
&& pip3 install virtualenv uwsgi
COPY conf/nginx/nginx.conf /etc/nginx/nginx.conf
COPY conf/nginx/default.conf /etc/nginx/conf.d/default.conf
COPY conf/supervisor/nginx.conf /etc/supervisor/conf.d/nginx.conf
COPY conf/supervisor/smartbusstop.conf /etc/supervisor/conf.d/smartbusstop.conf
COPY conf/supervisor/nginx.conf /etc/supervisor/conf.d/01_nginx.conf
COPY conf/supervisor/smartbusstop.conf /etc/supervisor/conf.d/02_smartbusstop.conf
RUN mkdir -p /opt/services/smartbusstop
RUN mkdir -p /opt/virtualenvs
COPY requirements.txt /opt/services/smartbusstop/requirements.txt
COPY data /opt/services/smartbusstop/data
COPY server /opt/services/smartbusstop/server
COPY static /opt/services/smartbusstop/static
......
......@@ -11,12 +11,13 @@ commands
```shell
$ pip install -r requirements.txt
$ python server -P <port> -H <host> -d
$ python -m server -P <port> -H <host> -d
```
Running the second command with no arguments will bind to localhost at port 5000
with debugging features disabled. Running with the -d switch (for debug) during
development is recommended due to autoreloader and better error reporing.
development is recommended for meaningful error reporing. Debugging allows running
arbitrary python code from browser, so it should never be used on public instances.
### Using docker to test changes locally
......
......@@ -33,11 +33,20 @@ server {
}
location / {
proxy_pass http://127.0.0.1:5000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
# run on the loopback interface
# proxy_redirect off;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header X-Forwarded-Host $server_name;
# proxy_pass http://127.0.0.1:5000;
# run through unix socket using uwsgi
include uwsgi_params;
uwsgi_param Host $host;
uwsgi_param X-Real-IP $remote_addr;
uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
uwsgi_param X-Forwarded-Host $server_name;
uwsgi_pass unix:/tmp/smartbusstop.sock;
}
}
[program:nginx]
user=root
command=/usr/sbin/nginx -g "daemon off;"
autostart=true
autorestart=true
user=root
priority=400
stderr_logfile=/var/log/nginx/stderr.log
[program:smartbusstop]
user=root
directory=/opt/services/smartbusstop/
command=/opt/virtualenvs/smartbusstop/bin/python server -H 127.0.0.1
; run using loopback interface
; command=/opt/virtualenvs/smartbusstop/bin/python -m server -H 127.0.0.1
; run through unix socket using uwsgi
command=/usr/local/bin/uwsgi -s /tmp/smartbusstop.sock --chown-socket=nginx:nginx --chmod-socket=664 --plugin python3 -H /opt/virtualenvs/smartbusstop --manage-script-name --mount /=server:application
stopsignal=QUIT
autostart=true
autorestart=true
redirect_stderr=true
__all__ = ['app', 'dataHandler', 'staticVariables']
from .app import app as application
from .app import main
#!/usr/bin/python
from app import main
import sys
if sys.path[0] == '' and __package__ != None:
from .app import main
main()
else:
print(" * Run the server using \"python -m server [...]\" or uwsgi")
exit()
import optparse, os
from staticVariables import TEMPLATE_DIR, STATIC_DIR, DATA_DIR, DEFAULT_STOP, DEFAULT_HOST, DEFAULT_PORT
from dataHandler import loadStop, loadJson, hasTemplate, hasData
from flask import Flask, render_template, abort, url_for, redirect, send_from_directory, jsonify
from .staticVariables import TEMPLATE_DIR, STATIC_DIR, DATA_DIR, DEFAULT_STOP, DEFAULT_HOST, DEFAULT_PORT
from .dataHandler import loadStop, loadJson, hasTemplate, hasData
app = Flask(__name__, template_folder = TEMPLATE_DIR, static_folder = STATIC_DIR)
@app.context_processor
......@@ -106,8 +106,6 @@ def main():
app.run(
debug = options.debug,
host = options.host,
port = int(options.port)
port = int(options.port),
use_reloader = False # unfortunately the reloader breaks when running with python -m
)
if __name__ == "__main__":
main()
import os, json
from staticVariables import DATA_DIR, TEMPLATE_DIR
from flask import url_for
from .staticVariables import DATA_DIR, TEMPLATE_DIR
# underscore as a convention for "private" variables (may or may not affect wildcard imports)
_stops = None
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment