日本語

Serving under a path prefix

An application served under a prefix such as /app names it once as a Base, and every link, URL, and call of the block starts with it.

Naming the prefix

An application served under a path prefix, such as /app behind a proxy, names it once as a Base constant, built with const fn Base::new(path: &'static str) -> Base, and writes under APP on the first line of routes!: fn pages(Router<State>) under APP;. APP is the name of a Base constant in scope, without a path before it. Base::ROOT is no prefix at all, and Base::as_str gives the prefix as text.

The prefix is a constant because the routes and actions it prefixes are, so it is chosen when the program is built, with option_env! where a build sets it. A prefix that does not start with /, ends with /, is / alone, holds {, }, or //, or holds a byte the browser percent-encodes fails to compile in a const:

use hypersig::Base;

// error[E0080]: evaluation panicked: a base path starts with `/`, does not
// end with `/`, and is not `/` alone
const APP: Base = Base::new("/app/");

What starts with the prefix

In the pages and in the patches the block's handlers send, these start with /app:

  • Links, Urls, and redirects to the block's routes. The route at / becomes /app alone.
  • The calls to the block's actions, streams, and uploads.
  • Each Asset path the page loads.
  • Each root path in a stylesheet's url(..).

The router still registers each path without the prefix, which Route::path and Action::path return:

use axum::Router;
use hypersig::html::{a, button, p};
use hypersig::{Action, Base, Page, Patches, Route, RouterExt, Signals};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Shop {
    count: i32,
}

// Chosen when the program is built. `option_env!` reads it from the build's environment.
const APP: Base = Base::new("/app");

hypersig::routes! {
    fn pages(Router<()>) under APP;
    const HOME: Route = "/" => home;
    const ITEM: Route<u32> = "/items/{id}" => item;
    action ADD: Action<Shop> = post "/add" => add;
}

async fn home((): (), (): ()) -> Page<Shop> {
    Page::new("Shop", Shop { count: 0 }).child(a().href(ITEM.link_with(&3_u32)).text("Tea"))
}

async fn item((): (), id: u32) -> Page<Shop> {
    Page::new("Item", Shop { count: 0 })
        .child(p().text(format!("Item {id}")))
        .child(button().on_click(ADD.call()).text("Add"))
        .child(a().href(HOME.link()).text("Back"))
}

async fn add((): (), (): ()) -> Patches<Shop> {
    Shop::count().patch(1).into()
}

// Links and URLs start with the base, and the router's paths do not.
assert_eq!(ITEM.url(3).as_str(), "/app/items/3");
assert_eq!(HOME.url(()).as_str(), "/app");
assert_eq!(ITEM.path(), "/items/{id}");
assert_eq!(ADD.path(), "/add");

// Nested at the base here. Behind a proxy that strips `/app`, serve `app` as it is.
let app = pages(Router::new()).hypersig_runtime();
let _served: Router = Router::new().nest(APP.as_str(), app);

Serving it

The same router runs inside Router::nest(APP.as_str(), app), or as it is behind a proxy that strips /app before the request arrives.

What stays unchecked

  • A page served by a router nested under any other path answers every request with a 500, whose body names neither path.
  • The compiler does not check that an action, a stream, or an upload a page calls comes from a block under the same base. One from a block without under APP calls its path without the prefix, which a proxy answers with its own 404 and the router inside nest answers with a 404. One block per application keeps every line under one base.
  • Nothing checks that the router serves the file an Asset path names.
  • A Url already starts with the base, so a canonical URL joins it to an Origin without Origin::under, which The page and its head covers.

Next