Skip to content
This repository was archived by the owner on Jul 11, 2025. It is now read-only.

Commit 45ea47f

Browse files
committed
CORS middleware
Signed-off-by: Peter Baker <peter.baker122@csiro.au>
1 parent 80a8e1e commit 45ea47f

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/Middleware.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@ using HTTP
33
using Dates
44
using JSON
55

6+
const CORS_HEADERS = [
7+
"Access-Control-Allow-Origin" => "*",
8+
"Access-Control-Allow-Headers" => "*",
9+
"Access-Control-Allow-Methods" => "POST, GET, OPTIONS"
10+
]
11+
12+
# https://juliaweb.github.io/HTTP.jl/stable/examples/#Cors-Server
13+
function CorsMiddleware(handler)
14+
return function(req::HTTP.Request)
15+
@debug "CORS middleware"
16+
# determine if this is a pre-flight request from the browser
17+
if HTTP.method(req)=="OPTIONS"
18+
return HTTP.Response(200, CORS_HEADERS)
19+
else
20+
return handler(req) # passes the request to the AuthMiddleware
21+
end
22+
end
23+
end
24+
25+
626
function setup_jwt_middleware(config::Dict)
727
if !get(config["jwt_auth"], "JWT_ENABLED", false)
828
return identity # Return a pass-through middleware if JWT is not enabled

src/ReefGuideAPI.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ function start_server(config_path)
168168
println("Initialisation complete, starting server on port 8000.")
169169
println("Starting with $(Threads.nthreads()) threads...")
170170
if Threads.nthreads() > 1
171-
serveparallel(host="0.0.0.0", port=8000)
171+
serveparallel(middleware=[CorsMiddleware], host="0.0.0.0", port=8000)
172172
else
173-
serve(host="0.0.0.0", port=8000)
173+
serve(middleware=[CorsMiddleware], host="0.0.0.0", port=8000)
174174
end
175175
end
176176

0 commit comments

Comments
 (0)