Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate actix_web;
- use actix_web::{web, App, Responder, HttpServer};
- use std::sync::{Arc, Mutex};
- struct AppData {
- counter: usize,
- }
- fn index(data: web::Data<Arc<Mutex<AppData>>>, info: web::Path<(String, u32)>) -> impl Responder {
- data.lock().unwrap().counter += 1;
- format!("Hello {}! id:{}", info.0, info.1)
- }
- fn counter(data: web::Data<Arc<Mutex<AppData>>>) -> impl Responder {
- format!("Counter: {}", data.lock().unwrap().counter)
- }
- fn main() -> std::io::Result<()> {
- let shared_state = Arc::new(Mutex::new(AppData{counter: 0}));
- HttpServer::new(move || App::new()
- .data(shared_state.clone())
- .route("/{name}/{id}", web::get().to(index))
- .route("/counter", web::get().to(counter))
- )
- .bind("127.0.0.1:8000")?
- .run()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement