webserver_rs/web_core/
config.rs

1use salvo::prelude::*;
2use serde::Deserialize;
3#[cfg(feature = "http3")]
4use std::path::PathBuf;
5#[cfg(feature = "http3")]
6use tokio::fs;
7
8#[derive(Deserialize, Clone)]
9pub struct Log {
10    pub dir: String,
11    pub prefix: String,
12    pub utcoffset: [i8; 3],
13    pub level: String,
14}
15#[derive(Deserialize, Clone)]
16pub struct Http3 {
17    pub dir: String,
18    pub cert_file_name: String,
19    pub key_file_name: String,
20}
21#[derive(Deserialize, Clone, Debug)]
22pub struct Database {
23    pub name: String,
24    pub protocol: String,
25}
26#[derive(Deserialize, Clone)]
27pub struct Config {
28    pub host: String,
29    pub pub_dir: String,
30    pub log: Log,
31    pub route_root: String,
32    pub secret_key: String,
33    pub assets_listing: bool,
34    pub database: Option<Vec<Database>>,
35    #[cfg(feature = "http3")]
36    pub http3: Http3,
37}
38
39#[cfg(feature = "http3")]
40#[allow(dead_code)]
41pub(crate) async fn read_cert_and_key(config: Http3) -> Result<(Vec<u8>, Vec<u8>), anyhow::Error> {
42    let cert_root_path = PathBuf::from(config.dir);
43    let cert_path = cert_root_path.join(config.cert_file_name);
44    let key_path = cert_root_path.join(config.key_file_name);
45    dbg!(cert_path.clone(), key_path.clone());
46    let cert = fs::read(cert_path).await?;
47    let key = fs::read(key_path).await?;
48    Ok((cert, key))
49}
50
51pub(crate) struct InjectConfig(pub(crate) Config);
52#[handler]
53impl InjectConfig {
54    async fn handle(
55        &self,
56        req: &mut Request,
57        depot: &mut Depot,
58        res: &mut Response,
59        ctrl: &mut FlowCtrl,
60    ) {
61        depot.inject(self.0.clone());
62        ctrl.call_next(req, depot, res).await;
63    }
64}