Switch to LazyLock mutex STORE state

This commit is contained in:
2024-12-22 16:29:10 -06:00
parent 5a3cd0e5ea
commit 64747ff11f
2 changed files with 19 additions and 13 deletions

View File

@@ -1,23 +1,25 @@
use std::collections::HashMap; use std::sync::LazyLock;
use salvo::http::HeaderValue; use salvo::http::HeaderValue;
use salvo::logging::Logger; use salvo::logging::Logger;
use salvo::prelude::{ use salvo::prelude::{
affix_state, handler, Depot, Listener, Request, Response, Router, Server, Service, StaticDir, handler, Listener, Request, Response, Router, Server, Service, StaticDir, TcpListener,
TcpListener,
}; };
use tokio::sync::Mutex;
use crate::models::State; use crate::models::State;
static STORE: LazyLock<Mutex<State>> = LazyLock::new(State::new);
mod models; mod models;
mod utility; mod utility;
#[handler] #[handler]
async fn download(depot: &mut Depot, req: &mut Request, res: &mut Response) { pub async fn download(req: &mut Request, res: &mut Response) {
let article_id = req.param::<String>("id").unwrap(); let article_id = req.param::<String>("id").unwrap();
let state = depot.obtain::<State>().unwrap(); let store = STORE.lock().await;
let executable = state.executables.get(&article_id as &str).unwrap(); let executable = store.executables.get(&article_id as &str).unwrap();
let data = executable.with_key(b"test"); let data = executable.with_key(b"test");
if let Err(e) = res.write_body(data) { if let Err(e) = res.write_body(data) {
@@ -41,17 +43,14 @@ async fn main() {
let addr = format!("0.0.0.0:{}", port); let addr = format!("0.0.0.0:{}", port);
tracing_subscriber::fmt().init(); tracing_subscriber::fmt().init();
let mut state = State { let mut store = STORE.lock().await;
executables: HashMap::new(), store.add_executable("windows", "./demo-windows.exe");
}; store.add_executable("linux", "./demo-linux");
drop(store);
state.add_executable("windows", "./demo-windows.exe");
state.add_executable("linux", "./demo-linux");
let static_dir = StaticDir::new(["./public"]).defaults("index.html"); let static_dir = StaticDir::new(["./public"]).defaults("index.html");
let router = Router::new() let router = Router::new()
.hoop(affix_state::inject(state))
.push(Router::with_path("download/<id>").get(download)) .push(Router::with_path("download/<id>").get(download))
.push(Router::with_path("<**path>").get(static_dir)); .push(Router::with_path("<**path>").get(static_dir));

View File

@@ -1,4 +1,5 @@
use std::{collections::HashMap, path}; use std::{collections::HashMap, path};
use tokio::sync::Mutex;
use crate::utility::search; use crate::utility::search;
@@ -8,6 +9,12 @@ pub(crate) struct State<'a> {
} }
impl<'a> State<'a> { impl<'a> State<'a> {
pub(crate) fn new() -> Mutex<Self> {
Mutex::new(Self {
executables: HashMap::new(),
})
}
pub(crate) fn add_executable(&mut self, exe_type: &'a str, exe_path: &str) { pub(crate) fn add_executable(&mut self, exe_type: &'a str, exe_path: &str) {
let data = std::fs::read(&exe_path).expect("Unable to read file"); let data = std::fs::read(&exe_path).expect("Unable to read file");