Skip to content

Support Wildcard in Routes #545

@ElijahAhianyo

Description

@ElijahAhianyo

Currently, Our current routes do not support wildcards, You can only do something like this:

use cot::request::extractors::Path;

async fn hello_name(Path(name): Path<String>) -> cot::Result<Html> {
    Ok(Html::new(format!("Hello, {}!", name)))
}

// inside `impl App`:

fn router(&self) -> Router {
    Router::with_urls([
        Route::with_handler_and_name("/", index, "index"),
        Route::with_handler_and_name("/hello", hello, "hello"),
        Route::with_handler_and_name("/hello/{name}", hello_name, "hello_name"),
    ])

or this for the case of multiple routes

async fn hello_name(Path((first_name, last_name)): Path<(String, String)>) -> cot::Result<Html> {
    Ok(Html::new(format!("Hello, {first_name} {last_name}!")))
}

// inside `impl App`:

fn router(&self) -> Router {
    Router::with_urls([
        // ...
        Route::with_handler_and_name("/hello/{first_name}/{last_name}/", hello_name, "hello_name"),
    ])
}

We have a couple of syntaxes from other frameworks to take inspiration from:

The Axum approach

Router::new().route("/static/*path", get(serve_static_file));

The Actix web approach:

web::resource("/docs/{tail:.*}").route(web::get().to(serve_doc));

The Rocket framework approach

#[get("/browse/<path..>")]
fn browse(path: PathBuf) -> String { ... }

The Django approach (using re_path)

re_path(r'^(?P<lang>[a-z]{2})/(?P<path>.+)$', localized_page),

The FastAPI approach

@app.get("/objects/{bucket}/{path:path}")
async def get_object(bucket: str, path: str): ...

The flask approach

@app.route("/app/<path:subpath>")
def serve_app(subpath):
    return send_from_directory("dist", subpath)

The laravel approach

(Technically, I dont think we can support this the way it is)

Route::get('/pages/{any}', [PageController::class, 'show'])
    ->where('any', '.*');

The Ruby on Rails approach

(similar to the Axum approach)

get '/pages/*path', to: 'pages#show'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions