Skip to content

Commit

Permalink
Merge pull request #399 from Stefal/use_gunicorn
Browse files Browse the repository at this point in the history
Use gunicorn and gevent
  • Loading branch information
Stefal authored Jun 10, 2024
2 parents 3c0c156 + 2b8a24e commit d57136d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Reverse proxy server with Rtkbase authentication, for Mosaic-X5 web interface
- Added description below form input. #381
### Changed
- Switch from eventlet to gevent + Gunicorn server.
### Deprecated
### Removed
### Fixed
Expand Down
8 changes: 3 additions & 5 deletions web_app/gnss_rproxy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
# Reverse proxy server to acces a Gnss receiver integrated Web Server (Mosaic-X5 or other)

import os
#from gevent import monkey
#monkey.patch_all()
import eventlet
eventlet.monkey_patch()
from eventlet import wsgi
from gevent import monkey
monkey.patch_all()
import requests

from RTKBaseConfigManager import RTKBaseConfigManager
Expand Down Expand Up @@ -167,6 +164,7 @@ def logout():
'bind': ['%s:%s' % ('0.0.0.0', rtkbaseconfig.get("main", "gnss_rcv_web_proxy_port", fallback=9090)),
'%s:%s' % ('[::1]', rtkbaseconfig.get("main", "gnss_rcv_web_proxy_port", fallback=9090)) ],
'workers': 1,
'worker_class': 'gevent',
'loglevel': 'warning',
}
#start gunicorn
Expand Down
4 changes: 2 additions & 2 deletions web_app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ cryptography==41.0.5;python_version<"3.8"
distro==1.8.0
dnspython==2.6.1;python_version>="3.8"
dnspython==2.3.0;python_version<"3.8"
eventlet==0.35.2
Flask==3.0.3;python_version>="3.8"
Flask==2.2.5;python_version<"3.8"
Flask-Login==0.6.3;python_version>="3.8"
Flask-Login==0.6.3;python_version<"3.8"
Flask-SocketIO==5.3.6
Flask-WTF==1.2.1;python_version>="3.8"
Flask-WTF==1.1.1;python_version<"3.8"
greenlet==3.0.1
gevent==24.2.2
gunicorn==22.0.0
h11==0.14.0
idna==3.7
importlib-metadata==6.7.0;python_version<"3.8"
Expand Down
35 changes: 29 additions & 6 deletions web_app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
# You should have received a copy of the GNU General Public License
# along with ReachView. If not, see <http://www.gnu.org/licenses/>.

#from gevent import monkey
#monkey.patch_all()
import eventlet
eventlet.monkey_patch()
from gevent import monkey
monkey.patch_all()

import time
import json
Expand Down Expand Up @@ -75,6 +73,7 @@
from werkzeug.security import check_password_hash
from werkzeug.utils import safe_join
import urllib
import gunicorn.app.base

app = Flask(__name__)
app.debug = False
Expand All @@ -90,7 +89,7 @@

login=LoginManager(app)
login.login_view = 'login_page'
socketio = SocketIO(app)
socketio = SocketIO(app, async_mode = 'gevent')
bootstrap = Bootstrap4(app)

#Get settings from settings.conf.default and settings.conf
Expand Down Expand Up @@ -119,6 +118,21 @@
rtkcv_standby_delay = 600
connected_clients = 0

class StandaloneApplication(gunicorn.app.base.BaseApplication):
def __init__(self, app, options=None):
self.options = options or {}
self.application = app
super().__init__()

def load_config(self):
config = {key: value for key, value in self.options.items()
if key in self.cfg.settings and value is not None}
for key, value in config.items():
self.cfg.set(key.lower(), value)

def load(self):
return self.application

class User(UserMixin):
""" Class for user authentification """
def __init__(self, username):
Expand Down Expand Up @@ -974,7 +988,16 @@ def arg_parse():
manager_thread.start()

app.secret_key = rtkbaseconfig.get_secret_key()
socketio.run(app, host = "::", port = args.port or rtkbaseconfig.get("general", "web_port", fallback=80), debug=args.debug) # IPv6 "::" is mapped to IPv4
#socketio.run(app, host = "::", port = args.port or rtkbaseconfig.get("general", "web_port", fallback=80), debug=args.debug) # IPv6 "::" is mapped to IPv4
gunicorn_options = {
'bind': ['%s:%s' % ('0.0.0.0', rtkbaseconfig.get("main", "web_port", fallback=80)),
'%s:%s' % ('[::1]', rtkbaseconfig.get("main", "web_port", fallback=80)) ],
'workers': 1,
'worker_class': 'gevent',
'loglevel': 'warning',
}
#start gunicorn
StandaloneApplication(app, gunicorn_options).run()

except KeyboardInterrupt:
print("Server interrupted by user!!")
Expand Down

0 comments on commit d57136d

Please sign in to comment.