Axum is one of the best web frameworks in the Rust community. It is a sub-project based on hyper maintained by Tokio. Axum provides web routing, declarative HTTP request parsing, HTTP response serialization, and can be combined with the middleware in the tower ecosystem.
Dependencies
= { = "<version>" }
optional features: http2
, multipart
, ws
.
Configuration items
[]
= "172.20.10.4" # IP address of the network card to bind, default 0.0.0.0
= 8000 # Port number to bind, default 8080
= false # Whether to use client connection information, default false
= true # Whether to enable graceful shutdown, default false
# Web middleware configuration
[]
= { = true } # Enable compression middleware
= { = true } # Capture panic generated by handler
= { = true, = "info" } # Enable log middleware
= { = true, = "5MB" } # Limit request body size
= { = true, = 60000 } # Request timeout 60s
# Cross-domain configuration
= { = true, = [
"*.github.io",
], = [
"Authentication",
], = [
"GET",
"POST",
], = 60 }
# Static resource configuration
= { = true, = "/static", = "static", = true, = "index.html" }
NOTE: The above middleware configuration can integrate the middleware provided in the tower ecosystem. Of course, if you are very familiar with the tower ecosystem, you can also configure it yourself by writing code without enabling these middleware. The following are relevant document links:
API interface
App implements the WebConfigurator feature, which can be used to specify routing configuration:
use spring::App;
use spring_web::get;
use spring_web::{WebPlugin, WebConfigurator, Router, axum::response::IntoResponse, handler::TypeRouter};
use spring_sqlx::SqlxPlugin;
#[tokio::main]
async fn main() {
App::new()
.add_plugin(SqlxPlugin)
.add_plugin(WebPlugin)
.add_router(router())
.run()
.await
}
fn router() -> Router {
Router::new().typed_route(hello_word)
}
#[get("/")]
async fn hello_word() -> impl IntoResponse {
"hello word"
}
You can also use the auto_config
macro to implement automatic configuration. This process macro will automatically register the routes marked by the Procedural Macro into the app:
+#[auto_config(WebConfigurator)]
#[tokio::main]
async fn main() {
App::new()
.add_plugin(SqlxPlugin)
.add_plugin(WebPlugin)
- .add_router(router())
.run()
.await
}
-fn router() -> Router {
- Router::new().typed_route(hello_word)
-}
Attribute macro
get
in the above example is an attribute macro. spring-web
provides eight standard HTTP METHOD process macros: get
, post
, patch
, put
, delete
, head
, trace
, options
.
You can also use the route
macro to bind multiple methods at the same time:
use route;
use IntoResponse;
async
In addition, spring also supports binding multiple routes to a handler, which requires the routes
attribute macro:
use ;
use IntoResponse;
async
Extract the Component registered by the plugin
In the above example, the SqlxPlugin
plugin automatically registers a Sqlx connection pool component for us. We can use Component
to extract this connection pool from State. Component
is an axum extractor.
use Context;
use get;
use ;
use ;
async
Axum also provides other extractors, which are reexported under spring_web::extractor
.
Read configuration
You can use Config
to extract the configuration in the toml file.
use get;
use ;
use Configurable;
use Deserialize;
async
Add the corresponding configuration to your configuration file:
[]
= 1
= true
Complete code reference web-example
Use Extractor in Middleware
You can also use Extractor in middleware, but please note that you need to follow the rules of axum.
use ;
use ConnectPool;
async
Complete code reference web-middleware-example