mini-server

Crates.iomini-server
lib.rsmini-server
version0.2.0
sourcesrc
created_at2023-11-25 22:43:31.171377
updated_at2024-08-19 19:15:26.318614
descriptionThe mini web server
homepagehttps://github.com/luxluth/mini-server#readme
repositoryhttps://github.com/luxluth/mini-server
max_upload_size
id1048620
size42,597
0x7C00 (luxluth)

documentation

README

mini-server

The mini rust server

cargo add mini-server

HTTP 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();
}

Dynamic paths

The path is an expression that can contains dynamic variables.

  • Basic paths: /, /this/is/a/path, ...
  • Dynamic 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
  });
}

Examples

To run an example:

cargo run --example $name
Commit count: 27

cargo fmt