Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.80 KB | None | 0 0
  1. extern crate actix_web;
  2. use actix_web::{web, App, Responder, HttpServer};
  3. use std::sync::{Arc, Mutex};
  4.  
  5. struct AppData {
  6.     counter: usize,
  7. }
  8.  
  9. fn index(data: web::Data<Arc<Mutex<AppData>>>, info: web::Path<(String, u32)>) -> impl Responder {
  10.     data.lock().unwrap().counter += 1;
  11.     format!("Hello {}! id:{}", info.0, info.1)
  12. }
  13.  
  14. fn counter(data: web::Data<Arc<Mutex<AppData>>>) -> impl Responder {
  15.     format!("Counter: {}", data.lock().unwrap().counter)
  16. }
  17.  
  18. fn main() -> std::io::Result<()> {
  19.     let shared_state = Arc::new(Mutex::new(AppData{counter: 0}));
  20.  
  21.     HttpServer::new(move || App::new()
  22.         .data(shared_state.clone())
  23.         .route("/{name}/{id}", web::get().to(index))
  24.         .route("/counter", web::get().to(counter))
  25.     )
  26.     .bind("127.0.0.1:8000")?
  27.     .run()
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement