summaryrefslogtreecommitdiff
path: root/lorry-controller-webapp
diff options
context:
space:
mode:
Diffstat (limited to 'lorry-controller-webapp')
-rwxr-xr-xlorry-controller-webapp31
1 files changed, 25 insertions, 6 deletions
diff --git a/lorry-controller-webapp b/lorry-controller-webapp
index 6acbac0..9064d17 100755
--- a/lorry-controller-webapp
+++ b/lorry-controller-webapp
@@ -20,6 +20,7 @@ import logging
import os
import sqlite3
import time
+import wsgiref.simple_server
import bottle
import cliapp
@@ -132,11 +133,12 @@ class WEBAPP(cliapp.Application):
['wsgi'],
'run in wsgi mode (default is debug mode, for development)')
- self.settings.integer(
- ['debug-port'],
- 'listen on PORT when in debug mode (i.e., not running under WSGI)',
- metavar='PORT',
- default=8888)
+ self.settings.string(
+ ['debug-port-file'],
+ 'write listening port to FILE when in debug mode '
+ '(i.e., not running under WSGI)',
+ metavar='FILE',
+ default='webapp.port')
self.settings.string(
['debug-host'],
@@ -182,10 +184,27 @@ class WEBAPP(cliapp.Application):
if self.settings['wsgi']:
WSGIServer(webapp).run()
else:
+ server_port_file = self.settings['debug-port-file']
+ class DebugServer(wsgiref.simple_server.WSGIServer):
+ '''WSGI-like server that uses an ephemeral port.
+
+ Rather than use a specified port, or default, the
+ DebugServer connects to an ephemeral port and writes
+ its number to debug-port-file, so a non-racy temporary
+ port can be used.
+
+ '''
+
+ def __init__(self, (host, port), *args, **kwargs):
+ wsgiref.simple_server.WSGIServer.__init__(
+ self, (host, 0), *args, **kwargs)
+ with open(server_port_file, 'w') as f:
+ f.write(str(self.server_port) + '\n')
+
bottle.run(
webapp,
host=self.settings['debug-host'],
- port=self.settings['debug-port'],
+ server_class=DebugServer,
quiet=True,
debug=True)