Revise webserver to use Confix

- Drop begin scope in of_config and improve UUID error message
- Tidy syntax
This commit is contained in:
Stavros Polymenis 2017-10-19 23:09:10 +01:00
parent d56fb93d58
commit c0b6f45fe5
3 changed files with 34 additions and 29 deletions

View File

@ -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

View File

@ -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

View File

@ -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