speed-rs-core

Crates.iospeed-rs-core
lib.rsspeed-rs-core
version0.4.1
sourcesrc
created_at2023-11-20 03:29:48.859584
updated_at2023-11-27 02:51:12.743928
descriptionA core HTTP handling server library in Rust, providing low-level HTTP server implementation.
homepage
repositoryhttps://github.com/longlt201203/speed-rs-core
max_upload_size
id1041649
size41,475
Lê Thành Long (longlt201203)

documentation

README

speed-rs-core

A core HTTP Server implementation for Rust.

Installation

Create a new Rust project using cargo

cargo new your-project

Add the package to your project

cargo add speed-rs-core

Or add the following line to the dependencies in your Cargo.toml file:

[dependencies]
...
speed-rs-core = "0.4.1"

Finally build the project

cargo build

Now you can use the package freely.

How To Use

speed-rs-core provides just the core HTTP handling, so you will need to handle the higher-level abstractions. Below is an example of how to respond with an HTML file to the client when there is a request:

use std::fs;
use speed_rs_core::{HttpServer, HttpServerMode, HttpStatusStruct};

fn main() {
    // Create the server in single-thread mode
    let mut server = HttpServer::new(HttpServerMode::SingleThread, "127.0.0.1:3000");
    
    // Provide the request handling function
    server.insert_handler(|mut req, mut res| {
        res.set_status(HttpStatusStruct(200, "OK"));
        res.insert_header("Content-Type".to_string(), "text/html".to_string());


        // Read the HTML
        let err = match fs::read_to_string("public/index.html") {
            Ok(html) => {
                res.text(html);
                None
            },
            Err(e) => Some(e)
        };

        // Since the ownership of req and res are taken, you must return them back to the server
        if let Some(e) = err { Err((req, res, Box::new(e))) } else { Ok((req, res)) }
    });

    // Start listening for requests
    server.listen(|| {
        println!("Server is listening at http://127.0.0.1:3000");
    });
}

[!NOTE] For more details, you can checkout this guide.

Development Guide

To further develop this package and leverage the powerful features of Rust, you can implement traits like RequestParamsExtractor for additional functionalities:

use std::collections::HashMap;
use speed_rs_core::HttpRequest;

trait RequestParamsExtractor {
    fn params(&self) -> HashMap<String, String>;
}

impl RequestParamsExtractor for HttpRequest {
    fn params(&self) -> HashMap<String, String> {
        // Implementation code here
        HashMap::new()
    }
}

// In your server's request handler
server.insert_handler(|mut req, mut res| {
    // ...
    let params = req.params();
    // ...
    Ok((req, res))
});

License

Distributed under the MIT License. See LICENSE for more information.

Commit count: 37

cargo fmt