File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ use std:: sync:: atomic:: { AtomicU32 , Ordering } ;
2+ use std:: sync:: Arc ;
3+
4+ #[ derive( Clone ) ]
5+ struct State {
6+ value : Arc < AtomicU32 > ,
7+ }
8+
9+ impl State {
10+ fn new ( ) -> Self {
11+ Self {
12+ value : Arc :: new ( AtomicU32 :: new ( 0 ) ) ,
13+ }
14+ }
15+ }
16+
17+ #[ async_std:: main]
18+ async fn main ( ) -> tide:: Result < ( ) > {
19+ tide:: log:: start ( ) ;
20+ let mut app = tide:: with_state ( State :: new ( ) ) ;
21+ app. at ( "/" ) . get ( |req : tide:: Request < State > | async move {
22+ let state = req. state ( ) ;
23+ let value = state. value . load ( Ordering :: Relaxed ) ;
24+ Ok ( format ! ( "{}\n " , value) )
25+ } ) ;
26+ app. at ( "/inc" ) . get ( |req : tide:: Request < State > | async move {
27+ let state = req. state ( ) ;
28+ let value = state. value . fetch_add ( 1 , Ordering :: Relaxed ) + 1 ;
29+ Ok ( format ! ( "{}\n " , value) )
30+ } ) ;
31+ app. listen ( "127.0.0.1:8080" ) . await ?;
32+ Ok ( ( ) )
33+ }
You can’t perform that action at this time.
0 commit comments