[][src]Struct hyperbole::App

pub struct App<I> { /* fields omitted */ }

Contains routes and handlers for a given http service, as well as top-level application state.

Examples

use hyper::{server::Server, Body};
use hyperbole::{path, record, App};

#[tokio::main]
async fn main() -> hyper::Result<()> {
    let app = App::empty()
        .context_path(path!["first" / param: u32])
        .map(|cx: record![param]| cx)
        // GET /first/:param/echo
        .get(path!["echo"], |cx: record![param, [Body]]| async move {
            let (param, body) = cx.into();
            format!("param: {}, body: {:?}", param, body)
        })
        .collapse();

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

Implementations

impl App<HNil>[src]

pub fn empty() -> Self[src]

Returns a new App without any top-level state.

impl<I: Sync + Send + Clone + 'static> App<I>[src]

pub fn new(state: I) -> Self where
    I: HList
[src]

Returns a new App with the provided top-level state.

The state passed here should be an hlist as constructed by hlist! or record!, and will be cloned for each incoming request.

Examples

use hyperbole::{access, hlist, record, App, Hlist};

// equivalent to App::empty()
let _app = App::new(hlist![]);
let _app = App::new(record![]);

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

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

pub fn context_path<P: Parser<Segment>>(
    self,
    spec: PathSpec<P>
) -> Ctx<I, Params<P>, Path<Base, P, Here>>
[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 path! macro can be used to construct an appropriate PathSpec.

Examples

use hyperbole::{path, App};

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

pub fn context(self) -> Ctx<I, Params<HNil>, Path<Base, HNil, Here>>[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<I>, 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::empty().not_found(handler);

pub fn into_make_service(self) -> AppDispatch<I>[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<I>[src]

Create a test client for this app.

Auto Trait Implementations

impl<I> !RefUnwindSafe for App<I>

impl<I> Send for App<I> where
    I: Send

impl<I> Sync for App<I> where
    I: Sync

impl<I> Unpin for App<I> where
    I: Unpin

impl<I> !UnwindSafe for App<I>

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, 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.