Salvo is a web server framework written in Rust.
🎯 Features
- Base on hyper, tokio and async supported;
- Websocket supported;
- Middleware is handler and support executed before or after handle;
- Easy to use routing system, routers can be nested, and you can add middleware in routers;
- multipart form supported, handle files upload is very simple;
- Serve a static virtual directory from many physical directories;
⚡️ Quick start
You can view samples here or read docs here.
Create a new rust project:
Add this to Cargo.toml
[]
= "0.9"
= { = "1", = ["full"] }
Create a simple function handler in the main.rs file, we call it hello_world
, this function just render plain text "Hello World"
.
use *;
async
There are many ways to write function handler.
-
You can omit function arguments if they do not used, like
_req
,_depot
in this example:async
-
Any type can be function handler's return value if it implements
Writer
. For example &str implementsWriter
and it will render string as plain text:async
-
The more common situation is we want to return a
Result<T, E>
to implify error handling. IfT
andE
implementsWriter
,Result<T, E>
can be function handler's return type:async
In the main
function, we need to create a root Router first, and then create a server and call it's bind
function:
use *;
async
async
Middleware
There is no difference between Handler and Middleware, Middleware is just Handler.
Tree-like routing system
Router supports nested, and you can add middleware in router. In this example, there are two routers, both of them has same path router "user"
, and both of them added to the same parent router, to do that because we want to add middleware to them and let them has different access control:
use *;
async
async
async
async
async
async
async
async
File upload
We can get file async by the function get_file
in Request
:
async
Multiple files also very simple:
async
More Examples
Your can find more examples in examples folder:
- basic_auth.rs
- compression.rs
- file_list.rs
- proxy.rs
- remote_addr.rs
- routing.rs
- sse_chat.rs
- sse.rs
- tls.rs
- todos.rs
- unix_socket.rs
- ws_chat.rs
- ws.rs
Some code and examples port from from warp and multipart-async.
☕ Supporters
Salvo is an open source project. If you want to support Salvo, you can ☕ buy a coffee here.
⚠️ License
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)