File size: 4,338 Bytes
ae4627d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | #!/usr/bin/env python
"""
main.py
ist303-miye
Copyright (C) 2017
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
"""
import support.pyinstaller_hack
import config
import client
import assets
import controllers
import support
from waitress import serve
from pyramid.config import Configurator
import threading as t
from support.events import GracefulShutdown
import pyramid_chameleon
def create_app_config():
"""
Creates Pyramid config
"""
config = Configurator()
config.include('pyramid_chameleon')
config.add_static_view(name='/css', path='assets:static/css')
config.add_static_view(name='/scripts', path='assets:static/scripts')
config.add_static_view(name='/fonts', path='assets:static/fonts')
config.add_route('home', '/')
config.add_route('reservation', '/reservation')
config.add_route('checkinout', '/checkinout')
config.add_route('checkinout_client', '/checkinout/{selected_action}/{selected_id}')
config.add_route('editres_view', '/editres/{res_id}')
config.add_route('view_bill', '/bill/{res_id}')
config.add_route('view_checkin_confirmation', '/checkin/{res_id}')
config.add_route('spa_reservation', '/spa_reservation')
config.add_route('directions', '/directions')
config.scan(controllers)
return config
def create_wsgi_app(config = create_app_config()):
"""
Creates WSGI application. Necessary to run in debug mode, i.e. by
waitress-serve --listen=*:8041 main:create_wsgi_app
"""
app = config.make_wsgi_app()
return app
class Server(object):
def __init__(self, host='127.0.0.1', port=8080):
"""
Initializes the server and required multi-threading vars
"""
self._host = host
self._port = port
self._config = None
self._app = None
# Create condition to see if the server is ready
self._ready_cv = t.Condition()
self._ready = False
# Create server thread
self._thread = t.Thread(target=self.server_worker, daemon=True)
def start_server(self):
"""
Starts the server thread
"""
self._thread.start()
def wait_for_server(self):
"""
Waits till the server is completely ready to serve data
"""
with self._ready_cv:
while not self._ready:
self._ready_cv.wait()
def server_worker(self):
"""
Initializes the server, notifies waiter that it is ready and continues to work
"""
self._config = create_app_config()
self._app = create_wsgi_app(self._config)
with self._ready_cv:
self._ready = True
self._ready_cv.notify_all()
serve(self._app, host=self._host, port=self._port)
def stop_server(self):
"""
Stops the server
"""
# Send GracefulShutdown event
gs = GracefulShutdown()
self._config.registry.notify(gs)
def start_client(host, port):
client.webview_start("http://%s:%d/" % (host, port))
if __name__ == '__main__':
server_host = '127.0.0.1'
port_number = 48552
# Start the server
srv = Server(server_host, port_number)
srv.start_server()
# Wait for the server startup
srv.wait_for_server()
# Now we can start the client
# This one usually requires main thread, since it works with GUI
start_client(server_host, port_number)
# When start_client() returns, it means it was closed. Stop the server.
srv.stop_server()
|