Git Product home page Git Product logo

Comments (4)

davidpdrsn avatar davidpdrsn commented on August 17, 2024 4

This is now supported with tower-http's ServeDir thanks to tower-rs/tower-http#156.

Example usage with axum:

use axum::{
    error_handling::HandleErrorExt, http::StatusCode, routing::service_method_routing::get, Router,
};
use tower_http::services::ServeDir;

#[tokio::main]
async fn main() {
    let app = Router::new().nest(
        "/",
        get(ServeDir::new(".").precompressed_gzip().handle_error(|err: std::io::Error| {
            // missing files are automatically converted to 404
            // this is for other unknown IO errors
            (
                StatusCode::NOT_FOUND,
                format!("Not found, unknown IO error: {}", err),
            )
        })),
    );

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service())
        .await
        .unwrap();
}
[package]
name = "axum-precompressed-files"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.3"
tokio = { version = "1.0", features = ["full"] }
tower-http = { git = "https://github.com/tower-rs/tower-http", features = ["fs"] }

I think I'll close this for now since all that left is wrapping up tower-http 0.2.

@shepmaster Please let me know if you have more questions.

from axum.

davidpdrsn avatar davidpdrsn commented on August 17, 2024 1

@shepmaster We're doing work on this in tower-http. Do you think tower-rs/tower-http#156 would work for your use case? There are some examples in the tests.

from axum.

davidpdrsn avatar davidpdrsn commented on August 17, 2024 1

My goal is to publish the final 0.1.x release of tower-http next week and then starting merging breaking PRs and working towards 0.2. Then at least then you can test it out with a git dependency. It should work just fine with axum I think.

When tower-http 0.2 will be released I'm not sure off.

from axum.

shepmaster avatar shepmaster commented on August 17, 2024

It certainly seems relevant! I note that you mention that a breaking change will be required — I assume that means that it will still be a while before I'd be able to test it out in Axum?

from axum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.