Skip to content

Commit f0e8346

Browse files
committed
feat: Restore compatibility with static deployment
Previous patch on byte/js selection broke static servers. This restores the compatible API by using GET args to filter the answer (on a static server, no filtering will be done but that just means a little more bandwidth usage).
1 parent 3cd75f5 commit f0e8346

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

src/state/learnocaml_api.ml

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,10 @@ module Conversions (Json: JSON_CODEC) = struct
279279
let to_http_request
280280
: type resp. resp request -> http_request
281281
=
282-
let get ?token path = {
282+
let get ?token ?(args=[]) path = {
283283
meth = `GET;
284284
path;
285-
args = match token with None -> [] | Some t -> ["token", Token.to_string t];
285+
args = (match token with None -> [] | Some t -> ["token", Token.to_string t]) @ args;
286286
} in
287287
let post ~token path body = {
288288
meth = `POST body;
@@ -337,11 +337,12 @@ module Conversions (Json: JSON_CODEC) = struct
337337
get ["exercise-index.json"]
338338

339339
| Exercise (Some token, id, js) ->
340-
let ext = if js then ".js.json" else ".bc.json" in
341-
get ~token ("exercises" :: String.split_on_char '/' (id^ext))
340+
get ~token
341+
("exercises" :: String.split_on_char '/' (id^".json"))
342+
~args:["mode", if js then "js" else "byte"]
342343
| Exercise (None, id, js) ->
343-
let ext = if js then ".js.json" else ".bc.json" in
344-
get ("exercises" :: String.split_on_char '/' (id^ext))
344+
get ("exercises" :: String.split_on_char '/' (id^".json"))
345+
~args:["mode", if js then "js" else "byte"]
345346

346347
| Lesson_index () ->
347348
get ["lessons.json"]
@@ -466,15 +467,8 @@ module Server (Json: JSON_CODEC) (Rh: REQUEST_HANDLER) = struct
466467
(match token with
467468
| Some token ->
468469
let id = Filename.chop_suffix (String.concat "/" path) ".json" in
469-
let id_js = match Filename.chop_suffix_opt ~suffix:".bc" id with
470-
| Some id -> Some (id, false)
471-
| None -> match Filename.chop_suffix_opt ~suffix:".js" id with
472-
| Some id -> Some (id, true)
473-
| None -> None
474-
in
475-
(match id_js with
476-
| Some (id, js) -> Exercise (Some token, id, js) |> k
477-
| None -> Invalid_request "Missing bc/js extension" |> k)
470+
let js = List.assoc_opt "mode" request.args = Some "js" in
471+
Exercise (Some token, id, js) |> k
478472
| None -> Invalid_request "Missing token" |> k)
479473
| Some "" ->
480474
Static ["exercise.html"] |> k

0 commit comments

Comments
 (0)