package stog_server_multi

  1. Overview
  2. Docs

Source file config.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
(*********************************************************************************)
(*                Stog                                                           *)
(*                                                                               *)
(*    Copyright (C) 2012-2024 INRIA All rights reserved.                         *)
(*    Author: Maxence Guesdon, INRIA Saclay                                      *)
(*                                                                               *)
(*    This program is free software; you can redistribute it and/or modify       *)
(*    it under the terms of the GNU General Public License as                    *)
(*    published by the Free Software Foundation, version 3 of the License.       *)
(*                                                                               *)
(*    This program is distributed in the hope that it will be useful,            *)
(*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *)
(*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the               *)
(*    GNU General Public License for more details.                               *)
(*                                                                               *)
(*    You should have received a copy of the GNU General Public                  *)
(*    License along with this program; if not, write to the Free Software        *)
(*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                   *)
(*    02111-1307  USA                                                            *)
(*                                                                               *)
(*    As a special exception, you have permission to link this program           *)
(*    with the OCaml compiler and distribute executables, as long as you         *)
(*    follow the requirements of the GNU GPL in regard to all of the             *)
(*    software in the executable aside from the OCaml compiler.                  *)
(*                                                                               *)
(*    Contact: Maxence.Guesdon@inria.fr                                          *)
(*                                                                               *)
(*********************************************************************************)

(** *)

open Stog.Url

module W = Ocf.Wrapper

type sha256 = string (* hexadecimal representation of SHA256 digest *)

type account = {
    login: string [@ocf W.string, ""];
    name : string [@ocf W.string, ""];
    email: string [@ocf W.string, ""];
    passwd: sha256 [@ocf W.string, ""] [@ocf.label "password"];
  } [@@ocf]

type t = {
    accounts : account list [@ocf W.list account_wrapper, []] ;

    ssh_priv_key : string option
        [@ocf W.option W.string, None]
        [@ocf.doc "Private SSH key to access repository"] ;

    git_repo_url : string
        [@ocf W.string, ""]
        [@ocf.label "repository_url"]
        [@ocf.doc "URL of git repository"];

    dir : string
        [@ocf W.string, ""]
        [@ocf.label "directory"]
        [@ocf.doc "Directory where to clone repositories"] ;

    stog_dir : string option
        [@ocf W.option W.string, None]
        [@ocf.label "stog_directory"]
        [@ocf.doc "Optional subdirectory where to run stog in a clone"];

    editable_files : string list
        [@ocf W.list W.string, [] ]
        [@ocf.doc "Regexps of files to be able to edit"];

    not_editable_files : string list
        [@ocf W.list W.string, [] ]
        [@ocf.doc "Regexps of files not to be able to edit"];

    http_url : Stog.Url.url_config
      [@ocf Stog.Url.url_config_wrapper,
        Stog.Url.default_url_config (Stog.Url.of_string "http://localhost:8080")]
        [@ocf.label "http_server"]
        [@ocf.doc "URL of HTTP server"];

    ws_url : Stog.Url.url_config
      [@ocf Stog.Url.url_config_wrapper,
          Stog.Url.default_url_config (Stog.Url.of_string "ws://localhost:8081")]
        [@ocf.label "ws_server"]
        [@ocf.doc "URL of websocket server"];

    css_file : string option
        [@ocf W.option W.string, None]
        [@ocf.doc "File to serve as default CSS file"] ;
  } [@@ocf]

let group () =
  let option_t = Ocf.option t_wrapper default_t in
  let g = Ocf.as_group option_t in
  (g, option_t)

let read file =
  let (group, t) = group () in
  if not (Sys.file_exists file) then
    begin
      Ocf.to_file group file;
      failwith (Printf.sprintf "Empty configuration file %S created, please edit it" file);
    end;
  try
    Ocf.from_file group file ;
    let t = Ocf.get t in
    let t =
      let dir =
        match t.dir with
        | "" -> Sys.getcwd ()
        | s when Filename.is_relative s -> Filename.concat (Sys.getcwd ()) s
        | s -> s
      in
      { t with dir }
    in
    let t =
      match t.ssh_priv_key with
      | None -> t
      | Some file ->
          let f =
            if Filename.is_relative file then
              Filename.concat (Sys.getcwd ()) file
            else
              file
          in
          { t with ssh_priv_key = Some f }
    in
    let map_url c =
      { pub = Stog.Url.remove_ending_slash c.Stog.Url.pub ;
        priv = Stog.Url.remove_ending_slash c.Stog.Url.priv ;
      }
    in
    let t = { t with http_url = map_url t.http_url } in
    let t = { t with ws_url = map_url t.ws_url } in
    t
  with
    Ocf.Error e -> failwith (Ocf.string_of_error e)
  | e ->
      let msg =
        match e with
          Sys_error s | Failure s -> s
        | e -> Printexc.to_string e
      in
      failwith msg
;;