-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use actix_web::{web, App, HttpServer, HttpResponse, Responder, middleware}; | ||
use serde_json::json; | ||
use std::sync::Mutex; | ||
|
||
#[derive(Clone)] | ||
struct AppState { | ||
user_count: Mutex<u32>, | ||
} | ||
|
||
async fn index(data: web::Data<AppState>) -> impl Responder { | ||
let count = data.user_count.lock().unwrap(); | ||
HttpResponse::Ok().json(json!({ | ||
"message": "Welcome to the Dashboard!", | ||
"user_count": *count, | ||
})) | ||
} | ||
|
||
async fn increment_user_count(data: web::Data<AppState>) -> impl Responder { | ||
let mut count = data.user_count.lock().unwrap(); | ||
*count += 1; | ||
HttpResponse::Ok().json(json!({ | ||
"user_count": *count, | ||
})) | ||
} | ||
|
||
pub async fn run_web_dashboard() -> std::io::Result<()> { | ||
let state = AppState { | ||
user_count: Mutex::new(0), | ||
}; | ||
|
||
HttpServer::new(move || { | ||
App::new() | ||
.data(state.clone()) | ||
.wrap(middleware::Logger::default()) | ||
.route("/", web::get().to(index)) | ||
.route("/increment", web::post().to(increment_user_count)) | ||
}) | ||
.bind("127.0.0.1:8080")? | ||
.run() | ||
.await | ||
} |