Skip to content

Instantly share code, notes, and snippets.

@imammubin
Created June 29, 2023 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imammubin/062c69a5c8a80dd12de4ab4a21c41093 to your computer and use it in GitHub Desktop.
Save imammubin/062c69a5c8a80dd12de4ab4a21c41093 to your computer and use it in GitHub Desktop.
#![allow(unused_imports, non_snake_case)]
use actix_web::{get, web, App, HttpResponse, HttpRequest, HttpServer, Responder};
use std::sync::{Mutex};
#[derive(Clone,Debug)]
pub struct WebLoad{
pub count: i32
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let counting = web::Data::new(Mutex::new(WebLoad{ count: 0 }) );
HttpServer::new(move|| {
App::new()
.app_data(counting.clone())
.route("/",web::get().to(index))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
async fn index(_req: HttpRequest, web_loaded: web::Data<Mutex<WebLoad>>) -> impl Responder{
let mut web_loaded=web_loaded.lock().unwrap();
web_loaded.count+=1;
let respon=format!("Hit This Page {:?}",web_loaded.count);
HttpResponse::Ok().body(respon)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment