mirror of
https://github.com/DefectingCat/candy
synced 2025-07-15 16:51:34 +00:00
fix config init twice
move index to host
This commit is contained in:
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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);
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
|
@ -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();
|
||||
|
Reference in New Issue
Block a user