HttpServe

Struct HttpServe 

Source
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

Source

pub fn new(init_name: &str) -> Self

Create a new instance of HttpServe depending on the function name.

Source

pub fn new_with_router(r: Router, init_name: &str) -> Self

Create a new instance of HttpServe with given router.

Source

pub fn set_router(&mut self, r: Router)

Set the router of the HttpServe.

Source

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.

Source

pub fn internal_server_error() -> Result<(), HttpResponse>

Predefined server error response.

Source

pub fn not_found_error(message: String) -> Result<(), HttpResponse>

Predefined not found error response.

Source

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
}
Source

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
}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.