mirror of
https://github.com/ANL-CEEESA/UnitCommitment.jl.git
synced 2025-12-06 00:08:52 -06:00
web: backend: Add CORS endpoints
This commit is contained in:
@@ -9,11 +9,17 @@ struct ServerHandle
|
|||||||
processor::JobProcessor
|
processor::JobProcessor
|
||||||
end
|
end
|
||||||
|
|
||||||
|
RESPONSE_HEADERS = [
|
||||||
|
"Access-Control-Allow-Origin" => "*",
|
||||||
|
"Access-Control-Allow-Methods" => "GET, POST, OPTIONS",
|
||||||
|
"Access-Control-Allow-Headers" => "Content-Type",
|
||||||
|
]
|
||||||
|
|
||||||
function submit(req, processor::JobProcessor)
|
function submit(req, processor::JobProcessor)
|
||||||
# Check if request body is empty
|
# Check if request body is empty
|
||||||
compressed_body = HTTP.payload(req)
|
compressed_body = HTTP.payload(req)
|
||||||
if isempty(compressed_body)
|
if isempty(compressed_body)
|
||||||
return HTTP.Response(400, "Error: No file provided")
|
return HTTP.Response(400, RESPONSE_HEADERS, "Error: No file provided")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Validate compressed JSON by decompressing and parsing
|
# Validate compressed JSON by decompressing and parsing
|
||||||
@@ -21,7 +27,11 @@ function submit(req, processor::JobProcessor)
|
|||||||
decompressed_data = transcode(GzipDecompressor, compressed_body)
|
decompressed_data = transcode(GzipDecompressor, compressed_body)
|
||||||
JSON.parse(String(decompressed_data))
|
JSON.parse(String(decompressed_data))
|
||||||
catch e
|
catch e
|
||||||
return HTTP.Response(400, "Error: Invalid compressed JSON")
|
return HTTP.Response(
|
||||||
|
400,
|
||||||
|
RESPONSE_HEADERS,
|
||||||
|
"Error: Invalid compressed JSON",
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Generate random job ID (lowercase letters and numbers)
|
# Generate random job ID (lowercase letters and numbers)
|
||||||
@@ -40,7 +50,7 @@ function submit(req, processor::JobProcessor)
|
|||||||
|
|
||||||
# Return job ID as JSON
|
# Return job ID as JSON
|
||||||
response_body = JSON.json(Dict("job_id" => job_id))
|
response_body = JSON.json(Dict("job_id" => job_id))
|
||||||
return HTTP.Response(200, response_body)
|
return HTTP.Response(200, RESPONSE_HEADERS, response_body)
|
||||||
end
|
end
|
||||||
|
|
||||||
function jobs_view(req)
|
function jobs_view(req)
|
||||||
@@ -53,7 +63,7 @@ function jobs_view(req)
|
|||||||
|
|
||||||
# Check if job directory exists
|
# Check if job directory exists
|
||||||
if !isdir(job_dir)
|
if !isdir(job_dir)
|
||||||
return HTTP.Response(404, "Job not found")
|
return HTTP.Response(404, RESPONSE_HEADERS, "Job not found")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Read log file if it exists
|
# Read log file if it exists
|
||||||
@@ -65,13 +75,10 @@ function jobs_view(req)
|
|||||||
output_content = isfile(output_path) ? read(output_path, String) : nothing
|
output_content = isfile(output_path) ? read(output_path, String) : nothing
|
||||||
|
|
||||||
# Create response JSON
|
# Create response JSON
|
||||||
response_data = Dict(
|
response_data = Dict("log" => log_content, "solution" => output_content)
|
||||||
"log" => log_content,
|
|
||||||
"solution" => output_content
|
|
||||||
)
|
|
||||||
|
|
||||||
response_body = JSON.json(response_data)
|
response_body = JSON.json(response_data)
|
||||||
return HTTP.Response(200, response_body)
|
return HTTP.Response(200, RESPONSE_HEADERS, response_body)
|
||||||
end
|
end
|
||||||
|
|
||||||
function start_server(host, port; optimizer)
|
function start_server(host, port; optimizer)
|
||||||
@@ -95,6 +102,7 @@ function start_server(host, port; optimizer)
|
|||||||
UnitCommitment.optimize!(model)
|
UnitCommitment.optimize!(model)
|
||||||
solution = UnitCommitment.solution(model)
|
solution = UnitCommitment.solution(model)
|
||||||
UnitCommitment.write(solution_filename, solution)
|
UnitCommitment.write(solution_filename, solution)
|
||||||
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -115,6 +123,14 @@ function start_server(host, port; optimizer)
|
|||||||
|
|
||||||
router = HTTP.Router()
|
router = HTTP.Router()
|
||||||
|
|
||||||
|
# Register CORS preflight endpoint
|
||||||
|
HTTP.register!(
|
||||||
|
router,
|
||||||
|
"OPTIONS",
|
||||||
|
"/**",
|
||||||
|
req -> HTTP.Response(200, RESPONSE_HEADERS, ""),
|
||||||
|
)
|
||||||
|
|
||||||
# Register /submit endpoint
|
# Register /submit endpoint
|
||||||
HTTP.register!(router, "POST", "/submit", req -> submit(req, processor))
|
HTTP.register!(router, "POST", "/submit", req -> submit(req, processor))
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ function jobs_test_usage()
|
|||||||
received_job_id = []
|
received_job_id = []
|
||||||
function work_fn(job_id)
|
function work_fn(job_id)
|
||||||
push!(received_job_id, job_id)
|
push!(received_job_id, job_id)
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
# Create processor with work function
|
# Create processor with work function
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ function server_test_usage()
|
|||||||
@test view_data["solution"] !== nothing
|
@test view_data["solution"] !== nothing
|
||||||
|
|
||||||
# Clean up
|
# Clean up
|
||||||
rm(job_dir, recursive=true)
|
rm(job_dir, recursive = true)
|
||||||
finally
|
finally
|
||||||
stop(server)
|
stop(server)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user