Warp use the Reply trait to response of the request
- This on fn request is require to impl if we want to derive the from request
fn into_response(self) -> warp::reply::Response {
Response::new(format!("message: {}", self.msg).into())
// Here we can see it's creating the response instance
// when may be we want send the response
// means ever type which impl the Reply trait can convortable
// into the Response.
}
- after the route path we can add the function
.recover(function)
which help use to handle errors (ouccar in above routes). - There args function in
.recover
takeRejection
type and we can match our custom error through Rejection::find(self) -> Option<&T>
-
Some time we need some restrictions to allow the web site to some area or not for other area.
-
So here instead of sending http Put like request we send the prefight request (
HTTP OPTION
request)- option request asks the server if it's OK to send the request, and the server replies with the allowed methods header then then allowed to send PUT request and get the body data.
-
we wrap he route with the
cors
and then it's send the option request which has deifined theallowed headers/methods/orign
-
In
CORS
we can check theHeaders
,Origin
,Methods
and also specified to allow. it's throw and error inForbidden
return type.
- A RESTful API is usually a way to GET, UPDATE, CREATE, and DELETE data through HTTP endpoints, which are grouped by resources.
let store_filter = warp::any().map(move || store.clone());
let get_items = warp::get()
.and(warp::path("questions"))
.and(warp::path::end())
.and(warp::query())
.and(store_filter)
-
from this snippits we can create the store/database which can assess over our every filters.
-
we can appand it between filter and filter replyer callback.
-
and also note that the after in the filter replyer callback function which take one more field for store varible.
-
Same for the
query
store variable it just need to add thewarp::query()
-
Custom Error
- we can create our custom error which juts require to impl interfaces(traits)
- we can create the Enums for our custom error which require some traits to impl.
- std::fmt::Display
- warp::reject::Reject
-
There are multiple lever of logging
- info
- warn
- error
- dubug
-
for loging we use some external crate like
log
andenv_logger
. -
env_logger
helps to define the log level which only log, which is define by the environment varibleRUST_LOG
. -
we can also apply filter on warp like the
CORS
and sending data to each filters, by creating varible fromwarp::log::custom(log< |Info|->() >)
and add to rutes by.with
function to the routes. -
There is one more library called 'log4rs' which is also use for loging, but log4rs contains some extra functionality like to configure by the config file (
_.yaml
) and can also update config without restarting the server. -
we can save the logs to external file and also manage output format to json too.
ORM
which statnds forobject-relational mapping
from this create can create structure/object which can intract with our database.- we can also use the
SQL
to intract with database. SQLx
is a crate to intract with our database.- There are some advanteages of
SQLx
:- it's asynchronous.
- it can support multiple database (MySQL, PostgresSQL, SQLite)
- also work on multiple runtimes (Tokio, async-std, Actix Web)
- and widely use in community
-
Different betwenn Mutext and RwLock
- Mutext -> for either a writer or reader
- RwLock -> allows many readers simultaneously but just one writer at a time
-
This both type is a port of std::sync module which means we
can't use it on async environment
. -
But the
RwLock
dose have the implementations in async as will. -
There are two format to send data from Client side to the server
- JSON
- DATA-URLENCODE => like the key and values assign by
=
and multiple value saperated by the&
sign.
-
Rust library project contains
lib.rs
which provide a public interface to an underlying functionality.
- GET_QUESTIONS
curl --location --request GET 'localhost:3030/questions'
- ADD_QUSTION
curl --location --request POST 'localhost:3030/questions' \
--header 'Content-Type: application/json' \
--data-raw '{
"id":"1",
"title": "new title",
"content": "update content"
}'
- UPDATE_QUESTION
curl --location --request PUT 'localhost:3030/questions/2' \
--header 'Content-Type: application/json' \
--data-raw '{
"id": "2",
"title": "update title",
"content": "update content"
}'
- DELETE_QUSTION
curl --location --request DELETE 'localhost:3030/questions/1'
- Add answer
curl --location --request POST 'localhost:3030/questions/1/answers' \
--header 'Content-type: application/x-www-form-urlencoded' \
--data-urlencode 'id=1' \
--data-urlencode 'content="first question content answer' \
- Get answers
curl --location --request GET 'localhost:3030/questions/2/answers'