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
📂・Installation
cargo add choki
or add it in your Cargo.toml
choki = "1.0.3"
💡・Features
- Create GET and POST endpoints and use them like you are used to in express.js
- Create Static endpoints
Declare them in your file
use ;
use Server;
Create a object from the class called Server
let mut server: Server = new; // 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.
Create GET endpoint
server.get.unwrap;
Create POST endpoint
server.post.unwrap;
Create STATIC endpoint
server.new_static.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)
Response
So they are four simple functions
res.send_bytes // sends raw bytes with content type you provide (you can provide ContentType::None and let the browser decide)
res.send_string // sends string as response
res.send_json // sends json as response
res.send_code // sends a HTTP response code (404,200...)
as of 1.0.3 you can set or delete cookies and ofc read them.
You can read cookies using req.cookies (stored as a vec)
You can set/delete them using
res.set_cookie;
res.delete_cookie;
Request
When you create an endpoint you have Request and Response. The request holds info about the request.
The final
You need to make the server actually 'listen' for requests so use this method:
server.listen
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
lock;
Also in the src folder there is a main.rs file which can be used as an example.