From a2456418868b45704cc1f88c1ef87161c85d2f07 Mon Sep 17 00:00:00 2001 From: MinRK Date: Sat, 13 Sep 2014 22:41:06 -0700 Subject: [PATCH] simplify handler setup with default_handlers in modules like IPython's, but a bit simpler since we don't have so many services to deal with. --- jupyterhub/apihandlers/__init__.py | 8 ++++++- jupyterhub/apihandlers/auth.py | 4 ++++ jupyterhub/app.py | 36 +++++++++++------------------- jupyterhub/handlers/__init__.py | 8 ++++++- jupyterhub/handlers/base.py | 4 ++++ jupyterhub/handlers/login.py | 5 +++++ 6 files changed, 40 insertions(+), 25 deletions(-) diff --git a/jupyterhub/apihandlers/__init__.py b/jupyterhub/apihandlers/__init__.py index d7c8ad39..8062bb46 100644 --- a/jupyterhub/apihandlers/__init__.py +++ b/jupyterhub/apihandlers/__init__.py @@ -1 +1,7 @@ -from .auth import * \ No newline at end of file +from . import auth, users +from .auth import * +from .users import * + +default_handlers = [] +for mod in (auth, users): + default_handlers.extend(mod.default_handlers) diff --git a/jupyterhub/apihandlers/auth.py b/jupyterhub/apihandlers/auth.py index 34bfc721..83299b77 100644 --- a/jupyterhub/apihandlers/auth.py +++ b/jupyterhub/apihandlers/auth.py @@ -20,3 +20,7 @@ class AuthorizationsAPIHandler(BaseHandler): self.write(json.dumps({ 'user' : orm_token.user.name, })) + +default_handlers = [ + (r"/api/authorizations/([^/]+)", AuthorizationsAPIHandler), +] diff --git a/jupyterhub/app.py b/jupyterhub/app.py index e97e9c9c..ed48e957 100644 --- a/jupyterhub/app.py +++ b/jupyterhub/app.py @@ -14,29 +14,19 @@ from jinja2 import Environment, FileSystemLoader import tornado.httpserver import tornado.options from tornado.ioloop import IOLoop -from tornado.log import LogFormatter, app_log +from tornado.log import LogFormatter from tornado import gen, web from IPython.utils.traitlets import ( - Unicode, Integer, Dict, TraitError, List, Instance, Bool, Bytes, Any, - DottedObjectName, + Unicode, Integer, Dict, TraitError, List, Bool, Bytes, Any, + DottedObjectName, Set, ) from IPython.config import Application, catch_config_error from IPython.utils.importstring import import_item here = os.path.dirname(__file__) -from .handlers import ( - Template404, - RootHandler, - LoginHandler, - LogoutHandler, - UserHandler, -) - -from .apihandlers import ( - AuthorizationsAPIHandler, -) +from . import handlers, apihandlers from . import orm from ._data import DATA_FILES_PATH @@ -220,19 +210,19 @@ class JupyterHubApp(Application): return handlers def init_handlers(self): - handlers = [ - (r"/", RootHandler), - (r"/login", LoginHandler), - (r"/logout", LogoutHandler), - (r"/api/authorizations/([^/]+)", AuthorizationsAPIHandler), - ] - self.handlers = self.add_url_prefix(self.hub_prefix, handlers) + h = [] + h.extend(handlers.default_handlers) + h.extend(apihandlers.default_handlers) + + self.handlers = self.add_url_prefix(self.hub_prefix, h) + + # some extra handlers, outside hub_prefix self.handlers.extend([ - (r"/user/([^/]+)/?.*", UserHandler), + (r"/user/([^/]+)/?.*", handlers.UserHandler), (r"/?", web.RedirectHandler, {"url" : self.hub_prefix, "permanent": False}), ]) self.handlers.append( - (r'(.*)', Template404) + (r'(.*)', handlers.Template404) ) def init_db(self): diff --git a/jupyterhub/handlers/__init__.py b/jupyterhub/handlers/__init__.py index 4cc535e3..579fa265 100644 --- a/jupyterhub/handlers/__init__.py +++ b/jupyterhub/handlers/__init__.py @@ -1,2 +1,8 @@ from .base import * -from .login import * \ No newline at end of file +from .login import * + +from . import base, login + +default_handlers = [] +for mod in (base, login): + default_handlers.extend(mod.default_handlers) diff --git a/jupyterhub/handlers/base.py b/jupyterhub/handlers/base.py index 46e35e7e..c178bf21 100644 --- a/jupyterhub/handlers/base.py +++ b/jupyterhub/handlers/base.py @@ -294,3 +294,7 @@ class UserHandler(BaseHandler): self.redirect(url_concat(self.settings['login_url'], { 'next' : self.request.path, })) + +default_handlers = [ + (r"/", RootHandler), +] diff --git a/jupyterhub/handlers/login.py b/jupyterhub/handlers/login.py index 05d58e84..4976b2fa 100644 --- a/jupyterhub/handlers/login.py +++ b/jupyterhub/handlers/login.py @@ -56,3 +56,8 @@ class LoginHandler(BaseHandler): username=username, ) self.finish(html) + +default_handlers = [ + (r"/login", LoginHandler), + (r"/logout", LogoutHandler), +]