Source file cmd_run.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
let run
    ?interface
    ?port
    ?stop
    ?debug
    ?error_handler
    ?secret
    ?old_secrets
    ?prefix
    ?https
    ?certificate_file
    ?key_file
    ?builtins
    ?greeting
    ?adjust_terminal
    handler
  =
  let () =
    Dream.run
      ?interface
      ?port
      ?stop
      ?debug
      ?error_handler
      ?secret
      ?old_secrets
      ?prefix
      ?https
      ?certificate_file
      ?key_file
      ?builtins
      ?greeting
      ?adjust_terminal
      handler
  in
  Ok 0

(* Command line interface *)

open Cmdliner

let doc = "Runs the Web application, by default at http://localhost:8080"

let sdocs = Manpage.s_common_options

let exits = Common.exits

let man =
  [ `S Manpage.s_description
  ; `P "$(tname) runs the Web application, by default at http://localhost:8080."
  ]

let info = Term.info "dream" ~doc ~sdocs ~exits ~man

let term
    ?interface
    ?port
    ?stop
    ?debug
    ?error_handler
    ?secret
    ?old_secrets
    ?prefix
    ?https
    ?certificate_file
    ?key_file
    ?builtins
    ?greeting
    ?adjust_terminal
    handler
  =
  let open Common.Syntax in
  let+ _term = Common.term
  and+ interface =
    match interface with
    | Some interface ->
      Term.const (Some interface)
    | None ->
      let default = Option.value interface ~default:"localhost" in
      let doc =
        "the network interface to listen on. Defaults to \"localhost\". Use \
         \"0.0.0.0\" to listen on all interfaces."
      in
      let docv = "INTERFACE" in
      let env = Arg.env_var "DREAM_INTERFACE" in
      Arg.(
        value
        & opt (some string) (Some default)
        & info [ "i"; "interface" ] ~doc ~docv ~env)
  and+ port =
    match port with
    | Some port ->
      Term.const (Some port)
    | None ->
      let default = Option.value port ~default:8080 in
      let doc = "the port to listen on. Defaults to 8080." in
      let docv = "PORT" in
      let env = Arg.env_var "DREAM_PORT" in
      Arg.(
        value
        & opt (some int) (Some default)
        & info [ "p"; "port" ] ~doc ~docv ~env)
  and+ debug =
    match debug with
    | Some debug ->
      Term.const debug
    | None ->
      let doc =
        "enables debug information in error templates. See \
         Dream.error_template. The default is false, to prevent accidental \
         deployment with debug output turned on."
      in
      let env = Arg.env_var "DREAM_DEBUG" in
      Arg.(value & flag & info [ "d"; "debug" ] ~doc ~env)
  and+ secret =
    match secret with
    | Some secret ->
      Term.const (Some secret)
    | None ->
      let doc =
        "a key to be used for cryptographic operations, such as signing CSRF \
         tokens. By default, a random secret is generated on each call to \
         Dream.run. For production, generate a key with $(b,gen-key)\n\
         and load it from file. A medium-sized Web app serving 1000 fresh \
         encrypted cookies per second should rotate keys about once a year. \
         See argument $(b,--old-secrets)) below for key rotation."
      in
      let docv = "SECRET" in
      let env = Arg.env_var "DREAM_SECRET" in
      Arg.(
        value & opt (some string) None & info [ "s"; "secret" ] ~doc ~docv ~env)
  and+ old_secrets =
    match old_secrets with
    | Some old_secrets ->
      Term.const [ old_secrets ]
    | None ->
      let doc =
        "a list of previous secrets that can still be used for decryption, but \
         not for encryption. This is intended for key rotation."
      in
      let docv = "OLD_SECRETS" in
      let env = Arg.env_var "DREAM_OLD_SECRETS" in
      Arg.(value & opt_all string [] & info [ "old-secret" ] ~doc ~docv ~env)
  and+ prefix =
    match prefix with
    | Some prefix ->
      Term.const (Some prefix)
    | None ->
      let default = Option.value prefix ~default:"/" in
      let doc =
        "a site prefix for applications that are not running at the root (/) \
         of their domain. The default is \"/\", for no prefix."
      in
      let docv = "PREFIX" in
      let env = Arg.env_var "DREAM_PREFIX" in
      Arg.(
        value
        & opt (some string) (Some default)
        & info [ "prefix" ] ~doc ~docv ~env)
  and+ https =
    match https with
    | Some https ->
      Term.const https
    | None ->
      let doc =
        "enables HTTPS. You should also specify $(b,--certificate-file) and \
         $(b,--key-file). However, for development, Dream includes an insecure \
         compiled-in localhost certificate. Enabling HTTPS also enables \
         transparent upgrading of connections to HTTP/2."
      in
      let env = Arg.env_var "DREAM_HTTPS" in
      Arg.(value & flag & info [ "https" ] ~doc ~env)
  and+ certificate_file =
    match certificate_file with
    | Some certificate_file ->
      Term.const (Some certificate_file)
    | None ->
      let doc =
        "specify the certificate file, when using $(b,--https). They are not \
         required for development, but are required for production. Dream will \
         write a warning to the log if you are using $(b,--https), don't \
         provide $(b,--certificate-file) and $(b,--key-file), and \
         $(b,--interface) is not \"localhost\"."
      in
      let docv = "CERTIFICATE_FILE" in
      let env = Arg.env_var "DREAM_CERTIFICATE_FILE" in
      Arg.(
        value
        & opt (some string) None
        & info [ "certificate-file" ] ~doc ~docv ~env)
  and+ key_file =
    match key_file with
    | Some key_file ->
      Term.const (Some key_file)
    | None ->
      let doc =
        "specify the key file, when using $(b,--https). They are not required \
         for development, but are required for production. Dream will write a \
         warning to the log if you are using $(b,--https), don't provide \
         $(b,--certificate-file) and $(b,--key-file), and $(b,--interface) is \
         not \"localhost\"."
      in
      let docv = "KEY_FILE" in
      let env = Arg.env_var "DREAM_KEY_FILE" in
      Arg.(value & opt (some string) None & info [ "key-file" ] ~doc ~docv ~env)
  and+ no_builtins =
    match builtins with
    | Some builtins ->
      Term.const (not builtins)
    | None ->
      let doc = "disables Built-in middleware." in
      let env = Arg.env_var "DREAM_NO_BUILTINS" in
      Arg.(value & flag & info [ "no-builtins" ] ~doc ~env)
  and+ no_greeting =
    match greeting with
    | Some greeting ->
      Term.const (not greeting)
    | None ->
      let doc =
        "disables the start-up log message that prints a link to the Web \
         application."
      in
      let env = Arg.env_var "DREAM_NO_GREETING" in
      Arg.(value & flag & info [ "no-greeting" ] ~doc ~env)
  and+ no_adjust_terminal =
    match adjust_terminal with
    | Some adjust_terminal ->
      Term.const (not adjust_terminal)
    | None ->
      let doc =
        "disables adjusting the terminal to disable echo and line wrapping."
      in
      let env = Arg.env_var "DREAM_NO_ADJUST_TERMINAL" in
      Arg.(value & flag & info [ "no-adjust-terminal" ] ~doc ~env)
  in
  let builtins = not no_builtins in
  let greeting = not no_greeting in
  let adjust_terminal = not no_adjust_terminal in
  run
    ?interface
    ?port
    ?stop
    ~debug
    ?error_handler
    ?secret
    ~old_secrets
    ?prefix
    ~https
    ?certificate_file
    ?key_file
    ~builtins
    ~greeting
    ~adjust_terminal
    handler
  |> Common.handle_errors

let cmd
    ?interface
    ?port
    ?stop
    ?debug
    ?error_handler
    ?secret
    ?old_secrets
    ?prefix
    ?https
    ?certificate_file
    ?key_file
    ?builtins
    ?greeting
    ?adjust_terminal
    handler
  =
  ( term
      ?interface
      ?port
      ?stop
      ?debug
      ?error_handler
      ?secret
      ?old_secrets
      ?prefix
      ?https
      ?certificate_file
      ?key_file
      ?builtins
      ?greeting
      ?adjust_terminal
      handler
  , info )