Comments (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.
@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.
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.
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)
- axum:rejections does not seem to work for TypedHeader HOT 3
- How to avoid Slowloris DoS Attack? HOT 11
- websocket trait bound is not satisfied HOT 5
- bump http to 0.2 HOT 2
- API version management HOT 1
- A beginner’s question about middleware: error[E0277]: the trait bound `axum::middleware::FromFn<fn(hyper::Request<axum::body::Body>, Next) -> impl Future<Output =
- Is the `reqwest-response` example outdated? HOT 5
- Add a File type to axum-extra HOT 5
- Implement Clone for `AppendHeaders` HOT 1
- Automatic conversion of enum to `sse::Event` HOT 2
- 0.7.5 can't get headerMap and body in one handler?
- Remove `heck` from axum-macros HOT 2
- Cors error to return bytes of file
- Unexpected behaviour in example `static-file-server`
- there is no timer running, must be called from the context of Tokio runtime HOT 1
- Report error: "implementation of `std::marker::Send` is not general enough" in route handler HOT 1
- stream-to-file can not upload big file HOT 1
- `FromRef` triggers `clippy::clone_of_ref_ptr` HOT 1
- Content-Type for Json does not support those of aws HOT 1
- axum fails tests without default features, due to incompletely declared feature "json" HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from axum.