jupyterhub / binderhub

Run your code in the cloud, with technology so advanced, it feels like magic!
https://binderhub.readthedocs.io
BSD 3-Clause "New" or "Revised" License
2.56k stars 390 forks source link

Restructure tornado handler code classes to files under a `handlers/` folder #1560

Open consideRatio opened 2 years ago

consideRatio commented 2 years ago

I was learning about binderhub's API and found that the binderhub/ folder contained very many .py files, making reading the source code unessecary complicated. I suggest a refactoring like in jupyterhub/jupyterhub, where there is a handlers/ folder.

As a further refactoring point, I consider it could make sense to split class definitions to their own files, but this issue just represents having a handlers/ folder.

Handlers

In practice we have the following code files defining Tornado web request handlers that I think should be moved to a dedicated handlers folder: main.py, builder.py, base.py, metrics.py, config.py. Searching for imports from .main etc indicates that only other handler files reference .base and only app.py reference the handlers.

If needed to ensure backward compatebility if other packages are directly referencing handlers, we can update binderhub/__init__.py to import the handlers I think.

main.py

MainHandler - /

GET, index.html and bundle.js, calls /build and redirects to the started user server

ParameterizedMainHandler - /v2/([^/]+)/(.+)

GET, loading.html and bundle.js, calls /build and redirects to the started user server. With this, users can launch directly without pressing a launch button.

LegacyRedirectHandler - /repo/([^/]+)/([^/]+)(/.*)?

GET, redirects to /v2/..., so its in practice /v1/

builder.py

BuildHandler - /build/([^/]+)/(.+)

GET, REST API, streams JSON messages about build and launch progress.

base.py

AboutHandler - /about

GET, about.html, provides basic information

VersionHandler - /versions

GET, REST API, emits JSON about versions for "builder" and "binderhub"

metrics.py

MetricsHandler - /metrics

GET, emits metrics for Prometheus to scrape

health.py

HealthHandler - /health

GET and HEAD, REST API, emits JSON about deployment health status under keys "ok" and "checks"

config.py

ConfigHandler - /_config

GET, REST API, emits JSON with repo providers configuration. Used by index.js to update the DOM dynamically using JQuery. The DOM updated is the input fields and labels for "repository" and "ref" in index.html. Introduced in https://github.com/jupyterhub/binderhub/pull/1038.