Skip to content

Static file server - handling requests without .html suffix #446

Answered by davidpdrsn
ekanna asked this question in Q&A
Discussion options

You must be logged in to vote

This should do it:

use axum::{
    body::{box_body, Body, BoxBody},
    http::{Request, Response, StatusCode, Uri},
    handler::get,
    Router,
};
use std::net::SocketAddr;
use tower::ServiceExt;
use tower_http::services::ServeDir;

#[tokio::main]
async fn main() {
    let app = Router::new().nest("/static", get(handler))

    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    tracing::debug!("listening on {}", addr);
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}

async fn handler(uri: Uri) -> Result<Response<BoxBody>, (StatusCode, String)> {
    let res = get_static_file(uri.clone()).await?;

    if res.status() == Statu…

Replies: 1 comment 13 replies

Comment options

You must be logged in to vote
13 replies
@ilyvion
Comment options

@brianmay
Comment options

@ilyvion
Comment options

@victordidenko
Comment options

@victordidenko
Comment options

Answer selected by ekanna
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
9 participants