allow all origins in dev mode and tie down to chess-scribe.org in prod

This commit is contained in:
2026-05-28 15:17:45 +02:00
parent 9ae4167f58
commit 21de8d9eca
4 changed files with 81 additions and 4 deletions
+5
View File
@@ -0,0 +1,5 @@
run:
dune build && dune exec ./_build/default/bin/main.exe DREAM_ENV=production
run-dev:
dune build && dune exec ./_build/default/bin/main.exe DREAM_ENV=development
+11 -1
View File
@@ -1,6 +1,16 @@
# dune init project chess-scribe-api --kind=exec
# dune exec ./_build/default/bin/main.exe DREAM_ENV=development
# OCAMLPARAM: unset
# Shared cache: enabled-except-user-rules
# Shared cache location: /home/owen/.cache/dune/db
# Workspace root: /home/owen/Desktop/chess-scribe-api
# Auto-detected concurrency: 4
# Dune context:
# { name = "default"
# ; kind = "default"
# ; profile = Dev
# ; merlin = true
# ; fdo_target_exe = None
# ; build_dir = In_build_dir "default"
# ; instrument_with = []
# }
$ /home/owen/.opam/default/bin/ocamlc.opt -config > /tmp/dune_9bdbb8_output
+1 -1
View File
@@ -1,4 +1,4 @@
(executable
(public_name chess-scribe-api)
(name main)
(libraries chess_scribe_api dream pgn_to_tex))
(libraries chess_scribe_api dream pgn_to_tex yojson))
+64 -2
View File
@@ -1,6 +1,68 @@
open Lwt.Syntax
open Yojson.Safe.Util
let is_dev_mode () =
Array.to_list Sys.argv
|> List.exists (fun arg -> arg = "DREAM_ENV=development")
(* Custom CORS Middleware *)
let cors_middleware next_handler request =
let allowed_origin =
if is_dev_mode () then "*" else "https://chess-scribe.org"
in
if Dream.method_ request = `OPTIONS then (
let response = Dream.response "" in
Dream.add_header response "Access-Control-Allow-Origin" allowed_origin;
Dream.add_header response "Access-Control-Allow-Methods"
"POST, GET, OPTIONS";
Dream.add_header response "Access-Control-Allow-Headers"
"Content-Type, Authorization";
Dream.set_status response `No_Content;
Lwt.return response)
else
(* 3. Pass normal requests through and append the origin header *)
let* response = next_handler request in
Dream.add_header response "Access-Control-Allow-Origin" allowed_origin;
Lwt.return response
let handle_convert request =
let* body = Dream.body request in
try
let json = Yojson.Safe.from_string body in
let pgn = json |> member "pgn" |> to_string in
let diagram_clock = json |> member "diagramClock" |> to_bool in
(* For diagrams, we grab the raw sub-tree as a string to pass along later *)
let diagrams_json = json |> member "diagrams" in
let diagrams_str = Yojson.Safe.to_string diagrams_json in
print_endline "\n--- Received Request ---";
Printf.printf "PGN:\n%s\n\n" pgn;
Printf.printf "Diagrams JSON String: %s\n" diagrams_str;
Printf.printf "Diagram Clock: %b\n" diagram_clock;
print_endline "------------------------\n";
Dream.json {|{"status": "ok", "message": "Payload parsed successfully!"}|}
with
| Type_error (msg, _) ->
Dream.json ~status:`Bad_Request
(Printf.sprintf
{|{"type": "error", "message": "JSON structure error: %s"}|} msg)
| Yojson.Json_error msg ->
Dream.json ~status:`Bad_Request
(Printf.sprintf
{|{"type": "error", "message": "Invalid JSON syntax: %s"}|} msg)
| exn ->
Dream.json ~status:`Internal_Server_Error
(Printf.sprintf {|{"type": "error", "message": "%s"}|}
(Printexc.to_string exn))
let () =
Dream.run ~interface:"0.0.0.0" ~port:8080
@@ Dream.logger
@@ Dream.logger @@ cors_middleware
@@ Dream.router
[
Dream.get "/" (fun request ->
@@ -8,7 +70,7 @@ let () =
(* Health check (Exact match) *)
Dream.get "/health" (fun _ -> Dream.respond "OK");
(* Specific API Endpoints (Must be ABOVE the wildcard) *)
Dream.get "/api/v1/pdf" (fun _ -> Dream.html "Generate PDF");
Dream.post "/api/v1/pdf" handle_convert;
(* Exact match for the docs root *)
Dream.get "/api/v1" (fun request ->
Dream.from_filesystem "static" "doc.html" request);