Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Accept command line arguments and provide ability to customize defaul…
…t settings
- Loading branch information
Showing
1 changed file
with
60 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,86 @@ | ||
from thunderpush.sortingstation import SortingStation | ||
from thunderpush.handler import ThunderSocketHandler | ||
from thunderpush import api | ||
from thunderpush import settings | ||
|
||
from sockjs.tornado import SockJSRouter | ||
|
||
import tornado.ioloop | ||
import optparse | ||
import logging | ||
|
||
logger = logging.getLogger() | ||
|
||
def run_app(): | ||
# configure logging level | ||
if settings.VERBOSE: | ||
logger.setLevel(logging.DEBUG) | ||
else: | ||
logger.setLevel(logging.INFO) | ||
|
||
def main(): | ||
ThunderRouter = SockJSRouter(ThunderSocketHandler, "/connect") | ||
|
||
# api urls | ||
urls = [ | ||
(r"/1\.0\.0/(?P<apikey>.+)/users/", api.UserCountHandler), | ||
(r"/1\.0\.0/(?P<apikey>.+)/users/(?P<user>.+)/", api.UserHandler), | ||
(r"/1\.0\.0/(?P<apikey>.+)/channels/(?P<channel>.+)/", api.ChannelHandler), | ||
(r"/1\.0\.0/(?P<apikey>.+)/users/", | ||
api.UserCountHandler), | ||
(r"/1\.0\.0/(?P<apikey>.+)/users/(?P<user>.+)/", | ||
api.UserHandler), | ||
(r"/1\.0\.0/(?P<apikey>.+)/channels/(?P<channel>.+)/", | ||
api.ChannelHandler), | ||
] | ||
|
||
# include sockjs urls | ||
urls += ThunderRouter.urls | ||
|
||
application = tornado.web.Application(urls, debug=True) | ||
application = tornado.web.Application(urls, settings.DEBUG) | ||
|
||
ss = SortingStation() | ||
|
||
# TODO: this should be moved somewhere else | ||
ss.create_messenger("key", "secretkey") | ||
|
||
application.listen(8080) | ||
logger.info("Starting Thunderpush server at %s:%d", | ||
settings.HOST, settings.PORT) | ||
|
||
application.listen(settings.PORT, settings.HOST) | ||
tornado.ioloop.IOLoop.instance().start() | ||
|
||
def parse_arguments(opts, args): | ||
for optname in ["PORT", "HOST", "VERBOSE", "DEBUG"]: | ||
value = getattr(opts, optname, None) | ||
|
||
if not value is None: | ||
setattr(settings, optname, value) | ||
|
||
def main(): | ||
parser = optparse.OptionParser() | ||
parser.add_option('-p', '--port', | ||
default=settings.PORT, | ||
help='binds server to custom port', | ||
action="store", type="int", dest="PORT") | ||
|
||
parser.add_option('-H', '--host', | ||
default=settings.HOST, | ||
help='binds server to custom address', | ||
action="store", type="string", dest="HOST") | ||
|
||
parser.add_option('-v', '--verbose', | ||
default=settings.VERBOSE, | ||
help='verbose mode', | ||
action="store_true", dest="VERBOSE") | ||
|
||
parser.add_option('-d', '--debug', | ||
default=settings.DEBUG, | ||
help='debug mode (useful for development)', | ||
action="store_true", dest="DEBUG") | ||
|
||
opts, args = parser.parse_args() | ||
|
||
parse_arguments(opts, args) | ||
run_app() | ||
|
||
|
||
|
||
if __name__ == "__main__": | ||
main() |