pub struct HttpServe { /* private fields */ }Expand description
HttpServe is the main struct of the Pluto library. It is used to create a new instance of HttpServe. It is used in the ‘http_request’ and ‘http_request_update’ function of the canister. This struct handles routing from not upgradable request to upgradable request. It also handles CORS.
Implementations§
Source§impl HttpServe
impl HttpServe
Sourcepub fn new(init_name: &str) -> Self
pub fn new(init_name: &str) -> Self
Create a new instance of HttpServe depending on the function name.
Sourcepub fn new_with_router(r: Router, init_name: &str) -> Self
pub fn new_with_router(r: Router, init_name: &str) -> Self
Create a new instance of HttpServe with given router.
Sourcepub fn set_router(&mut self, r: Router)
pub fn set_router(&mut self, r: Router)
Set the router of the HttpServe.
Sourcepub fn bad_request_error(error: Value) -> Result<(), HttpResponse>
pub fn bad_request_error(error: Value) -> Result<(), HttpResponse>
Add a handler to the router. The handler will be executed if the request do matches any method and path.
Sourcepub fn internal_server_error() -> Result<(), HttpResponse>
pub fn internal_server_error() -> Result<(), HttpResponse>
Predefined server error response.
Sourcepub fn not_found_error(message: String) -> Result<(), HttpResponse>
pub fn not_found_error(message: String) -> Result<(), HttpResponse>
Predefined not found error response.
Sourcepub fn use_cors(&mut self, cors_policy: Cors)
pub fn use_cors(&mut self, cors_policy: Cors)
Set the CORS policy of the HttpServe.
use ic_cdk::{query, update};
use pluto::router::Router;
use pluto::http_serve;
use pluto::http::{RawHttpRequest, RawHttpResponse};
use pluto::http::HttpServe;
use pluto::method::Method;
use pluto::cors::Cors;
#[query]
async fn http_request(req: RawHttpRequest) -> RawHttpResponse {
bootstrap(http_serve!(), req).await
}
#[update]
async fn http_request_update(req: RawHttpRequest) -> RawHttpResponse {
bootstrap(http_serve!(), req).await
}
async fn bootstrap(mut app: HttpServe, req: RawHttpRequest) -> RawHttpResponse {
let router = Router::new();
let cors = Cors::new()
.allow_origin("*")
.allow_methods(vec![Method::POST, Method::PUT])
.allow_headers(vec!["Content-Type", "Authorization"])
.max_age(Some(3600));
app.set_router(router);
app.use_cors(cors);
app.serve(req).await
}Sourcepub async fn serve(self, req: RawHttpRequest) -> RawHttpResponse
pub async fn serve(self, req: RawHttpRequest) -> RawHttpResponse
Serve the request. It will return a RawHttpResponse. It will return an internal server error if the request is not valid. It will return a not found error if the request does not match any method and path.
use ic_cdk::{query, update};
use pluto::router::Router;
use pluto::http_serve;
use pluto::http::{RawHttpRequest, RawHttpResponse};
use pluto::http::HttpServe;
#[query]
async fn http_request(req: RawHttpRequest) -> RawHttpResponse {
bootstrap(http_serve!(), req).await
}
#[update]
async fn http_request_update(req: RawHttpRequest) -> RawHttpResponse {
bootstrap(http_serve!(), req).await
}
async fn bootstrap(mut app: HttpServe, req: RawHttpRequest) -> RawHttpResponse {
let router = Router::new();
app.set_router(router);
app.serve(req).await
}