Skip to content

Commit

Permalink
feat: make it possible to hide the simulator URL, or configure it
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Feb 24, 2022
1 parent c22c3e7 commit 5533c29
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
44 changes: 43 additions & 1 deletion doppelgaenger-websocket/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import os
from typing import Any
import signal
import tornado.websocket
import tornado.httpserver
import tornado.ioloop
Expand All @@ -10,6 +10,8 @@
from logzero import logger
from bson import json_util
from healthcheck import TornadoHandler, HealthCheck
import time


health = HealthCheck()

Expand Down Expand Up @@ -66,6 +68,36 @@ async def watch(collection):
ChangesHandler.on_change(change)


class HomeHandler(tornado.web.RequestHandler):
def get(self):
self.render("templates/index.html", simulator_url=os.getenv("SIMULATOR_URL", ""))


def sig_handler(sig, frame):
logger.warning('Caught signal: %s', sig)
tornado.ioloop.IOLoop.instance().add_callback(shutdown)

MAX_WAIT_SECONDS_BEFORE_SHUTDOWN = 3

def shutdown():
logger.info('Stopping http server')
server.stop()

logger.info('Will shutdown in %s seconds ...', MAX_WAIT_SECONDS_BEFORE_SHUTDOWN)
io_loop = tornado.ioloop.IOLoop.instance()

deadline = time.time() + MAX_WAIT_SECONDS_BEFORE_SHUTDOWN

def stop_loop():
now = time.time()
if now < deadline and (io_loop._callbacks or io_loop._timeouts):
io_loop.add_timeout(now + 1, stop_loop)
else:
io_loop.stop()
logging.info('Shutdown')
stop_loop()


def main():
load_dotenv()

Expand All @@ -75,13 +107,18 @@ def main():
client = MotorClient(os.environ["MONGODB__URL"])
collection = client[database][application]

global app

app = tornado.web.Application(
[
(r"/socket", ChangesHandler),
(
"/health",
TornadoHandler, dict(checker=health)
),
(
r"/", HomeHandler
),
(
r"/(.*)", tornado.web.StaticFileHandler,
{"path": "templates/", "default_filename": "index.html"}
Expand All @@ -92,6 +129,9 @@ def main():

app.listen(8082)

signal.signal(signal.SIGTERM, sig_handler)
signal.signal(signal.SIGINT, sig_handler)

loop = tornado.ioloop.IOLoop.current()
loop.add_callback(watch, collection)

Expand All @@ -103,6 +143,8 @@ def main():
if change_stream is not None:
change_stream.close()

logger.wan("Exiting...")


if __name__ == "__main__":
main()
4 changes: 3 additions & 1 deletion doppelgaenger-websocket/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ <h1>Dashboard example</h1>
<ul class="list-group list-group-flush">
<li class="list-group-item">
<strong>State:</strong> <span id="state">?</span>
<span class="float-end"><a href="https://device-simulator-burrboard.apps.wonderful.iot-playground.org/" target="_blank">Open Simulator</a> </span>
{% if simulator_url != "" %}
<span class="float-end"><a href="{{ simulator_url }}" target="_blank">Open Simulator</a> </span>
{% end %}
</li>
<li class="list-group-item" id="last-message-container">
<code id="last-message"></code>
Expand Down

0 comments on commit 5533c29

Please sign in to comment.