Dependencies
= { = "0.0.8" }
Configuration items
[]
= "172.20.10.4" # IP address of the network card to bind, default 127.0.0.1
= 8000 # Port number to bind, default 8080
# Web middleware configuration
[]
= { = true } # Enable compression middleware
= { = true } # Enable log middleware
= { = true } # Capture panic generated by handler
= { = 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" }
API interface
App implements the WebConfigurator feature, which can be used to specify routing configuration:
#[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
}
Attribute macro
get
in the above example is an attribute macro. Spring 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:
async
In addition, spring also supports binding multiple routes to a handler, which requires the routes
attribute macro:
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 get;
use ;
use Component;
use Result;
use Context;
async
Axum also provides other extractors, which are reexported under spring_web::extractor
.
Complete code reference web-example