salvo-basic-app
Salvo Basic Application Setup
Dependencies
[dependencies]
salvo = "0.89.3"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Minimal server
use salvo::prelude::*;
#[handler]
async fn hello() -> &'static str {
"Hello World"
}
#[tokio::main]
async fn main() {
let router = Router::new().get(hello);
let acceptor = TcpListener::new("0.0.0.0:8080").bind().await;
Server::new(acceptor).serve(router).await;
}
Handlers
The #[handler] macro turns an async function into a Handler. All parameters are optional and order-independent:
req: &mut Request— HTTP requestres: &mut Response— HTTP responsedepot: &mut Depot— request-scoped storagectrl: &mut FlowCtrl— middleware flow control
A handler can return any type implementing Writer or Scribe (including (), &'static str, String, Json<T>, StatusCode, Result<T, E>), or write directly via res.render(...).
Response types
use salvo::prelude::*;
use salvo::writing::Text;
use serde::Serialize;
#[derive(Serialize)]
struct User { name: String, age: u8 }
#[handler]
async fn json_body() -> Json<User> {
Json(User { name: "Alice".into(), age: 30 })
}
#[handler]
async fn html_body() -> Text<&'static str> {
Text::Html("<h1>Hello</h1>")
}
#[handler]
async fn no_content() -> StatusCode {
StatusCode::NO_CONTENT
}
#[handler]
async fn go_elsewhere(res: &mut Response) {
res.render(Redirect::found("https://example.com"));
}
HTML rendering uses Text::Html(...) (also Text::Plain, Text::Json, Text::Xml, Text::Css). There is no salvo::writing::Html type.
Error handling
Return Result<T, E> where E: Writer. StatusError is the canonical error type:
#[handler]
async fn may_fail() -> Result<Json<User>, StatusError> {
let user = fetch_user().await
.map_err(|e| StatusError::internal_server_error().cause(e))?;
Ok(Json(user))
}
Available constructors: bad_request(), unauthorized(), forbidden(), not_found(), internal_server_error(), etc. Chain .brief(...), .detail(...), .cause(...) for context.
Request access
#[handler]
async fn inspect(req: &mut Request) -> String {
let method = req.method();
let path = req.uri().path();
let ct: Option<String> = req.header("Content-Type");
let name: Option<String> = req.query("name"); // ?name=...
let id: Option<i64> = req.param("id"); // /{id}
let body: MyData = req.parse_json().await.unwrap(); // JSON body
format!("{method} {path}")
}
header, query, and param are generic over T: Deserialize and return Option<T>. parse_json returns Result.
Response mutation
#[handler]
async fn created(res: &mut Response) {
res.status_code(StatusCode::CREATED);
res.headers_mut().insert("X-Custom", "value".parse().unwrap());
res.render(Json(serde_json::json!({"ok": true})));
}
Related Skills
- salvo-routing: Advanced routing configuration and path parameters
- salvo-middleware: Add middleware for logging, auth, and CORS
- salvo-error-handling: Graceful error handling patterns
More from salvo-rs/salvo-skills
salvo-csrf
Implement CSRF (Cross-Site Request Forgery) protection using cookie or session storage. Use for protecting forms and state-changing endpoints.
16salvo-auth
Implement authentication and authorization using JWT, Basic Auth, or custom schemes. Use for securing API endpoints and user management.
15salvo-cors
Configure Cross-Origin Resource Sharing (CORS) and security headers. Use for APIs accessed from browsers on different domains.
15salvo-proxy
Implement reverse proxy to forward requests to backend services. Use for load balancing, API gateways, and microservices routing.
15salvo-openapi
Generate OpenAPI documentation automatically from Salvo handlers. Use for API documentation, Swagger UI, and API client generation.
15salvo-tls-acme
Configure TLS/HTTPS with automatic certificate management via ACME (Let's Encrypt). Use for production deployments with secure connections.
14