Skip to content

Commit cb12bd3

Browse files
committed
Prefix URLs in the front-end
Signed-off-by: Alban Gruin <[email protected]>
1 parent f81cc97 commit cb12bd3

11 files changed

+63
-26
lines changed

src/app/dune

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
(wrapped false)
44
(flags :standard -warn-error -9-27-32)
55
(modules Learnocaml_local_storage
6+
Learnocaml_config
67
Server_caller
78
Learnocaml_common)
89
(preprocess
910
(per_module ((pps js_of_ocaml.ppx)
11+
Learnocaml_config
1012
Learnocaml_local_storage
1113
Server_caller)
1214
((pps ppx_ocplib_i18n js_of_ocaml.ppx)

src/app/learnocaml_common.ml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
open Js_utils
1010
open Lwt.Infix
1111
open Learnocaml_data
12+
open Learnocaml_config
1213

1314
module H = Tyxml_js.Html
1415

@@ -171,7 +172,7 @@ let show_loading ?(id = "ocp-loading-layer") contents f =
171172
Manip.(removeClass elt "loaded") ;
172173
Manip.(addClass elt "loading") ;
173174
let chamo_src =
174-
"/icons/tryocaml_loading_" ^ string_of_int (Random.int 9 + 1) ^ ".gif" in
175+
api_server ^ "/icons/tryocaml_loading_" ^ string_of_int (Random.int 9 + 1) ^ ".gif" in
175176
Manip.replaceChildren elt
176177
H.[
177178
div ~a: [ a_id "chamo" ] [ img ~alt: "loading" ~src: chamo_src () ] ;
@@ -286,7 +287,7 @@ let button ~container ~theme ?group ?state ~icon lbl cb =
286287
| Some group -> group in
287288
let button =
288289
H.(button [
289-
img ~alt:"" ~src:("/icons/icon_" ^ icon ^ "_" ^ theme ^ ".svg") () ;
290+
img ~alt:"" ~src:(api_server ^ "/icons/icon_" ^ icon ^ "_" ^ theme ^ ".svg") () ;
290291
pcdata " " ;
291292
span ~a:[ a_class [ "label" ] ] [ pcdata lbl ]
292293
]) in
@@ -537,13 +538,13 @@ let stars_div stars =
537538
let num = 5 * int_of_float (stars *. 2.) in
538539
let num = max (min num 40) 0 in
539540
let alt = Format.asprintf [%if"difficulty: %d / 40"] num in
540-
let src = Format.asprintf "/icons/stars_%02d.svg" num in
541+
let src = Format.asprintf "%s/icons/stars_%02d.svg" api_server num in
541542
H.img ~alt ~src ()
542543
]
543544
544545
let exercise_text ex_meta exo =
545546
let mathjax_url =
546-
"/js/mathjax/MathJax.js?delayStartupUntil=configured"
547+
api_server ^ "/js/mathjax/MathJax.js?delayStartupUntil=configured"
547548
in
548549
let mathjax_config =
549550
"MathJax.Hub.Config({\n\
@@ -578,7 +579,7 @@ let exercise_text ex_meta exo =
578579
<html><head>\
579580
<title>%s - exercise text</title>\
580581
<meta charset='UTF-8'>\
581-
<link rel='stylesheet' href='/css/learnocaml_standalone_description.css'>\
582+
<link rel='stylesheet' href='%s/css/learnocaml_standalone_description.css'>\
582583
<script type='text/x-mathjax-config'>%s</script>
583584
<script type='text/javascript' src='%s'></script>\
584585
</head>\
@@ -588,6 +589,7 @@ let exercise_text ex_meta exo =
588589
<script type='text/javascript'>MathJax.Hub.Configured()</script>\
589590
</html>"
590591
ex_meta.Exercise.Meta.title
592+
api_server
591593
mathjax_config
592594
mathjax_url
593595
descr
@@ -1023,7 +1025,7 @@ module Display_exercise =
10231025
let num = 5 * int_of_float (ex_meta.Meta.stars *. 2.) in
10241026
let num = max (min num 40) 0 in
10251027
let alt = Format.asprintf [%if"difficulty: %d / 40"] num in
1026-
let src = Format.asprintf "/icons/stars_%02d.svg" num in
1028+
let src = Format.asprintf "%s/icons/stars_%02d.svg" api_server num in
10271029
img ~alt ~src ()
10281030
in
10291031
div ~a:[ a_class [ "stars" ] ] [

src/app/learnocaml_config.ml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(* This file is part of Learn-OCaml
2+
*
3+
* Copyright (C) 2020 Alban Gruin.
4+
*
5+
* Learn-OCaml is distributed under the terms of the MIT license. See the
6+
* included LICENSE file for details. *)
7+
8+
class type learnocaml_config = object
9+
method enableTryocaml: bool Js.optdef_prop
10+
method enableLessons: bool Js.optdef_prop
11+
method enableExercises: bool Js.optdef_prop
12+
method enableToplevel: bool Js.optdef_prop
13+
method enablePlayground: bool Js.optdef_prop
14+
method txtLoginWelcome: Js.js_string Js.t Js.optdef_prop
15+
method txtNickname: Js.js_string Js.t Js.optdef_prop
16+
method root: Js.js_string Js.t Js.optdef_prop
17+
end
18+
19+
let config : learnocaml_config Js.t = Js.Unsafe.js_expr "learnocaml_config"
20+
let api_server = Js.(to_string (Optdef.get config##.root (fun () -> string "")))

src/app/learnocaml_config.mli

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(* This file is part of Learn-OCaml
2+
*
3+
* Copyright (C) 2020 Alban Gruin.
4+
*
5+
* Learn-OCaml is distributed under the terms of the MIT license. See the
6+
* included LICENSE file for details. *)
7+
8+
class type learnocaml_config = object
9+
method enableTryocaml: bool Js.optdef_prop
10+
method enableLessons: bool Js.optdef_prop
11+
method enableExercises: bool Js.optdef_prop
12+
method enableToplevel: bool Js.optdef_prop
13+
method enablePlayground: bool Js.optdef_prop
14+
method txtLoginWelcome: Js.js_string Js.t Js.optdef_prop
15+
method txtNickname: Js.js_string Js.t Js.optdef_prop
16+
method root: Js.js_string Js.t Js.optdef_prop
17+
end
18+
19+
val config : learnocaml_config Js.t
20+
val api_server : string

src/app/learnocaml_index_main.ml

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ open Js_utils
1010
open Lwt
1111
open Learnocaml_data
1212
open Learnocaml_common
13+
open Learnocaml_config
1314

1415
module H = Tyxml_js.Html5
1516

@@ -63,7 +64,7 @@ let show_loading msg = show_loading ~id:El.loading_id H.[ul [li [pcdata msg]]]
6364
let get_url token dynamic_url static_url id =
6465
match token with
6566
| Some _ -> dynamic_url ^ Url.urlencode id ^ "/"
66-
| None -> static_url ^ Url.urlencode id
67+
| None -> api_server ^ "/" ^ static_url ^ Url.urlencode id
6768

6869
let exercises_tab token _ _ () =
6970
show_loading [%i"Loading exercises"] @@ fun () ->
@@ -111,7 +112,7 @@ let exercises_tab token _ _ () =
111112
| Some pct when pct >= 100 -> [ "stats" ; "success" ]
112113
| Some _ -> [ "stats" ; "partial" ])
113114
pct_signal in
114-
a ~a:[ a_href (get_url token "/exercises/" "/exercise.html#id=" exercise_id) ;
115+
a ~a:[ a_href (get_url token "/exercises/" "exercise.html#id=" exercise_id) ;
115116
a_class [ "exercise" ] ] [
116117
div ~a:[ a_class [ "descr" ] ] (
117118
h1 [ pcdata title ] ::
@@ -163,7 +164,7 @@ let playground_tab token _ _ () =
163164
let open Tyxml_js.Html5 in
164165
let title = pmeta.Playground.Meta.title in
165166
let short_description = pmeta.Playground.Meta.short_description in
166-
a ~a:[ a_href (get_url token "/playground/" "/playground.html#id=" id) ;
167+
a ~a:[ a_href (get_url token "/playground/" "playground.html#id=" id) ;
167168
a_class [ "exercise" ] ] [
168169
div ~a:[ a_class [ "descr" ] ] (
169170
h1 [ pcdata title ] ::
@@ -606,18 +607,6 @@ let init_sync_token button_group =
606607
Lwt.return (Some token))
607608
(fun _ -> Lwt.return None)
608609

609-
class type learnocaml_config = object
610-
method enableTryocaml: bool Js.optdef_prop
611-
method enableLessons: bool Js.optdef_prop
612-
method enableExercises: bool Js.optdef_prop
613-
method enableToplevel: bool Js.optdef_prop
614-
method enablePlayground: bool Js.optdef_prop
615-
method txtLoginWelcome: Js.js_string Js.t Js.optdef_prop
616-
method txtNickname: Js.js_string Js.t Js.optdef_prop
617-
end
618-
619-
let config : learnocaml_config Js.t = Js.Unsafe.js_expr "learnocaml_config"
620-
621610
let set_string_translations () =
622611
let configured v s = Js.Optdef.case v (fun () -> s) Js.to_string in
623612
let translations = [

src/app/server_caller.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ let () =
7171
(Printexc.to_string e))
7272
| _ -> None
7373

74-
let urlpath =
75-
let api_server = "" in
76-
fun p -> String.concat "/" (api_server::p)
74+
let urlpath p =
75+
String.concat "/" (Learnocaml_config.api_server :: p)
7776

7877
let request req =
7978
let do_req = function

static/description.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<script src="/js/ace/ace.js" type="text/javascript" charset="utf-8" defer></script>
1313
<!-- var editor = ace.edit("learnocaml-exo-editor-pane"); -->
1414
<!-- editor.setOptions({ fontFamily: 'Inconsolata', fontSize: "18px" }); -->
15+
<script language="JavaScript" src="/js/learnocaml-config.js"></script>
1516
<script language="JavaScript" src="/js/learnocaml-description.js" defer></script>
1617

1718
</head>

static/exercise.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<script src="/js/ace/ace.js" type="text/javascript" charset="utf-8" defer></script>
1212
<!-- var editor = ace.edit("learnocaml-exo-editor-pane"); -->
1313
<!-- editor.setOptions({ fontFamily: 'Inconsolata', fontSize: "18px" }); -->
14+
<script language="JavaScript" src="/js/learnocaml-config.js"></script>
1415
<script language="JavaScript" src="/js/learnocaml-exercise.js" defer></script>
1516
</head>
1617
<body>
@@ -32,7 +33,7 @@
3233
</div>
3334
<script language="JavaScript">
3435
var n = Math.floor (Math.random () * 8.99) + 1;
35-
document.getElementById('chamo-img').src = '/icons/tryocaml_loading_' + n + '.gif';
36+
document.getElementById('chamo-img').src = learnocaml_config.root + '/icons/tryocaml_loading_' + n + '.gif';
3637
</script>
3738
<!-- Anything below could be recreated dynamically, but IDs must be kept. -->
3839
<div id="learnocaml-exo-toolbar">

static/partition-view.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<script src="/js/ace/ace.js" type="text/javascript" charset="utf-8" defer></script>
1212
<!-- var editor = ace.edit("learnocaml-exo-editor-pane"); -->
1313
<!-- editor.setOptions({ fontFamily: 'Inconsolata', fontSize: "18px" }); -->
14+
<script language="JavaScript" src="/js/learnocaml-config.js"></script>
1415
<script language="JavaScript" src="/js/learnocaml-partition-view.js" defer></script>
1516
</head>
1617
<body>

static/playground.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<script src="/js/ace/ace.js" type="text/javascript" charset="utf-8" defer></script>
1111
<!-- var editor = ace.edit("learnocaml-exo-editor-pane"); -->
1212
<!-- editor.setOptions({ fontFamily: 'Inconsolata', fontSize: "18px" }); -->
13+
<script language="JavaScript" src="/js/learnocaml-config.js"></script>
1314
<script language="JavaScript" src="/js/learnocaml-playground.js" defer></script>
1415
</head>
1516
<body>
@@ -31,7 +32,7 @@
3132
</div>
3233
<script language="JavaScript">
3334
var n = Math.floor (Math.random () * 8.99) + 1;
34-
document.getElementById('chamo-img').src = '/icons/tryocaml_loading_' + n + '.gif';
35+
document.getElementById('chamo-img').src = learnocaml_config.root + '/icons/tryocaml_loading_' + n + '.gif';
3536
</script>
3637
<!-- Anything below could be recreated dynamically, but IDs must be kept. -->
3738
<div id="learnocaml-exo-toolbar">

static/student-view.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<script src="/js/ace/ace.js" type="text/javascript" charset="utf-8" defer></script>
1212
<!-- var editor = ace.edit("learnocaml-exo-editor-pane"); -->
1313
<!-- editor.setOptions({ fontFamily: 'Inconsolata', fontSize: "18px" }); -->
14+
<script language="JavaScript" src="/js/learnocaml-config.js"></script>
1415
<script language="JavaScript" src="/js/learnocaml-student-view.js" defer></script>
1516
</head>
1617
<body>

0 commit comments

Comments
 (0)