Crates.io | mini-server |
lib.rs | mini-server |
version | 0.2.0 |
source | src |
created_at | 2023-11-25 22:43:31.171377 |
updated_at | 2024-08-19 19:15:26.318614 |
description | The mini web server |
homepage | https://github.com/luxluth/mini-server#readme |
repository | https://github.com/luxluth/mini-server |
max_upload_size | |
id | 1048620 |
size | 42,597 |
The mini rust server
cargo add mini-server
use mini_server::*;
fn main() {
let mut app = HTTPServer::default();
app.get("/", |_, _| {
let mut response = HTTPResponse::default();
response.set_body(b"Hello World!".to_vec());
response
});
app.run();
}
The path is an expression that can contains dynamic variables.
/
, /this/is/a/path
, .../this/is/a/@varibale
, /this/is/another/#variable
#
and @
are prefixes for dynamic values. #
for denoting numbers
and @
for strings
use mini_server::*;
fn main() {
let mut app = HTTPServer::default();
app.get("/hello/@name/#age", |_, exprs| {
let name = expand!(exprs, "name", PathExpr::String);
let age = expand!(exprs, "age", PathExpr::Number);
let mut response = HTTPResponse::default();
response.set_body(
format!("Hello {name}, you are {age}!")
.as_bytes()
.to_vec(),
);
response
});
}
To run an example:
cargo run --example $name