Skip to content

Commit

Permalink
feat: Restore compatibility with static deployment
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
AltGr committed Nov 3, 2023
1 parent 3cd75f5 commit f0e8346
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/state/learnocaml_api.ml
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,10 @@ module Conversions (Json: JSON_CODEC) = struct
let to_http_request
: type resp. resp request -> http_request
=
let get ?token path = {
let get ?token ?(args=[]) path = {
meth = `GET;
path;
args = match token with None -> [] | Some t -> ["token", Token.to_string t];
args = (match token with None -> [] | Some t -> ["token", Token.to_string t]) @ args;
} in
let post ~token path body = {
meth = `POST body;
Expand Down Expand Up @@ -337,11 +337,12 @@ module Conversions (Json: JSON_CODEC) = struct
get ["exercise-index.json"]

| Exercise (Some token, id, js) ->
let ext = if js then ".js.json" else ".bc.json" in
get ~token ("exercises" :: String.split_on_char '/' (id^ext))
get ~token
("exercises" :: String.split_on_char '/' (id^".json"))
~args:["mode", if js then "js" else "byte"]
| Exercise (None, id, js) ->
let ext = if js then ".js.json" else ".bc.json" in
get ("exercises" :: String.split_on_char '/' (id^ext))
get ("exercises" :: String.split_on_char '/' (id^".json"))
~args:["mode", if js then "js" else "byte"]

| Lesson_index () ->
get ["lessons.json"]
Expand Down Expand Up @@ -466,15 +467,8 @@ module Server (Json: JSON_CODEC) (Rh: REQUEST_HANDLER) = struct
(match token with
| Some token ->
let id = Filename.chop_suffix (String.concat "/" path) ".json" in
let id_js = match Filename.chop_suffix_opt ~suffix:".bc" id with
| Some id -> Some (id, false)
| None -> match Filename.chop_suffix_opt ~suffix:".js" id with
| Some id -> Some (id, true)
| None -> None
in
(match id_js with
| Some (id, js) -> Exercise (Some token, id, js) |> k
| None -> Invalid_request "Missing bc/js extension" |> k)
let js = List.assoc_opt "mode" request.args = Some "js" in
Exercise (Some token, id, js) |> k
| None -> Invalid_request "Missing token" |> k)
| Some "" ->
Static ["exercise.html"] |> k
Expand Down

0 comments on commit f0e8346

Please sign in to comment.