Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created June 13, 2021 02:28
Show Gist options
  • Save rust-play/2312d148dc93687e22c39ee8f33693c3 to your computer and use it in GitHub Desktop.
Save rust-play/2312d148dc93687e22c39ee8f33693c3 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use actix_web::{get, web, App, HttpServer, Responder};
use dashmap::DashMap;
#[get("/")]
async fn index(state_data: web::Data<AppState>) -> impl Responder {
"Hello World!"
}
#[get("/test")]
async fn test(state_data: web::Data<AppState>) -> impl Responder {
let dashMap = &state_data.dashMap;
// for k in dashMap {
// println!("{:#?}", (k));
// }
"Hello World!"
}
pub struct AppState {
pub dashMap: DashMap<String, String>,
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let dm: DashMap<String, String> = DashMap::new();
dm.insert("ok".to_string(), "ok".to_string());
let state_data = web::Data::new(AppState { dashMap: dm });
HttpServer::new(move || {
App::new()
.service(index)
.service(test)
.app_data(state_data.clone())
})
.bind("127.0.0.1:8081")?
.run()
.await
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment