shio rs
1.0.0
Shio是生鏽的快速,簡單和異步的微型網絡框架。
異步。處理程序都是異步處理的,並且本身可能是異步的。 shio::Handler接收tokio_core::reactor::Handle該操作可用於安排線程 - 本地事件循環中的其他工作。
多線程。默認情況下,請求由多個線程處理,每個線程都運行由tokio提供動力的事件循環。
穩定。 Shio完全致力於工作,並繼續從事穩定的生鏽工作。
[ dependencies ]
shio = " 0.2.0 " extern crate shio ;
use shio :: prelude :: * ;
fn hello_world ( _ : Context ) -> Response {
Response :: with ( "Hello World! n " )
}
fn hello ( ctx : Context ) -> Response {
Response :: with ( format ! ( "Hello, {}! n " , & ctx . get :: < Parameters > ( ) [ "name" ] ) )
}
fn main ( ) {
Shio :: default ( )
. route ( ( Method :: GET , "/" , hello_world ) )
. route ( ( Method :: GET , "/{name}" , hello ) )
. run ( ":7878" ) . unwrap ( ) ;
} 請求處理程序是實現shio::Handler特徵的值。
每個請求都不會克隆處理程序,因此可能包含狀態。請注意,必須Send + Sync 。
extern crate shio ;
use std :: thread ;
use std :: sync :: atomic :: { AtomicUsize , Ordering } ;
use shio :: prelude :: * ;
# [ derive ( Default ) ]
struct HandlerWithState {
counter : AtomicUsize ,
}
impl shio :: Handler for HandlerWithState {
type Result = Response ;
fn call ( & self , _ : Context ) -> Self :: Result {
let counter = self . counter . fetch_add ( 1 , Ordering :: Relaxed ) ;
Response :: with ( format ! (
"Hi, #{} (from thread: {:?}) n " ,
counter ,
thread :: current ( ) . id ( )
) )
}
}Shio包含更多的用法示例/。
示例可以使用cargo run -p <example name>運行。例如,要運行hello示例,請使用:
$ cargo run -p hello根據任何一個
可以選擇。
除非您另有明確說明,否則任何有意提交的捐款(如Apache-2.0許可證中定義)應為雙重許可,如上所述,沒有任何其他條款或條件。