Crates.io | pulsar_web |
lib.rs | pulsar_web |
version | 0.1.2 |
source | src |
created_at | 2024-10-31 23:34:21.439434 |
updated_at | 2024-11-01 20:26:09.869931 |
description | fast and reliable web framework |
homepage | |
repository | |
max_upload_size | |
id | 1431074 |
size | 46,731 |
Fast and reliable web framework.
First, create the new rust project and install the pulsar_web
crate:
cargo new my_project
cargo add pulsar_web
Now, set up the boilerplate in the src/main.rs
to get started:
use pulsar_web::{Pulse, pulsar};
async fn server(mut server: Pulse) {
server.get("/", |_, res| {
res.data("hello world!");
}).await;
server.launch(3000).await;
}
pulsar!(server);
And now launch the server and visit the localhost on the port 3000
:
cargo run
# output: server launched on: http://127.0.0.1:3000/
Route path is defined in the first argument of the http method:
server.get("/", |_, res| {
res.data("hello, world!");
}).await;
"/"
is the route,get
correspondences to http GET method and the second argument of the method is the method handler, withreq
andres
parameters.
In the code above, GET method on /
route is being handled with the second argument.
You can pass the route you want to listen to in the first argument (e.g /user/settings
).
If a request is made to the unhandled route, 404 error message is responded.
Priority ranking for the routes comes from the first defined method to the last.
To implement dynamic routing, you can wrap the slug name with <
and >
:
server.get("/user/<username>", |req, res| {
let message = format!("hello, {}!", req.route.slugs.get("username"));
res.data(&message);
}).await;
You can easily get the query data as well:
server.get("/?key=value", |req, res|{
let param = req.route.params.get("key");
}).await;
http | pulsar |
---|---|
GET | get |
POST | post |
PUT | put |
PATCH | patch |
DELETE | delete |
any | all |
Every method handler has the same API. For example, to get the received data from the POST request,
you can use req.body
, which is also accessible in GET handler but has a value of an empty string.
req
parameter in the handler's closure argument, represents the requested data,
and res
- responded. req
isn't mutable, but the response can be changed.
Framework also includes cookie, session and expiration manager tools, built inside the
req
and res
arguments.
Project is in the early stage of the development and setting up the documentation would be huge waste of time due to the drastic changes between the versions. Please feel free to contribute or give us some tips!
This software is released in the public domain, meaning that anyone can distribute, modify, use or publish it. Read the full LICENSE.