I recently upgraded to Julia 7 and one of the HttpServer.jl package is not compatible anymore. Im trying to switch to HTTP.jl instead but I am very new to this concept.
This is the how the current code looks:
function get_http_handler()
return HttpHandler() do request::Request, response::Response
log(“Request”)
end
response_body = JSON.json(output)
log(“Response body:”)
response = Response(response_body, headers)
log(“Response:”, response)
return response
end
function run_web_server()
http_handler = get_http_handler()
server = Server(http_handler)
run(server, 8080)
http.events[“error”] = (client, err) → begin
server = Server(http_handler)
run(server, 8080)
end
end
run_web_server()
What do I have to change to make it work with HTTP.jl ? Since, I am very new to this, I dint find the documentation as straight forward as i would like. Any help would be appreciated.
Before we can adequately assist you, would you mind please reading this post and then updating your post:
Specifically the parts about creating a “Minimum Working Example” (what does run_simulation() do, I don’t see it defined in your post), and using triple-backticks properly.
function run_web_server()
http_handler = get_http_handler()
server = Server(http_handler)
run(server, 8080)
http.events["error"] = (client, err) -> begin
server = Server(http_handler)
run(server, 8080)
end
end
run_web_server()
The run_simulation function is what calls our tool at the back end. It returns a simulation ID which is String as a response.
Re-pasting this and cleaning up a bit. You have some bad quotation marks that don’t parse, and a missing end statement:
using HTTP, JSON
function get_http_handler()
return HttpHandler() do request::Request, response::Response
log("Request")
headers = get_headers()
output = nothing
log(request.resource)
log(request.method)
if request.method == "OPTIONS"
return Response(200, headers)
elseif request.resource == "/run" && request.method == "POST"
inputs_string = String(request.data)
output = run_simulation(inputs_string)
end
response_body = JSON.json(output)
log("Response body:")
response = Response(response_body, headers)
log("Response:", response)
return response
end
end
function run_web_server()
http_handler = get_http_handler()
server = Server(http_handler)
run(server, 8080)
http.events["error"] = (client, err) -> begin
server = Server(http_handler)
run(server, 8080)
end
end
run_web_server()
Given that going from HttpServer.jl to HTTP.jl is not a simple changed using statement, I would recommend you read the HTTP.jl docs (Home · HTTP.jl) and try to put things together piece-by-piece. If I (or anyone else) gets some free time soon, I’ll try to figure it out on my own or provide some additional hints. However, the best way to fix this is to try translating it on your own, with the help of the docs and examples in the repository. That way you’ll know how to maintain and update it in the future too
Sorry, its my first time on Discourse and thank you for the guidelines. If there is a simple example on setting up a webserver that receives requests and sends out responds please do share. I will go through the documentation as well to understand it better.
Thank you
Thank you, I will check it out. In the future we plan on having an asynch web server. Can I do that using Bukdu? We mainly wanted to switch to HTTP.jl because it would give us more functionality moving forward.