feat(server):构建基本服务。
This commit is contained in:
25
license_server/src/products.rs
Normal file
25
license_server/src/products.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use serde::Deserialize;
|
||||
use tokio::{fs::File, io::AsyncReadExt};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Product {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
static PRODUCTS: OnceLock<Vec<Product>> = OnceLock::new();
|
||||
|
||||
pub fn get() -> &'static Vec<Product> {
|
||||
PRODUCTS.get().unwrap()
|
||||
}
|
||||
|
||||
pub async fn load_products() -> anyhow::Result<()> {
|
||||
let mut products_file = File::open("./products.json").await?;
|
||||
let mut contents = String::new();
|
||||
products_file.read_to_string(&mut contents).await?;
|
||||
let products: Vec<Product> = serde_json::from_str(&contents)?;
|
||||
PRODUCTS.set(products).unwrap();
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user