web: add handler for '/' URL

Previously, the web server was responsible for redirecting '/' to a
sensible URL. Most likely, the target URL was '/linux/latest/source'.
From there on, web did the redirect to the proper version.

Avoid a redirect by handling '/' directly from our application to the
correct version.

Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>
This commit is contained in:
Théo Lebrun 2024-11-06 14:25:24 +01:00
parent 4779965e0f
commit d06c760b2a

View file

@ -42,6 +42,8 @@ from .web_utils import ProjectConverter, IdentConverter, validate_version, valid
VERSION_CACHE_DURATION_SECONDS = 2 * 60 # 2 minutes
ADD_ISSUE_LINK = "https://github.com/bootlin/elixir/issues/new"
DEFAULT_PROJECT = 'linux'
# Error with extra information about browsed project,
# to be used in project/version URLs
class ElixirProjectError(falcon.errors.HTTPError):
@ -190,6 +192,22 @@ def stringify_source_path(project, version, path):
path = f'{ get_source_base_url(project, version) }{ path }'
return path.rstrip('/')
# Handles the '/' URL
class IndexResource:
def on_get(self, req, resp):
ctx = req.context
project = DEFAULT_PROJECT
query = get_query(ctx.config.project_dir, project)
if not query:
raise ElixirProjectError('Error', f'Unknown default project: {project}',
status=falcon.HTTP_INTERNAL_SERVER_ERROR)
version = parse.quote(query.query('latest'))
resp.status = falcon.HTTP_FOUND
resp.location = stringify_source_path(project, version, '/')
return
# Handles source URLs
# Path parameters are asssumed to be unquoted by converters
class SourceResource:
@ -763,6 +781,7 @@ def get_application():
app.set_error_serializer(error_serializer)
app.add_route('/', IndexResource())
app.add_route('/{project}/{version}/source/{path:path}', SourceResource())
app.add_route('/{project}/{version}/source', SourceWithoutPathResource())
app.add_route('/{project}/{version}/ident', IdentPostRedirectResource())