|
@@ -1,10 +1,14 @@
|
|
|
+import eventlet
|
|
|
+import socketio
|
|
|
+
|
|
|
from glob import glob
|
|
|
-from os import path, getcwd
|
|
|
+from os import getcwd
|
|
|
+from os import path
|
|
|
from os.path import exists
|
|
|
+from logging.config import dictConfig
|
|
|
|
|
|
-import eventlet
|
|
|
-import socketio
|
|
|
-from flask import Flask, send_from_directory
|
|
|
+from flask import Flask
|
|
|
+from flask import send_from_directory
|
|
|
|
|
|
from pycs.database.Database import Database
|
|
|
from pycs.frontend.endpoints.ListJobs import ListJobs
|
|
@@ -55,56 +59,30 @@ class WebServer:
|
|
|
|
|
|
# pylint: disable=line-too-long
|
|
|
def __init__(self, settings: dict, database: Database, jobs: JobRunner):
|
|
|
+
|
|
|
+ dictConfig(settings["logging"])
|
|
|
+
|
|
|
# initialize web server
|
|
|
- if exists('webui/index.html'):
|
|
|
- print('production build')
|
|
|
-
|
|
|
- # find static files and folders
|
|
|
- static_files = {}
|
|
|
-
|
|
|
- for file_path in glob('webui/*'):
|
|
|
- file_path = file_path.replace('\\', '/')
|
|
|
- static_files[file_path[5:]] = file_path
|
|
|
-
|
|
|
- # separately add svg files and set their correct mime type
|
|
|
- for svg_path in glob('webui/img/*.svg'):
|
|
|
- svg_path = svg_path.replace('\\', '/')
|
|
|
- static_files[svg_path[5:]] = {'content_type': 'image/svg+xml', 'filename': svg_path}
|
|
|
-
|
|
|
- # create service objects
|
|
|
- if len(settings['allowedOrigins']) > 0:
|
|
|
- origins = settings['allowedOrigins']
|
|
|
- self.__sio = socketio.Server(cors_allowed_origins=origins, async_mode='eventlet')
|
|
|
- else:
|
|
|
- self.__sio = socketio.Server(async_mode='eventlet')
|
|
|
-
|
|
|
- self.__flask = Flask(__name__)
|
|
|
- self.__app = socketio.WSGIApp(self.__sio, self.__flask, static_files=static_files)
|
|
|
-
|
|
|
- # overwrite root path to serve index.html
|
|
|
- @self.__flask.route('/', methods=['GET'])
|
|
|
- def index():
|
|
|
- # pylint: disable=unused-variable
|
|
|
- return send_from_directory(path.join(getcwd(), 'webui'), 'index.html')
|
|
|
-
|
|
|
- else:
|
|
|
- print('development build')
|
|
|
-
|
|
|
- # create service objects
|
|
|
- self.__sio = socketio.Server(cors_allowed_origins='*', async_mode='eventlet')
|
|
|
- self.__flask = Flask(__name__)
|
|
|
- self.__app = socketio.WSGIApp(self.__sio, self.__flask)
|
|
|
-
|
|
|
- # set access control header to allow requests from Vue.js development server
|
|
|
- @self.__flask.after_request
|
|
|
- def after_request(response):
|
|
|
- # pylint: disable=unused-variable
|
|
|
- response.headers['Access-Control-Allow-Origin'] = '*'
|
|
|
- return response
|
|
|
+ self.__flask = Flask(__name__)
|
|
|
+
|
|
|
+ init_func = self.production_init if exists('webui/index.html') else self.development_init
|
|
|
+ kwargs, static_files = init_func()
|
|
|
+
|
|
|
+ self.__sio = socketio.Server(**kwargs)
|
|
|
+ self.__app = socketio.WSGIApp(self.__sio, self.__flask, static_files=static_files)
|
|
|
+ self.__host, self.__port = settings['host'], settings['port']
|
|
|
|
|
|
# set json encoder so database objects are serialized correctly
|
|
|
self.__flask.json_encoder = JSONEncoder
|
|
|
|
|
|
+ notifications = self.init_notifications(jobs)
|
|
|
+ self.define_routes(database, jobs, notifications)
|
|
|
+
|
|
|
+ @property
|
|
|
+ def logger(self):
|
|
|
+ return self.__flask.logger
|
|
|
+
|
|
|
+ def init_notifications(self, jobs):
|
|
|
# create notification manager
|
|
|
notifications = NotificationManager(self.__sio)
|
|
|
|
|
@@ -114,6 +92,55 @@ class WebServer:
|
|
|
jobs.on_finish(notifications.edit_job)
|
|
|
jobs.on_remove(notifications.remove_job)
|
|
|
|
|
|
+ return notifications
|
|
|
+
|
|
|
+ def development_init(self):
|
|
|
+
|
|
|
+ self.logger.info('development build')
|
|
|
+
|
|
|
+ # set access control header to allow requests from Vue.js development server
|
|
|
+ @self.__flask.after_request
|
|
|
+ def after_request(response):
|
|
|
+ # pylint: disable=unused-variable
|
|
|
+ response.headers['Access-Control-Allow-Origin'] = '*'
|
|
|
+ return response
|
|
|
+
|
|
|
+ return dict(cors_allowed_origins='*', async_mode='eventlet'), None
|
|
|
+
|
|
|
+ def production_init(self):
|
|
|
+
|
|
|
+ self.logger.info('production build')
|
|
|
+
|
|
|
+ kwargs = dict(async_mode='eventlet')
|
|
|
+ if len(settings['allowedOrigins']) > 0:
|
|
|
+ origins = settings['allowedOrigins']
|
|
|
+ kwargs["cors_allowed_origins"] = origins
|
|
|
+
|
|
|
+ # overwrite root path to serve index.html
|
|
|
+ @self.__flask.route('/', methods=['GET'])
|
|
|
+ def index():
|
|
|
+ # pylint: disable=unused-variable
|
|
|
+ return send_from_directory(path.join(getcwd(), 'webui'), 'index.html')
|
|
|
+
|
|
|
+ return kwargs, self.static_files
|
|
|
+
|
|
|
+ @property
|
|
|
+ def static_files(self) -> dict:
|
|
|
+ # find static files and folders
|
|
|
+ static_files = {}
|
|
|
+
|
|
|
+ for file_path in glob('webui/*'):
|
|
|
+ file_path = file_path.replace('\\', '/')
|
|
|
+ static_files[file_path[5:]] = file_path
|
|
|
+
|
|
|
+ # separately add svg files and set their correct mime type
|
|
|
+ for svg_path in glob('webui/img/*.svg'):
|
|
|
+ svg_path = svg_path.replace('\\', '/')
|
|
|
+ static_files[svg_path[5:]] = {'content_type': 'image/svg+xml', 'filename': svg_path}
|
|
|
+
|
|
|
+ return static_files
|
|
|
+
|
|
|
+ def define_routes(self, database, jobs, notifications):
|
|
|
# additional
|
|
|
self.__flask.add_url_rule(
|
|
|
'/folder',
|
|
@@ -280,5 +307,5 @@ class WebServer:
|
|
|
view_func=PredictFile.as_view('predict_file', database, notifications, jobs)
|
|
|
)
|
|
|
|
|
|
- # finally start web server
|
|
|
- eventlet.wsgi.server(eventlet.listen((settings['host'], settings['port'])), self.__app)
|
|
|
+ def run(self):
|
|
|
+ eventlet.wsgi.server(eventlet.listen((self.__host, self.__port)), self.__app)
|