feat(server):构建基本服务。
This commit is contained in:
parent
242a36a61c
commit
cd88b97102
|
@ -6,4 +6,19 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
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" }
|
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"] }
|
||||||
|
|
28
license_server/src/logging.rs
Normal file
28
license_server/src/logging.rs
Normal file
|
@ -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();
|
||||||
|
}
|
|
@ -1,3 +1,19 @@
|
||||||
fn main() {
|
#![feature(diagnostic_namespace)]
|
||||||
println!("Hello, world!");
|
#![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"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
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(())
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user