blitzkrieg

Crates.ioblitzkrieg
lib.rsblitzkrieg
version0.2.3
sourcesrc
created_at2023-01-26 20:09:58.477556
updated_at2023-09-30 19:01:40.941217
descriptionAn HTTP Web Server.
homepagehttps://github.com/adhamsalama/blitzkrieg
repositoryhttps://github.com/adhamsalama/blitzkrieg
max_upload_size
id768828
size29,758
Adham Salama (adhamsalama)

documentation

README

Blitzkrieg

Blitzkrieg airplane logo

An HTTP web server written from scratch in Rust (WIP).

Documentation

https://docs.rs/blitzkrieg/latest/blitzkrieg/

Installation

cargo add blitzkrieg

Usage

Hello world

use blitzkrieg::http::{Request, Response};
use blitzkrieg::server::Server;

fn main() {
    let server = Server::new("127.0.0.1:3000", 4, Box::new(handler));
    server.start();
}

fn handler(request: Request) -> Response {
    Response::new(200).body("Hello, world!")
}

File

use std::collections::HashMap;

use blitzkrieg::{
    http::{BodyType, Request, Response},
    server::Server,
};

fn main() {
    let server = Server::new("127.0.0.1:4242", 4, Box::new(handler));
    server.start();
}

fn handler(req: Request) -> Response {
    let mut res = Response::new(200);
    let file = if let Some(BodyType::File(file)) = req.body {
        file
    } else {
        return res.body("Hello, world!");
    };
    std::fs::write(format!("file.{}", file.extension), &file.content.clone()).unwrap();
    res.body = Some(file.content);
    let mut headers = HashMap::new();
    headers.insert(
        "Content-Type".into(),
        req.headers.get("Content-Type").unwrap().into(),
    );
    res.headers = Some(headers);
    res
}
Commit count: 84

cargo fmt