From c0b6f45fe5b87ee51b5afee332fe17745a591024 Mon Sep 17 00:00:00 2001 From: Stavros Polymenis Date: Thu, 19 Oct 2017 23:09:10 +0100 Subject: [PATCH] Revise webserver to use Confix - Drop begin scope in of_config and improve UUID error message - Tidy syntax --- src/converters/template.ml | 17 +++++++++-------- src/core/archive.ml | 23 +++++++++++------------ src/logarion_webserver.ml | 23 ++++++++++++++--------- 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/converters/template.ml b/src/converters/template.ml index 0f3d921..e89da49 100644 --- a/src/converters/template.ml +++ b/src/converters/template.ml @@ -21,15 +21,16 @@ module Configuration = struct item = None; } - let of_toml toml = - let path_tpl k = Confix.ConfixToml.(path toml ("templates" / k)) in + let of_config config = + let open Confix.ConfixToml in + let p k = path config ("templates" / k) in { - dir = path_tpl "dir"; - header = path_tpl "header"; - note = path_tpl "note"; - front = path_tpl "front"; - list = path_tpl "list"; - item = path_tpl "item"; + dir = p "dir"; + header = p "header"; + note = p "note"; + front = p "front"; + list = p "list"; + item = p "item"; } end diff --git a/src/core/archive.ml b/src/core/archive.ml index 2b11220..cc5e796 100644 --- a/src/core/archive.ml +++ b/src/core/archive.ml @@ -15,18 +15,17 @@ module Configuration = struct let open Confix.Config in let str k = ConfixToml.(string config ("general" / k)) in try - begin - Ok { - repository = (try Lpath.repo_of_string (str "repository" |> mandatory) - with - | Invalid_argument s -> failwith ("Invalid repository: " ^ s) - | Failure s -> failwith s); - title = str "title" |> with_default ""; - owner = str "owner" |> with_default ""; - email = str "email" |> with_default ""; - id = match Id.of_string (str "uuid" |> mandatory) with Some id -> id | None -> failwith "Invalid UUID in file"; - } - end + Ok { + repository = + (try Lpath.repo_of_string (str "repository" |> mandatory) + with + | Invalid_argument s -> failwith ("Invalid repository: " ^ s) + | Failure s -> failwith s); + title = str "title" |> with_default ""; + owner = str "owner" |> with_default ""; + email = str "email" |> with_default ""; + id = match Id.of_string (str "uuid" |> mandatory) with Some id -> id | None -> failwith "Invalid UUID in config"; + } with Failure str -> Error str diff --git a/src/logarion_webserver.ml b/src/logarion_webserver.ml index e14127f..a4b5f0b 100644 --- a/src/logarion_webserver.ml +++ b/src/logarion_webserver.ml @@ -18,15 +18,15 @@ module Configuration = struct template = Template.Configuration.default_paths; } - let of_toml toml = + let of_config config = let open Confix.Config in let open Confix.ConfixToml in try Ok { - url = string toml ("general" / "url" ) |> mandatory |> Uri.of_string; - static = path toml ("general" / "static_dir" ) |> mandatory; - styles = paths toml ("general" / "stylesheets") |> mandatory; - template = Template.Configuration.of_toml toml; + url = string config ("general"/"url") |> mandatory |> Uri.of_string; + static = path config ("general"/"static_dir") |> mandatory; + styles = paths config ("general"/"stylesheets") |> mandatory; + template = Template.Configuration.of_config config; } with Failure str -> Error str @@ -53,19 +53,24 @@ let optional_html_response = function Some h -> html_response h | None -> html_r let () = let module L = Logarion in + let module Config = Confix.Config.Make (Confix.ConfixToml) in + Random.self_init(); let module Config = Confix.Config.Make (Confix.ConfixToml) in let config = - match Config.(config "logarion.toml" &> L.Archive.Configuration.of_config) - with Ok cfg -> cfg | Error str -> prerr_endline str; exit 1 + Config.(config "logarion.toml" |> to_record L.Archive.Configuration.of_config) + |> function Ok cfg -> cfg | Error str -> prerr_endline str; exit 1 in let web_config = - match Config.(config "web.toml" &> Configuration.of_toml) - with Ok cfg -> cfg | Error str -> prerr_endline str; exit 1 + Config.(config "web.toml" |> to_record Configuration.of_config) + |> function Ok cfg -> cfg | Error str -> prerr_endline str; exit 1 in + Confix.Config.Validation.terminate_when_invalid (Configuration.validity web_config); + Confix.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