GITv

Route repository pages through one handler, and assets from a table

Browse files

The two repository routes had the same handler written out twice, and each of the nine static files had its own route closure. One repository_page handler now serves both routes, and the assets are a table of path, media type and contents. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Author
Um9i vulgarmessiah@gmail.com
Date
22 Nov 2026, 22:17
Commit
0fb1e469ce0a
Parent
7fc2fe7

1 file changed +40−42

@@ -44,6 +44,19 @@ const ICON_APPLE: &[u8] = include_bytes!("../assets/icons/apple-touch.png");
const MANIFEST: &str = r##"{"id":"/","name":"GITv","short_name":"GITv","start_url":"/","scope":"/","display":"standalone","background_color":"#f6f7f9","theme_color":"#ffffff","icons":[{"src":"/favicon.svg","sizes":"any","type":"image/svg+xml"},{"src":"/icons/192.png","sizes":"192x192","type":"image/png"},{"src":"/icons/512.png","sizes":"512x512","type":"image/png"},{"src":"/icons/maskable.png","sizes":"512x512","type":"image/png","purpose":"maskable"}]}"##;
const FAVICON: &str = r##"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><rect width="32" height="32" rx="8" fill="#0f6b5f"/><g fill="none" stroke="#fff" stroke-width="2.6" stroke-linecap="round"><circle cx="11" cy="9" r="2.6"/><circle cx="11" cy="23" r="2.6"/><circle cx="22" cy="12" r="2.6"/><path d="M11 11.6v8.8M22 14.6c0 4-3.4 5-11 6"/></g></svg>"##;
/// The files the pages need, by path, with their media types. Anyone may load them.
const ASSETS: [(&str, &str, &[u8]); 9] = [
("/favicon.svg", "image/svg+xml", FAVICON.as_bytes()),
("/manifest.webmanifest", "application/manifest+json", MANIFEST.as_bytes()),
("/icons/192.png", "image/png", ICON_192),
("/icons/512.png", "image/png", ICON_512),
("/icons/maskable.png", "image/png", ICON_MASKABLE),
("/apple-touch-icon.png", "image/png", ICON_APPLE),
("/fonts/display.woff2", "font/woff2", FONT_DISPLAY),
("/fonts/text.woff2", "font/woff2", FONT_TEXT),
("/fonts/mono.woff2", "font/woff2", FONT_MONO),
];
/// The stylesheet's URL carries a hash of its contents, so browsers can cache it for good and
/// still pick up every change.
static STYLE_URL: LazyLock<String> = LazyLock::new(|| {
@@ -207,6 +220,23 @@ async fn blocking(visitor: Visitor, f: impl FnOnce() -> View + Send + 'static) -
view.response(visitor).await
}
/// A repository page: `/{owner}/{name}` (which redirects to its files) or
/// `/{owner}/{name}/{view}`.
async fn repository_page(
Path(mut segments): Path<std::collections::HashMap<String, String>>,
RawQuery(query): RawQuery,
Extension(signed_in): Extension<bool>,
client: Client,
) -> Response {
let owner = segments.remove("owner").unwrap_or_default();
let name = segments.remove("name").unwrap_or_default();
let view = segments.remove("view");
blocking(Visitor { client, signed_in }, move || {
route(&owner, &name, view.as_deref(), &parse_query(query.as_deref()), signed_in)
})
.await
}
fn main() -> std::process::ExitCode {
let outcome = match Cli::parse().action {
Action::Serve { port } => serve(port),
@@ -230,24 +260,14 @@ fn serve(port: u16) -> Result<(), String> {
tokio::task::spawn_blocking(render::warm_up);
let login = login::Login::new();
// Anyone may load these exact paths: the sign-in page and what it needs to look right.
let public = Router::new()
.route("/style.css", get(|| async { stylesheet() }))
.route("/favicon.svg", get(|| async { static_file("image/svg+xml", FAVICON.as_bytes()) }))
.route(
"/manifest.webmanifest",
get(|| async { static_file("application/manifest+json", MANIFEST.as_bytes()) }),
)
.route("/icons/192.png", get(|| async { static_file("image/png", ICON_192) }))
.route("/icons/512.png", get(|| async { static_file("image/png", ICON_512) }))
.route("/icons/maskable.png", get(|| async { static_file("image/png", ICON_MASKABLE) }))
.route("/apple-touch-icon.png", get(|| async { static_file("image/png", ICON_APPLE) }))
.route("/fonts/display.woff2", get(|| async { static_file("font/woff2", FONT_DISPLAY) }))
.route("/fonts/text.woff2", get(|| async { static_file("font/woff2", FONT_TEXT) }))
.route("/fonts/mono.woff2", get(|| async { static_file("font/woff2", FONT_MONO) }))
.route(
"/login",
get(login::sign_in_form).post(login::sign_in).layer(DefaultBodyLimit::max(login::FORM_LIMIT)),
);
let mut public = Router::new().route("/style.css", get(|| async { stylesheet() }));
for (path, kind, data) in ASSETS {
public = public.route(path, get(move || async move { static_file(kind, data) }));
}
let public = public.route(
"/login",
get(login::sign_in_form).post(login::sign_in).layer(DefaultBodyLimit::max(login::FORM_LIMIT)),
);
// Account actions and unmatched paths require a session.
let private = Router::new()
.route("/logout", post(login::sign_out).layer(DefaultBodyLimit::max(login::FORM_LIMIT)))
@@ -264,30 +284,8 @@ fn serve(port: u16) -> Result<(), String> {
blocking(Visitor { client, signed_in: user }, move || view_index(user))
}),
)
.route(
"/{owner}/{name}",
get(
|Path((o, n)): Path<(String, String)>,
RawQuery(q): RawQuery,
Extension(user): Extension<bool>,
client: Client| {
let visitor = Visitor { client, signed_in: user };
blocking(visitor, move || route(&o, &n, None, &parse_query(q.as_deref()), user))
},
),
)
.route(
"/{owner}/{name}/{view}",
get(
|Path((o, n, v)): Path<(String, String, String)>,
RawQuery(q): RawQuery,
Extension(user): Extension<bool>,
client: Client| {
let visitor = Visitor { client, signed_in: user };
blocking(visitor, move || route(&o, &n, Some(&v), &parse_query(q.as_deref()), user))
},
),
)
.route("/{owner}/{name}", get(repository_page))
.route("/{owner}/{name}/{view}", get(repository_page))
.layer(axum::middleware::from_fn_with_state(Arc::clone(&login), login::browse));
let app = public
.route("/{owner}/{name}/info/refs", get(smart_http::refs))