forked from shiyanhui/Young
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
111 lines (80 loc) · 2.95 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
# -*- coding: utf-8 -*-
import sys
import os
from signal import signal, SIGINT
import nsq
from monguo.connection import Connection
from tornado.ioloop import IOLoop
from tornado.web import Application
from tornado.options import define, options, parse_command_line
from tornado.httpserver import HTTPServer
from elasticsearch import Elasticsearch
from torsession.sync import SessionManager
from young import setting, urlmap
from lib.message import (
WriterManager, MessageTopic, chat_message_handler,
send_activation_email_handler, send_reset_password_email_handler,
send_has_unread_message_email_handler, message_handler
)
def register_message_writers():
'''为nsq消息系统建立生产者'''
WriterManager.writer = nsq.Writer(['127.0.0.1:4150'])
def register_message_readers():
'''为nsq消息系统建立消费者'''
for topic in MessageTopic.all_topic:
handler = None
if topic == MessageTopic.CHAT_MESSAGE_NEW:
handler = chat_message_handler
elif topic == MessageTopic.SEND_ACTIVATION_EMAIL:
handler = send_activation_email_handler
elif topic == MessageTopic.SEND_RESET_PASSWORD_EMAIL:
handler = send_reset_password_email_handler
elif topic == MessageTopic.SEND_HAS_UNREAD_MESSAGE_EMAIL:
handler = send_has_unread_message_email_handler
else:
handler = message_handler
if handler is not None:
reader = {
'message_handler': handler,
"nsqd_tcp_addresses": ['127.0.0.1:4150'],
'topic': topic,
'channel': topic,
'lookupd_poll_interval': 15
}
nsq.Reader(**reader)
# controlling is in c, Crtl^C won't exit, so we should kill self
def suicide(signum, frame):
os.system("kill -9 %d" % os.getpid())
def runserver():
'''启动服务器'''
signal(SIGINT, suicide)
# for sleekxmpp
reload(sys)
sys.setdefaultencoding('utf8')
define('port', default=8000, help='run on the given port', type=int)
define("database", default="Young", help="database to use", type=str)
define("debug", default=False, help="whether is debug mode", type=bool)
parse_command_line()
register_message_writers()
register_message_readers()
Connection.connect(options.database)
setting.APPLICATION_SETTINGS.update({
"debug": options.debug,
"template_path": os.path.join(
setting.ROOT_LOCATION,
"app" if options.debug else "templates"
),
"es": Elasticsearch(),
"session_manager": SessionManager(
Connection.get_database(pymongo=True).session
)
})
application = Application(
handlers=urlmap.urlpattern,
**setting.APPLICATION_SETTINGS
)
http_server = HTTPServer(application, xheaders=True)
http_server.listen(options.port)
IOLoop.instance().start()
if __name__ == '__main__':
runserver()