A simple http server library built from scratch. Using only the threadpool, num_cpus and flate2. (and ofc the built in std)
Using bumpalo allocator for the body buffer.
Heavily inspired by ExpressJs
📂・Installation
cargo add choki
or add it in your Cargo.toml
choki = "1.1.5"
💡・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: = new;
You can set max request size and a public var that is in this case type u8 and it is cloned to every thread/request.
Create GET endpoint
server.get.unwrap;
Create POST endpoint
server.post.unwrap;
Create PUT endpoint
server.put.unwrap;
Create DELETE endpoint
server.delete.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)
Create endpoints with params
As of 1.0.8 choki supports params
server.post.unwrap;
Also queries and body are supported.
req.body is a Vec<BodyItem> which are the items in the body (if multipart-form and etc. (you can check it req.content_type));
req.query are the queries (/search?name=123 the thing after ?)
Middleware is also supported.
server.use_middleware;
Response
So they are four simple functions There two types of responses:
- Sending the data in one big chunk
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
- Sending it chunked
res.send_bytes_chunked
res.send_string_chunked
res.send_json_chunked
res.send_code // sends a HTTP response code (404,200...)
Also you can send download bytes or streams
res.send_download_bytes // Sends bytes and the browser is goind to start to download it.
res.send_download_stream // Pipes a stream and the browser is goind to start to download it.
And piping stream
res.pipe_stream
Sending raw code
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;
as of 1.0.6 you can set or delete headers and ofc read them.
You can set/delete them using
res.set_header;
res.delete_cookie;
Request
When you create an endpoint you have Request and Response. The request holds info about the request.
To get the body use the function body()
let body: = req.body;
The final
You need to make the server actually 'listen' for requests so use this method:
server.listen.unwrap;
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.