-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
116 lines (100 loc) · 4.07 KB
/
server.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Import global settings
import settings as gsettings
from lib.base import BaseHandler, BaseStaticFileHandler
# Private new handler
from lib.book import V1_AddBookHandler
# Private old handler
from lib.book import (GetHandler, EditHandler,
DeleteHandler)
from lib.auth import (AuthHandler, LoginHandler,
LogoutHandler, RegisterHandler,
NologinHandler)
from lib.wunderlist import (WunEditHandler, GetWunBooksHandler,
VoteHandler)
from lib.search import SearchHandler
from lib.borrow import BorrowBookHandler
from lib import superuuid
# Public module
import tornado.locale
import tornado.httpserver
import tornado.ioloop
import tornado.web as web
import pymongo
from tornado.options import define, options
# Update some options with providing command arguments
# can make debug easier
# Define server
define("port", default=gsettings.LISTEN_PORT, type=int, help="run on the given port")
# Define mongodb server
define("mongodb_host", default=gsettings.MONGODB_HOST, help="mongodb host")
define("mongodb_port", default=gsettings.MONGODB_PORT, help="mongodb port")
# Define DB
define("db_continue", default=gsettings.DB_CONTINUE, help="database name")
class Application(tornado.web.Application):
def __init__(self):
# Load global settings, then this variable-gsettings can be
# used in tornado.web.RequestHandler with self.application.gsettings
# and in tornado.web.Application with self.gsettings
self.gsettings = gsettings
handlers = [
# Handler first request
(r"/", MainHandler),
# (r"/(.*\.html)", web.StaticFileHandler, {"path": "public"}),
(r"/(.*\.html)", HtmlHandler),
# Old API
(r"/book/get", GetHandler),
(r"/book/edit", EditHandler),
(r"/book/delete", DeleteHandler),
(r"/book/search", SearchHandler),
(r"/auth/", AuthHandler),
(r"/auth/login", LoginHandler),
(r"/auth/nologin", NologinHandler),
(r"/auth/logout", LogoutHandler),
(r"/auth/register", RegisterHandler),
(r"/wunderlist/get", GetWunBooksHandler),
(r"/wunderlist/edit", WunEditHandler),
(r"/wunderlist/vote", VoteHandler),
# RESTful API
(r"/v1/books/", V1_AddBookHandler),
(r"/book/borrowing", BorrowBookHandler)
# Add new Handler HERE
]
# These settings are for the tornado internal settings
# and they should refer to global settings
settings = {
"login_url": self.gsettings.LOGIN_URL,
"xsrf_cookies": self.gsettings.XSRF_COOKIES,
# Suite to one instance
"cookie_secret": superuuid.generate(),
# favicon.ico robots.txt also direct into here
"static_url_prefix": self.gsettings.STATIC_URL_PREFIX,
"static_path": self.gsettings.STATIC_PATH,
"static_handler_class": BaseStaticFileHandler,
"debug": self.gsettings.DEBUG
}
tornado.web.Application.__init__(self, handlers, **settings)
# Init database
conn = pymongo.MongoClient(options.mongodb_host,
options.mongodb_port)
self.db = conn[options.db_continue]
class MainHandler(BaseHandler):
def get(self):
if self.get_current_user():
self.redirect("/static/donate.html")
else:
self.redirect("/static/login.html")
class HtmlHandler(BaseHandler):
def get(self, html_name):
self.redirect("/static/" + html_name)
def main():
# If DEBUG, then use pretty console log
if gsettings.DEBUG is False:
tornado.options.options.log_file_prefix = gsettings.LOG_FILE_PREFIX
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port, address=gsettings.LISTEN_ADDRESS)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()