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<Exec, Ex> Amiya<Exec, Ex>
impl<Exec, Ex> Amiya<Exec, Ex>
Sourcepub fn uses<M: Middleware<Ex> + 'static>(self, middleware: M) -> Self
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);Sourcepub fn executor<NewExec>(self, executor: NewExec) -> Amiya<NewExec, Ex>
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>
impl<Exec, Ex> Amiya<Exec, Ex>
Sourcepub fn listen<A: ToSocketAddrs>(self, addr: A) -> Result<Sender<()>>
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.