This repository has been archived by the owner on May 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 429
/
main.py
80 lines (64 loc) · 2.29 KB
/
main.py
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
# coding: utf-8
#
import argparse
import socket
from pprint import pprint
import tornado.ioloop
from logzero import logger
from rethinkdb import r
from tornado.httpserver import HTTPServer
from tornado.log import enable_pretty_logging
from web.database import db
from web.entry import make_app
from web.views import OpenIdLoginHandler, SimpleLoginHandler, GithubLoginHandler
def machine_ip():
""" return current machine ip """
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
try:
return s.getsockname()[0]
finally:
s.close()
def main():
_auth_handlers = {
"simple": SimpleLoginHandler,
"openid": OpenIdLoginHandler,
"github": GithubLoginHandler
}
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# yapf: disable
parser.add_argument('-p', '--port', type=int,
default=4000, help='listen port')
parser.add_argument('-d', '--debug', action='store_true',
help='open debug log, and open hot reload')
parser.add_argument('--auth', type=str, default='simple',
choices=_auth_handlers.keys(), help='authentication method')
parser.add_argument("--no-xheaders", action="store_true",
help="disable support for X-Real-Ip/X-Forwarded-For")
parser.add_argument(
'--auth-conf-file', type=argparse.FileType('r'), help='authentication config file')
# yapf: enable
args = parser.parse_args()
print(args)
enable_pretty_logging()
db.setup()
ioloop = tornado.ioloop.IOLoop.current()
# TODO(ssx): for debug use
# async def dbtest():
# items = await db.table("devices").get_all(
# limit=2, rsql_hook=lambda q: q.order_by(r.desc("createdAt")))
# for item in items:
# pprint(item)
# ioloop.spawn_callback(dbtest)
login_handler = _auth_handlers[args.auth]
app = make_app(login_handler, debug=args.debug)
server = HTTPServer(app, xheaders=not args.no_xheaders)
server.listen(args.port)
logger.info("listen on port http://%s:%d", machine_ip(), args.port)
try:
ioloop.start()
except KeyboardInterrupt:
ioloop.stop()
if __name__ == "__main__":
main()