Crates.io | choki |
lib.rs | choki |
version | 1.0.16 |
source | src |
created_at | 2024-05-22 18:45:42.028099 |
updated_at | 2024-11-30 16:26:20.564416 |
description | A simple http server built from scratch. Not intended for production uses but a simple website with some query stuff can be made. (can withstand a lot of requests per second) |
homepage | |
repository | https://github.com/Kartofi/choki |
max_upload_size | |
id | 1248312 |
size | 32,186 |
A simple http server library built from scratch.
Using only the threadpool crate and num_cpus. (and ofc the built in std)
Heavily inspired by express.js
cargo add choki
or add it in your Cargo.toml
choki = "1.0.11"
use choki::structs::{Request, Response};
use choki::Server;
Server
let mut server: Server<u8> = Server::new(Some(1024),None); // you can also type None if you dont want any restrictions
The number in the () is the max content length of the request or in simple terms the max size of the request sent from the client.
server.get("/".to_string(), |req: Request, mut res: Response, public_var: Option<u8>| {
res.send_string("HI");
}).unwrap();
server.post("/".to_string(), |req: Request, mut res: Response, public_var: Option<u8>| {
res.send_string("HI");
}).unwrap();
server.new_static("/images".to_string(), "./tests/images".to_string()).unwrap(); // The first one is the path in the browser for example: example.com/images and the second one is the exposed path from the computer(local)
As of 1.0.8 choki supports params
server.post("/search/[id]".to_string(), |req: Request, mut res: Response, public_var: Option<u8>| {
println!("{}", req.params.get("id").unwrap()); // if i make request to /search/pizza this will print pizza
res.send_string("HI");
}).unwrap();
So they are four simple functions
res.send_bytes(&mut self, data: &[u8], content_type: Option<ContentType>) // sends raw bytes with content type you provide (you can provide ContentType::None and let the browser decide)
res.send_string(&mut self, data: &str) // sends string as response
res.send_json(&mut self, data: &str) // sends json as response
res.send_code(&mut self, code: usize) // sends a HTTP response code (404,200...)
as of 1.0.3 you can set or delete cookies and ofc read them.
pub struct Cookie {
pub name: String,
pub value: String,
pub path: String,
pub expires: String,
}
You can read cookies using req.cookies (stored as a vec)
You can set/delete them using
res.set_cookie(cookie: &Cookie);
res.delete_cookie(name: &str);
as of 1.0.6 you can set or delete headers and ofc read them.
pub struct Header {
pub name: String,
pub value: String,
}
You can set/delete them using
res.set_header(header: &Header);
res.delete_cookie(name: &str);
When you create an endpoint you have Request and Response.
The request holds info about the request.
pub struct Request {
pub query: HashMap<String, String>, // for example in the url www.example.com/?name=Kartof the query will be ["name" => "Kartof"] as hashmap
pub user_agent: Option<String>, // this is the user agent from which the user accesses the website
pub content_length: usize, // the length of the request (they are no implementations for multy form thingy so its not so useful)
}
You need to make the server actually 'listen' for requests so use this method:
server.listen(port: u32, address: Option<String>) -> Result<(), HttpServerError>
And finally because you wanna keep the main thread running or else the server will close as soon as the code runs.
Add this at the end of your file
Server::<i32>::lock();
Also in the src folder there is a main.rs file which can be used as an example.