Amiya

Struct Amiya 

Source
pub struct Amiya<Exec, Ex = ()> { /* private fields */ }
Expand description

Amiya HTTP Server.

Amiya itself also implement the Middleware trait and can be added to another Amiya instance, see examples/subapp.rs for a example.

Implementations§

Source§

impl<Ex> Amiya<BuiltInExecutor, Ex>

Source

pub fn new() -> Self

Create a Amiya instance.

Source§

impl<Exec, Ex> Amiya<Exec, Ex>
where Ex: Send + Sync + 'static,

Source

pub fn uses<M: Middleware<Ex> + 'static>(self, middleware: M) -> Self

Add a middleware to the end, middleware will be executed as the order of be added.

You can create middleware by implement the Middleware trait for your custom type or use the m macro to convert a async func or closure.

§Examples
use amiya::m;

amiya::new().uses(m!(ctx => ctx.next().await));
use amiya::{m, middleware::Router};

let router = Router::new().endpoint().get(m!(
    ctx => ctx.resp.set_body("Hello world!");
));

amiya::new().uses(router);
Source

pub fn executor<NewExec>(self, executor: NewExec) -> Amiya<NewExec, Ex>

Set the executor.

Normal users do not need to call this method because Amiya has a built-in multi-thread executor BuiltInExecutor. This method let you change it to your custom one.

Your executor needs to implement the Executor trait.

See examples/tokio_executor.rs for an example of use tokio async runtime.

Source§

impl<Exec, Ex> Amiya<Exec, Ex>
where Exec: Executor + 'static, Ex: Default + Send + Sync + 'static,

Source

pub fn listen<A: ToSocketAddrs>(self, addr: A) -> Result<Sender<()>>

start Amiya server on given addr.

§Return

A bounded 1 capacity channel for stop the server.

Amiya server will stop listening the addr when receive message from this channel.

§Examples
amiya::new().listen("127.0.0.1:8080");
amiya::new().listen(("127.0.0.1", 8080));
use std::net::Ipv4Addr;

amiya::new().listen((Ipv4Addr::new(127, 0, 0, 1), 8080));
use std::net::{SocketAddrV4, Ipv4Addr};

let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
amiya::new().listen(socket);
amiya::new().listen("[::]:8080");
let stop = amiya::new().listen("[::]:8080").unwrap();
// do other things
stop.try_send(()); // amiya http server will stop
§Errors

When listen provided address and port failed.

Trait Implementations§

Source§

impl<Ex> Default for Amiya<BuiltInExecutor, Ex>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<Exec, Ex> Middleware<Ex> for Amiya<Exec, Ex>
where Exec: Send + Sync, Ex: Send + Sync + 'static,

Source§

fn handle<'life0, 'life1, 'async_trait>( &'life0 self, ctx: Context<'life1, Ex>, ) -> Pin<Box<dyn Future<Output = Result> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Your middleware handler function, it will be called when request reach this middleware

Auto Trait Implementations§

§

impl<Exec, Ex> Freeze for Amiya<Exec, Ex>
where Exec: Freeze,

§

impl<Exec, Ex = ()> !RefUnwindSafe for Amiya<Exec, Ex>

§

impl<Exec, Ex> Send for Amiya<Exec, Ex>
where Exec: Send,

§

impl<Exec, Ex> Sync for Amiya<Exec, Ex>
where Exec: Sync,

§

impl<Exec, Ex> Unpin for Amiya<Exec, Ex>
where Exec: Unpin,

§

impl<Exec, Ex = ()> !UnwindSafe for Amiya<Exec, Ex>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,