feat(server):构建基本服务。

This commit is contained in:
徐涛
2024-04-02 16:59:40 +08:00
parent 242a36a61c
commit cd88b97102
4 changed files with 87 additions and 3 deletions

View 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(())
}