Skip to content

Commit

Permalink
Add missing services
Browse files Browse the repository at this point in the history
  • Loading branch information
enolfc committed Dec 19, 2024
1 parent 93abfda commit aa3a9ff
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Empty file.
55 changes: 55 additions & 0 deletions d4science_hub/services/d4science_spawn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""An helper service to redirect users to the right server option.
It expects that the users is already authenticated.
"""

import os
import os.path
from urllib.parse import urlparse

from jupyterhub.services.auth import HubOAuthCallbackHandler, HubOAuthenticated
from jupyterhub.utils import url_path_join
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.web import Application, RequestHandler, authenticated


class D4ScienceHandler(HubOAuthenticated, RequestHandler):
@authenticated
def get(self):
user = self.get_current_user()
rname = self.request.path.split("/")[-1]
if rname:
rname = f"rname-{rname}"
dest_url = url_path_join("/hub/spawn/", user["name"], rname)
self.redirect(
dest_url,
permanent=False,
)
return


def main():
app = Application(
[
(
url_path_join(
os.environ["JUPYTERHUB_SERVICE_PREFIX"], "oauth_callback"
),
HubOAuthCallbackHandler,
),
(
r"%s/[^/]*" % os.environ["JUPYTERHUB_SERVICE_PREFIX"],
D4ScienceHandler,
),
],
cookie_secret=os.urandom(32),
)
http_server = HTTPServer(app)
url = urlparse(os.environ["JUPYTERHUB_SERVICE_URL"])
http_server.listen(url.port)
IOLoop.current().start()


if __name__ == "__main__":
main()

0 comments on commit aa3a9ff

Please sign in to comment.