[/badge.svg?branch=master&event=push)](https://github.com/salvo-rs/salvo/actions)
[/badge.svg?branch=master&event=push)](https://github.com/salvo-rs/salvo/actions)
[/badge.svg?branch=master&event=push)](https://github.com/salvo-rs/salvo/actions)
[](https://codecov.io/gh/salvo-rs/salvo)
[](https://crates.io/crates/salvo)
[](https://crates.io/crates/salvo)

You can view samples here or read docs here.
Create a new rust project:
bash
cargo new hello_salvo --bin
Add this to Cargo.toml
toml
[dependencies]
salvo = "0.9"
tokio = { version = "1", features = ["full"] }
Create a simple function handler in the main.rs file, we call it hello_world
, this function just render plain text "Hello World".
```rust use salvo::prelude::*;
async fn helloworld(req: &mut Request, depot: &mut Depot, res: &mut Response) { res.renderplain_text("Hello World"); } ```
There are many ways to write function handler. ``` rust
async fn helloworld(res: &mut Response) {// remove params you don't used. res.renderplain_text("Hello World"); }
async fn hello_world(res: &mut Response) -> &'static str {// just return &str "Hello World" }
async fn hello_world(res: &mut Response) -> anyhow::Result<&'static str> {// return Result Ok("Hello World") } ```
In the main
function, we need to create a root Router first, and then create a server and call it's bind
function:
```rust use salvo::prelude::*;
async fn helloworld(res: &mut Response) { res.renderplain_text("Hello World"); }
async fn main() { let router = Router::new().get(hello_world); let server = Server::new(router); server.bind(([0, 0, 0, 0], 7878)).await; } ```
```rust use salvo::prelude::*;
async fn main() {
let router = Router::new()
.get(index)
.push(
Router::new()
.path("users")
.before(auth)
.post(createuser)
.push(Router::new().path(r"
Server::new(router).bind(([0, 0, 0, 0], 7878)).await;
}
async fn index(res: &mut Response) { res.renderplaintext("Hello world!"); }
async fn auth(res: &mut Response) { res.renderplaintext("user has authed\n\n"); }
async fn listusers(res: &mut Response) { res.renderplain_text("list users"); }
async fn showuser(res: &mut Response) { res.renderplain_text("show user"); }
async fn createuser(res: &mut Response) { res.renderplain_text("user created"); }
async fn updateuser(res: &mut Response) { res.renderplain_text("user updated"); }
async fn deleteuser(res: &mut Response) { res.renderplain_text("user deleted"); }
```
Your can find more examples in examples folder: - basicauth.rs - compression.rs - filelist.rs - proxy.rs - remoteaddr.rs - routing.rs - ssechat.rs - sse.rs - tls.rs - todos.rs - unixsocket.rs - wschat.rs - ws.rs
Salvo is an open source project. If you want to support Salvo, you can ☕ buy a coffee here.
Salvo is licensed under either of * Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)