aboutsummaryrefslogtreecommitdiffstats
path: root/bin/fg21sim-webui
blob: 8fc76731442f05682b13d3fb018b1c6097a1787d (plain)
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
#!/usr/bin/env python3
# -*- mode: python -*-
#
# Copyright (c) 2016 Weitian LI <liweitianux@live.com>
# MIT license

"""
Start (and/or control) the Web user interface (UI) of "fg21sim",
which is built using the Tornado_ web server and WebSocket_ protocol.

.. _Tornado: http://www.tornadoweb.org/

.. _WebSocket: https://en.wikipedia.org/wiki/WebSocket ,
   http://caniuse.com/#feat=websockets
"""


import os
import sys
import argparse
import logging

import tornado.ioloop

from fg21sim.configs import configs
from fg21sim.utils import setup_logging
from fg21sim.webui import make_application


def main():
    parser = argparse.ArgumentParser(description="Start the fg21sim Web UI")
    parser.add_argument("-p", "--port", type=int, default=21127,
                        help="Tornado server listen port (default: 21127)")
    parser.add_argument("-d", "--debug", action="store_true",
                        help="enable Tornado debug mode")
    args = parser.parse_args()

    loglevel = "DEBUG" if args.debug else None
    setup_logging(dict_config=configs.logging, level=loglevel)
    tool = os.path.basename(sys.argv[0])
    logger = logging.getLogger(tool)
    logger.info("COMMAND: {0}".format(" ".join(sys.argv)))

    application = make_application(debug=args.debug)
    application.listen(args.port)
    logger.info("Tornado started on: {protocol}://{host}:{port}".format(
        protocol="http", host="localhost", port=args.port))
    tornado.ioloop.IOLoop.current().start()


if __name__ == "__main__":
    main()