Skip to content

A Rust web framework with no external dependencies

License

Notifications You must be signed in to change notification settings

m1guelpf/lil-http-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lil-http

A barebones HTTP 1.1 framework, built in Rust with no external dependencies (other than tokio).

Features

  • Listening to incoming requests
  • Parsing method, path, query, headers, and body according to the HTTP 1.1 spec
  • Responding to requests with an arbitrary body and headers
  • Helpers for responding with text or JSON
  • Allow defining routes and methods as closures
  • Appropiately routing the request to its function, or 404'ing otherwise
  • Appropiately crafting and returning 405 errors on invalid methods.

Usage

use lil_http::{Body, Response, Server};

#[tokio::main]
async fn main() {
    let mut http = Server::new().await.unwrap();

    http.routes
        .get("/", |request| {
            println!("Received {} request to {}", request.method, request.path);

            Response::text(
                format!(
                    "Hello, {}!",
                    request.query.get("name").unwrap_or(&"World".to_string())
                )
                .as_str(),
            )
        })
        .get("/api/user", |request| {
            println!("Received {} request to {}", request.method, request.path);

            Response::json(&serde_json::json!({
                "name": "Miguel Piedrafita",
                "age": 20,
            }))
        })
        .post("/api/hello", |request| {
            println!("Received {} request to {}", request.method, request.path);

            let Body::Json(body) = request.body else {
                return Response::invalid_request();
            };

            let Some(name) = body.get("name") else {
                return Response::invalid_request();
            };

            Response::json(&serde_json::json!({
                "message": format!("Hello, {name}!"),
            }))
        });

    http.run().await;
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A Rust web framework with no external dependencies

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Languages