Commit 8edc08b6 by Stavros Polymenis

Validate configuration and report issues

First iteration of configuration checking
Refactor configuration validating into Config module
Check Archive configuration during webserver startup
Checked notes subdirectory
Validate existense of styles under static directory
parent e2b750a3
......@@ -33,6 +33,13 @@ module Configuration = struct
email = str "email" default.email;
id = match Id.of_string (str "uuid" "") with Some id -> id | None -> Id.generate();
}
let validity config =
let repo = Lpath.fpath_of_repo config.repository in
let open Config.Validation in
empty
&> is_directory repo
&> is_directory Lpath.(fpath_of_notes (notes_of_repo config.repository))
end
module AliasMap = Meta.AliasMap
......
......@@ -37,3 +37,31 @@ let paths_opt toml table_name key_name =
let paths toml table_name key_name default =
match strs_opt toml table_name key_name with
Some ss -> List.map Fpath.v ss | None -> default
module Validation = struct
let empty = []
let (&>) report = function None -> report | Some msg -> msg :: report
let (&&>) report = function [] -> report | msgs -> msgs @ report
let check ok msg = if ok then None else Some msg
let file_exists ?(msg=(fun s -> (s ^ " is not a file"))) ?(parent_dir=Fpath.v ".") file =
let str = Fpath.(to_string (parent_dir // file)) in
check (Sys.file_exists str) (msg str)
let is_directory ?(msg=(fun s -> (s ^ " is not a directory"))) dir =
let str = Fpath.to_string dir in
check (Sys.file_exists str && Sys.is_directory str) (msg str)
let files_exist ?(msg=(fun s -> (s ^ " is not a file"))) ?(parent_dir=Fpath.v ".") files =
let f report file = report &> file_exists ~msg ~parent_dir file in
List.fold_left f empty files
let terminate_when_invalid =
let error i msg = prerr_endline ("Error " ^ string_of_int i ^ ": " ^ msg) in
function
| [] -> ()
| msgs -> List.iteri error (List.rev msgs); exit 1
end
......@@ -31,6 +31,12 @@ module Configuration = struct
styles = paths toml "general" "stylesheets" default.styles;
template = Template.Configuration.of_toml_file toml
}
let validity config =
let open Logarion.Config.Validation in
empty
&> is_directory config.static
&&> files_exist ~parent_dir:config.static config.styles
end
let note_of_body_pairs pairs =
......@@ -54,12 +60,15 @@ let () =
let wcfg =
try Configuration.of_toml_file (Lpath.from_config_paths "web.toml")
with Not_found -> Configuration.default in
with Not_found -> Configuration.default
in
Logarion.Config.Validation.terminate_when_invalid (Configuration.validity wcfg);
let config =
let open L.Archive.Configuration in
try of_toml_file (Lpath.from_config_paths "logarion.toml")
with Not_found -> default ()
in
Logarion.Config.Validation.terminate_when_invalid (L.Archive.Configuration.validity config);
let module L = Logarion.Archive.Make(File) in
let store = File.store config.repository in
let lgrn = L.{ config; store; } in
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment