Skip to content

Commit fce228e

Browse files
committed
[tornado] add the start command
1 parent c92356b commit fce228e

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

ioc/extra/tornado/command.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from ioc.extra.command import Command
2+
import tornado
3+
from tornado.httpserver import HTTPServer
4+
5+
6+
class StartCommand(Command):
7+
8+
def __init__(self, application):
9+
self.application = application
10+
11+
def initialize(self, parser):
12+
parser.description = 'Start tornado integrated server'
13+
parser.add_argument('--address', '-a', default='0.0.0.0', help="the host to bind the port")
14+
parser.add_argument('--port', '-p', default=5000, type=int, help="the port to listen")
15+
parser.add_argument('--processes', '-np', default=1, type=int, help="number of processes to start (0=cores available)")
16+
17+
def execute(self, args, output):
18+
output.write("Starting tornado %s:%s\n" % (args.address, args.port))
19+
20+
server = HTTPServer(self.application)
21+
server.bind(args.port, args.address)
22+
23+
output.write("Waiting for connection...\n")
24+
server.start(args.processes)
25+
26+
tornado.ioloop.IOLoop.instance().start()

ioc/extra/tornado/handler.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from werkzeug.routing import NotFound
1+
from werkzeug.routing import NotFound, RequestRedirect
22

33
import tornado.web
44
import tornado.httpclient
@@ -69,6 +69,10 @@ def dispatch(self):
6969
if self.is_finish():
7070
return
7171

72+
except RequestRedirect, e:
73+
self.redirect(e.new_url, True, 301)
74+
return
75+
7276
except NotFound:
7377
self.set_status(404)
7478
self.write("Not Found")

ioc/extra/tornado/resources/config/services.yml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@ services:
66
class: tornado.web.Application
77
arguments: []
88
kwargs:
9-
debug: True
10-
autoreload: True
11-
compiled_template_cache: True
12-
static_hash_cache: True
13-
serve_traceback: True
9+
debug: False
10+
autoreload: False
11+
compiled_template_cache: False
12+
static_hash_cache: False
13+
serve_traceback: False
1414
gzip: True
1515
cookie_secret: MySecret
1616

1717
ioc.extra.tornado.asset_helper:
1818
class: ioc.extra.tornado.router.AssetHelper
1919
arguments: [ "%ioc.extra.tornado.static_public_path%", '@ioc.extra.tornado.router', 'element.static']
2020

21+
ioc.extra.tornado.command.server:
22+
class: ioc.extra.tornado.command.StartCommand
23+
arguments:
24+
- '@ioc.extra.tornado.application'
25+
tags:
26+
command:
27+
- { name: 'tornado:start' }

tests/ioc/extra/tornado/test_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ def test_found(self):
3232

3333
def test_error(self):
3434
response = self.fetch('/exception')
35-
self.assertEquals("An unexpected error occurred", response.body)
35+
self.assertEquals("An unexpected error occurred", response.body[0:28])
3636
self.assertEquals(500, response.code)

0 commit comments

Comments
 (0)