package stog_server_multi

  1. Overview
  2. Docs

Source file page.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
(*********************************************************************************)
(*                Stog                                                           *)
(*                                                                               *)
(*    Copyright (C) 2012-2015 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 Config
open Stog.Url
module S = Cohttp_lwt_unix.Server
module H = Xtmpl.Xhtml

module XR = Xtmpl.Rewrite

let url_ cfg path =
  let url = Stog.Url.append cfg.http_url.pub path in
  Stog.Url.to_string url

let path_login = ["login"]
let path_sessions = ["sessions"]

let url_login cfg = url_ cfg path_login
let url_sessions cfg = url_ cfg path_sessions

let page_tmpl = [%xtmpl "templates/multi_page.tmpl"]
let page_body_tmpl = [%xtmpl "templates/multi_page_body.tmpl"]

let app_name = "Stog-multi-server"

type block = [`Msg of string | `Block of XR.tree list]

let xmls_of_block = function
| (`Msg str) -> [XR.cdata str]
| (`Block xmls) -> xmls

let error_block b =
  let xmls =  xmls_of_block b in
  let atts = XR.atts_one ("","class") [XR.cdata "alert alert-error"] in
  [ XR.node ("","div") ~atts xmls ]

let message_block b =
  let xmls =  xmls_of_block b in
  let atts = XR.atts_one ("","class") [XR.cdata "alert alert-info"] in
  [ XR.node ("","div") ~atts xmls ]

let nbsp = List.hd ([%xtmpl.string " "] ())

let mk_js_script code =
  H.script ~type_: "text/javascript" [ XR.cdata code ]

let page cfg account_opt ?(empty=false) ?error  ?(js=[]) ?message ~title body =
  let topbar = [] in
  let css_url = url_ cfg ["styles" ; Stog_server.Preview.default_css ] in
  let js = List.map mk_js_script js in
  let headers = (Ojs_server.Tmpl.link_css css_url) :: js in
  let page_error =
    match error with
      None -> None
    | Some e -> Some (error_block e)
  in
  let page_message =
    match message with
      None -> None
    | Some e -> Some (message_block e)
  in
  let body =
    if empty then
      body
    else
      page_body_tmpl ~title ~topbar ?page_error ?page_message ~body ()
  in
  page_tmpl ~app_name ~title ~headers ~body ()

module Form_login = [%ojs.form "templates/form_login.tmpl"]

let param_of_body body =
  let params = Uri.query_of_encoded body in
  fun s ->
    match List.assoc s params with
    | exception Not_found -> None
    | [] | "" :: _ -> None
    | s :: _ -> Some s



OCaml

Innovation. Community. Security.