From cd88b971026bb94fd5d39946ddf4d726c660e9b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 2 Apr 2024 16:59:40 +0800 Subject: [PATCH] =?UTF-8?q?feat(server):=E6=9E=84=E5=BB=BA=E5=9F=BA?= =?UTF-8?q?=E6=9C=AC=E6=9C=8D=E5=8A=A1=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- license_server/Cargo.toml | 17 ++++++++++++++++- license_server/src/logging.rs | 28 ++++++++++++++++++++++++++++ license_server/src/main.rs | 20 ++++++++++++++++++-- license_server/src/products.rs | 25 +++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 license_server/src/logging.rs create mode 100644 license_server/src/products.rs diff --git a/license_server/Cargo.toml b/license_server/Cargo.toml index d938071..c31ffd6 100644 --- a/license_server/Cargo.toml +++ b/license_server/Cargo.toml @@ -6,4 +6,19 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cert_lib = { path = "../cert_lib" } \ No newline at end of file +anyhow = "1.0.81" +axum = { version = "=0.7.4", features = ["default", "macros", "http2"] } +axum-extra = { version = "0.9.3", features = ["query", "form"] } +axum-macros = "0.4.1" +cert_lib = { path = "../cert_lib" } +chrono = { version = "0.4.37", features = ["serde"] } +chrono-tz = { version = "0.8.6", features = ["serde"] } +serde = { version = "1.0.197", features = ["derive"] } +serde_json = { version = "1.0.115", features = ["raw_value"] } +serde_qs = { version = "0.12.0", features = ["axum"] } +serde_repr = "0.1.18" +tokio = { version = "1.37.0", features = ["full"] } +tracing = { version = "0.1.40", features = ["attributes"] } +tracing-appender = "0.2.3" +tracing-error = "0.2.0" +tracing-subscriber = { version = "0.3.18", features = ["env-filter", "time", "local-time", "json", "serde"] } diff --git a/license_server/src/logging.rs b/license_server/src/logging.rs new file mode 100644 index 0000000..9934d1f --- /dev/null +++ b/license_server/src/logging.rs @@ -0,0 +1,28 @@ +use chrono::Local; +use tracing_subscriber::{ + fmt::{self, format::Writer, time::FormatTime}, + layer::SubscriberExt, + util::SubscriberInitExt, + EnvFilter, Registry, +}; + +struct LocalTimer; + +impl FormatTime for LocalTimer { + fn format_time(&self, w: &mut Writer<'_>) -> std::fmt::Result { + write!(w, "{}", Local::now().format("%FT%T%.3f")) + } +} + +pub fn initialize_logging() { + let console_layer = fmt::layer() + .pretty() + .with_file(false) + .with_timer(LocalTimer) + .with_writer(std::io::stderr); + + Registry::default() + .with(EnvFilter::new("debug")) + .with(console_layer) + .init(); +} diff --git a/license_server/src/main.rs b/license_server/src/main.rs index e7a11a9..8260136 100644 --- a/license_server/src/main.rs +++ b/license_server/src/main.rs @@ -1,3 +1,19 @@ -fn main() { - println!("Hello, world!"); +#![feature(diagnostic_namespace)] +#![allow(dead_code)] + +use tracing::{error, info}; + +mod logging; +mod products; + +#[tokio::main] +async fn main() { + // 初始化日志系统 + logging::initialize_logging(); + + // 加载产品数据 + match products::load_products().await { + Err(e) => error!("Failed to load products: {}", e), + Ok(_) => info!("Products loaded successfully"), + } } diff --git a/license_server/src/products.rs b/license_server/src/products.rs new file mode 100644 index 0000000..4f33fd9 --- /dev/null +++ b/license_server/src/products.rs @@ -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> = OnceLock::new(); + +pub fn get() -> &'static Vec { + 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 = serde_json::from_str(&contents)?; + PRODUCTS.set(products).unwrap(); + Ok(()) +}