[][src]Struct hyperbole::App

pub struct App { /* fields omitted */ }

Contains routes and handlers for a given http application.

Examples

use hyper::{server::Server, Body};
use hyperbole::{record_args, uri, App, R};

#[record_args]
async fn echo(param: u32, _body: Body) -> String {
    format!("param: {}, body: {:?}", param, _body)
}

#[tokio::main]
async fn main() -> hyper::Result<()> {
    let app = App::new()
        .context_path(uri!["first" / param: u32])
        .map(|cx: R![param: _]| cx)
        // GET /first/:param/echo
        .get(uri!["echo"], echo)
        .collapse();

    Server::bind(&([127, 0, 0, 1], 12345).into())
        .serve(app.into_make_service())
        .await
}

Implementations

impl App[src]

pub fn new() -> Self[src]

Returns a new App with no handlers.

Examples

use hyperbole::{f, zoom, App, R};

let _app = App::new()
    .context()
    // &'static str will be available in this context
    .inject("hello world")
    .map(|cx: R![&str]| {
        println!("str is {:?}", cx.get::<&str, _>());
        cx
    })
    .map(|cx: R![&'static str]| cx)
    .collapse();

let _app = App::new()
    .context()
    // 'x: {integer}' will be available in this context
    .inject(f![x = 40])
    .map(|cx: R![x: u64]| {
        println!("x is {}", zoom!(&cx.x));
        cx
    })
    .collapse();

pub fn context_path<P: HList + Parser<Segment> + Send>(
    self,
    spec: PathSpec<P>
) -> Ctx<Params<P>, Path<Base, P, Here>, App>
[src]

Begin a new request context at the provided base path. Any parameters parsed from the uri will be merged into the context's state.

The uri! macro can be used to construct an appropriate PathSpec.

Examples

use hyperbole::{uri, App};

let _app = App::new()
    // begin at /
    .context_path(uri![])
    .collapse()
    // begin at /abc
    .context_path(uri!["abc"])
    .collapse()
    // begin at /xyz/:param
    .context_path(uri!["xyz" / param: u32])
    .collapse();

pub fn context(self) -> Ctx<HNil, Base, App>[src]

Begin a new request context at the root path.

pub fn not_found<F, Args, Ix, Fut, Resp>(self, handler: F) -> Self where
    F: Fn(Args) -> Fut + Sync + Send + 'static,
    Fut: Future<Output = Resp> + Send + 'static,
    Resp: Reply + 'static,
    End<Base, F, Args, Ix>: Link<Init, HNil, Output = Response, Params = HNil> + 'static, 
[src]

Configure a handler for the case where an incoming request does not match any existing routes.

As in Ctx::handle, the handler closure should accept an hlist argument, and return a future that evaluates to an http response (via the Reply trait).

The argument hlist may consist of any subset of types from Init.

Examples

use hyper::{Method, StatusCode, Uri};
use hyperbole::{record_args, reply::Reply, App};

#[record_args]
async fn handler(_m: Method, _u: Uri) -> impl Reply {
    "not found".with_status(StatusCode::NOT_FOUND)
}

let _app = App::new().not_found(handler);

pub fn into_make_service(self) -> AppDispatch[src]

Consume this App, turning it into a MakeService compatible with hyper servers.

See hyper::server::Builder::serve for usage details.

pub fn test_client(self) -> Client[src]

Create a test client for this app.

pub fn merge(self, routes: Routes) -> Self[src]

Merge route handlers into this App.

Panics

This method panics if any routes in routes conflict with a route in the App.

This example panics
use hyperbole::{uri, App, Ctx, R};

let routes = Ctx::default()
    .get(uri!["conflict"], |_: R![]| async { "" })
    .get(uri!["conflict"], |_: R![]| async { "" })
    .into_routes();

// 'a handler is already registered for path "/conflict"'
let _app = App::new().merge(routes);
This example panics
use hyperbole::{uri, App, Ctx, R};

let routes = Ctx::default()
    .get(uri!["something"], |_: R![]| async { "" })
    .get(uri![param: u32], |_: R![]| async { "" })
    .into_routes();

// 'wildcard ":param" conflicts with existing children in path "/:param"'
let _app = App::new().merge(routes);

Trait Implementations

impl Default for App[src]

Auto Trait Implementations

impl !RefUnwindSafe for App

impl Send for App

impl Sync for App

impl Unpin for App

impl !UnwindSafe for App

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<Choices> CoproductSubsetter<CNil, HNil> for Choices[src]

type Remainder = Choices

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U, I> LiftInto<U, I> for T where
    U: LiftFrom<T, I>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<Source> Sculptor<HNil, HNil> for Source[src]

type Remainder = Source

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.