def __gt__(self, handler): # TODO refactoring the ugly code, try to replace the Slot app = Application() if self.path in app._handlers: setattr(app._handlers[self.path].handler_class, self.method.lower(), handler) else: obj = MagicRoute() setattr(obj, '_magic_route', True) setattr(obj, self.method.lower(), handler) # Application is singleton app._handlers[self.path] = URLSpec(self.path, obj)
from imouto.web import RequestHandler, Application class Redirect_1(RequestHandler): async def get(self): self.redirect('/2') class Redirect_2(RequestHandler): async def get(self): self.write('redirect successful') app = Application([ (r'/1', Redirect_1), (r'/2', Redirect_2) ]) app.run()
from imouto.web import Application from imouto.magicroute import GET, POST async def hello_world_get(request, response): response.write("Hello World, it'is get") async def hello_world_post(request, resposne): resposne.write("Hello World, it'is post") GET / '/' > hello_world_get POST / '/' > hello_world_post app = Application() app.run()
from imouto.secure import create_secure_value from imouto.web import RequestHandler, Application class HeaderDemo(RequestHandler): async def get(self): # header test self.write('your Accept-Encoding is {}\n'.format( self.headers['Accept-Encoding'])) self.set_header('access-token', '123456') self.set_header('secure-token', create_secure_value('name', 'value', secret='root')) app = Application([(r'/', HeaderDemo)]) app.run()
from collections import namedtuple from imouto.web import RequestHandler, Application class JsonDemo(RequestHandler): async def get(self): T = namedtuple('T', ['neko']) self.write_json(T(self.get_query_argument('neko', 'unknown'))) async def post(self): self.write_json({'neko': self.get_body_argument('neko')}) app = Application([(r'/', JsonDemo)]) app.run()
from imouto.web import RequestHandler, Application class CookieDemo(RequestHandler): async def get(self): self.write('your cookie ' + self.get_cookie('cookie_name', 'unknown')) self.set_cookie('cookie_name', 'imouto') app = Application([(r'/', CookieDemo)]) app.run()
from imouto.web import RequestHandler, Application class HelloWorldHandler(RequestHandler): def initialize(self): self.hoge = "Hello World " async def get(self, name): self.write(self.hoge + name) app = Application([(r'/(\w+)', HelloWorldHandler)], debug=True) app.run()