fix config init twice

move index to host
This commit is contained in:
xfy
2024-05-20 11:56:27 +08:00
parent 8451432a40
commit 12c3694774
6 changed files with 16 additions and 12 deletions

View File

@ -6,13 +6,13 @@ wasm = "application/wasm"
[[host]]
ip = "0.0.0.0"
port = 4000
# index file name and format
index = ["index.html"]
# http keep alive timeout
keep_alive = 15
[[host.route]]
# index file name and format
index = ["index.htm"]
# route path: GET /
location = "/"
location = "/test"
# static file path
root = "./html"
# overrite headers

View File

@ -15,6 +15,9 @@ pub struct SettingRoute {
pub location: String,
/// The static assets root folder
pub root: String,
/// Index files format
#[serde(default = "host_index")]
pub index: Vec<String>,
}
pub type HostRouteMap = BTreeMap<String, SettingRoute>;
@ -26,9 +29,6 @@ pub struct SettingHost {
route: Vec<Option<SettingRoute>>,
#[serde(skip_deserializing, skip_serializing)]
pub route_map: HostRouteMap,
/// Index files format
#[serde(default = "host_index")]
pub index: Vec<String>,
/// HTTP keep-alive timeout
#[serde(default = "keep_alive_timeout_default")]
pub keep_alive: u16,

View File

@ -5,10 +5,10 @@ use tracing::error;
use crate::config::{init_config, MIMEType, Settings};
// global settings
static SETTINGS: OnceLock<Settings> = OnceLock::new();
pub static SETTINGS: OnceLock<Settings> = OnceLock::new();
pub fn get_settings() -> &'static Settings {
SETTINGS.get_or_init(|| {
init_config("./config.toml")
init_config("")
.map_err(|err| {
error!("get_or_init config failed: {err}");
exit(1);

View File

@ -1,4 +1,4 @@
use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
use clap::Parser;
use tokio::task::JoinSet;
@ -6,7 +6,7 @@ use tracing::{debug, info};
use crate::{
config::init_config,
consts::{get_settings, ARCH, NAME, OS, VERSION},
consts::{get_settings, ARCH, NAME, OS, SETTINGS, VERSION},
utils::init_logger,
};
@ -22,7 +22,10 @@ mod utils;
async fn main() -> Result<()> {
let args = cli::Cli::parse();
init_logger();
init_config(&args.config).with_context(|| "init config failed")?;
let settings = init_config(&args.config).with_context(|| "init config failed")?;
SETTINGS
.set(settings)
.map_err(|err| anyhow!("init config failed {err:?}"))?;
// global config
let settings = get_settings();

View File

@ -139,7 +139,7 @@ pub async fn handle_connection(
// find resource local file path
let mut path = None;
for index in host.index.iter() {
for index in router.index.iter() {
let p = parse_assets_path(assets_path, &router.root, index);
if Path::new(&p).exists() {
path = Some(p);

View File

@ -78,6 +78,7 @@ mod tests {
let setting_route = SettingRoute {
location: "/".to_string(),
root: "./public".to_string(),
index: vec!["index.html".into()],
};
let map = BTreeMap::from([("/".to_string(), setting_route)]);
let (_, assets_path) = find_route("/docs/home", &map).unwrap();