diff --git a/config.yaml b/config.yaml index d1fd2c1..cc4bb43 100644 --- a/config.yaml +++ b/config.yaml @@ -1,4 +1,7 @@ check-interval: 1 +status-server: + host: 127.0.0.1 + port: 8090 timers: gaming: apps: diff --git a/lib/__init__.py b/lib/__init__.py index aa37724..982709b 100644 --- a/lib/__init__.py +++ b/lib/__init__.py @@ -253,3 +253,21 @@ class Config(object): '''How often the usage check should happen''' interval = self.data.get('check-interval', 1) return int(interval) + + @property + def statusServer(self): + '''Optional host/port overrides for the embedded status server''' + config = self.data.get('status-server') or {} + host = config.get('host') + port = config.get('port') + if port is None: + parsed_port = None + else: + try: + parsed_port = int(port) + except (TypeError, ValueError): + raise ValueError('status-server.port must be an integer') + return { + 'host': host, + 'port': parsed_port, + } diff --git a/readme.md b/readme.md index fcc2935..b12e70a 100644 --- a/readme.md +++ b/readme.md @@ -11,6 +11,10 @@ I'm using this at a raspberry pi gaming station (retropie) to enforce usage limi ```yaml # how often should the script check for apps runtime? check-interval: 1 +# optional http status endpoint configuration +status-server: + host: 127.0.0.1 + port: 8090 # timers: list of apps that will be watched timers: # the app name (list key) @@ -40,6 +44,8 @@ warn-command: > runuser -l randogoth -c 'DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "App Timer" "{timer_name}: {time_left_int} minutes left"' ``` +The status server shows timer usage at `http://host:port/` (defaults `127.0.0.1:8090`). Override the bind address or port via the `status-server` block, or keep using the `APP_TIMER_STATUS_HOST` / `APP_TIMER_STATUS_PORT` environment variables if you prefer exporting them in your service unit. + ## Installation There's a systemd service file ready for use. diff --git a/timer.py b/timer.py index 20859b0..6fbcf1c 100644 --- a/timer.py +++ b/timer.py @@ -6,9 +6,6 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from lib import Config -STATUS_SERVER_HOST = os.environ.get('APP_TIMER_STATUS_HOST', '127.0.0.1') -STATUS_SERVER_PORT = int(os.environ.get('APP_TIMER_STATUS_PORT', '8090')) - STATUS_CONTEXT = {'config': None} @@ -166,15 +163,32 @@ class StatusRequestHandler(BaseHTTPRequestHandler): print("HTTP %s - %s" % (self.log_date_time_string(), format % args)) -def start_status_server(): +def _resolve_status_server_bindings(config): + server_config = getattr(config, 'statusServer', {}) or {} + env_host = os.environ.get('APP_TIMER_STATUS_HOST') + env_port = os.environ.get('APP_TIMER_STATUS_PORT') + host = env_host or server_config.get('host') or '127.0.0.1' + if env_port is not None: + try: + port = int(env_port) + except ValueError: + raise ValueError('APP_TIMER_STATUS_PORT must be an integer') + elif server_config.get('port') is not None: + port = server_config['port'] + else: + port = 8090 + return host, port + + +def start_status_server(host, port): try: - httpd = ThreadingHTTPServer((STATUS_SERVER_HOST, STATUS_SERVER_PORT), StatusRequestHandler) + httpd = ThreadingHTTPServer((host, port), StatusRequestHandler) except OSError as exc: - print('Failed to start status server on %s:%s (%s)' % (STATUS_SERVER_HOST, STATUS_SERVER_PORT, exc)) + print('Failed to start status server on %s:%s (%s)' % (host, port, exc)) return None thread = threading.Thread(target=httpd.serve_forever, daemon=True) thread.start() - print('Status server running on http://%s:%s' % (STATUS_SERVER_HOST, STATUS_SERVER_PORT)) + print('Status server running on http://%s:%s' % (host, port)) return httpd def check_timers(config): @@ -197,7 +211,8 @@ def check_timers(config): config = Config() STATUS_CONTEXT['config'] = config -status_server = start_status_server() +status_host, status_port = _resolve_status_server_bindings(config) +status_server = start_status_server(status_host, status_port) while True: # check config changes if config.hasChanges():