Skip to content

Commit 6ce3c47

Browse files
committed
feat: new framework "micro-web"
1 parent d7114b5 commit 6ce3c47

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

rust/micro-web/Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "server"
3+
version = "0.0.0"
4+
authors = [
5+
"zavakid <zava.kid@gmail.com>",
6+
]
7+
edition = "2021"
8+
9+
[dependencies]
10+
micro-web="0.1.0-alpha.10"
11+
http = "1.1.0"
12+
tokio = {version = "1", features = ["rt-multi-thread", "net", "io-util", "macros", "sync", "signal", "test-util"] }
13+
14+
[profile.release]
15+
opt-level = 3
16+
debug = false
17+
debug-assertions = false
18+
lto = true
19+
panic = "abort"
20+
incremental = false
21+
codegen-units = 1
22+
rpath = false
23+
strip = false

rust/micro-web/config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
framework:
2+
website: https://github.com/foldright/micro-http/
3+
version: 0.1.0-alpha.10

rust/micro-web/src/main.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use http::StatusCode;
2+
use micro_web::router::{get, post, Router};
3+
use micro_web::{handler_fn, PathParams, Server};
4+
5+
async fn empty_body() -> &'static str {
6+
""
7+
}
8+
9+
async fn echo_uid<'s, 'r>(path_params: &PathParams<'s, 'r>) -> String {
10+
path_params.get("id").map(|s| s.to_owned()).unwrap()
11+
}
12+
13+
async fn default_handler() -> (&'static str, StatusCode) {
14+
("404 not found", StatusCode::NOT_FOUND)
15+
}
16+
17+
#[tokio::main]
18+
async fn main() {
19+
// Build router with multiple routes and handlers
20+
let router = Router::builder()
21+
.route("/", get(handler_fn(empty_body)))
22+
.route("/user", post(handler_fn(empty_body)))
23+
// POST route for JSON data with content-type filter
24+
.route("/user/{id}", get(handler_fn(echo_uid)))
25+
// Add response encoding wrapper
26+
.build();
27+
28+
// Configure and start the server
29+
Server::builder()
30+
.router(router)
31+
.bind("0.0.0.0:3000")
32+
.default_handler(handler_fn(default_handler))
33+
.build()
34+
.unwrap()
35+
.start()
36+
.await;
37+
}
38+

0 commit comments

Comments
 (0)