Switch from HttpServer.jl to HTTP.jl

question
package

#1

Hi,

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”)

    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

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.

Thnaks


#2

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.


#3

Thank you for the recommendation.
Please consider the updated code.

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
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.


#4

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 (https://juliaweb.github.io/HTTP.jl/stable/) 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 :smile:


#5

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


#6

Here’s an example using Bukdu, an web server based on HTTP.jl

# https://discourse.julialang.org/t/switch-from-httpserver-jl-to-http-jl/19717

using Bukdu
using HTTP.Messages: setheader

struct SimulationController <: ApplicationController
    conn::Conn
end

function take_options(c::SimulationController)
    req = c.conn.request
    @info :req_headers req.headers
    @info :req_method_target (req.method, req.target)
    # setheader(req.response, "Access-Control-Allow-Origin" => "*")
    setheader(req.response, "Content-Length" => "0")
    nothing
end

function run_simulation(inputs_string::String)
    @info :inputs inputs_string
    # work
end

function run_simulation(c::SimulationController)
    inputs_string = String(c.conn.request.body)
    output = run_simulation(inputs_string)
    render(JSON, output)
end

routes() do
    Bukdu.options("/", SimulationController, take_options)
    Bukdu.options("/run", SimulationController, take_options)
    post("/run", SimulationController, run_simulation)
end

Bukdu.start(8080)

#=
curl -i -X OPTIONS http://localhost:8080/
curl -i -X OPTIONS http://localhost:8080/run
curl -i -H "Content-Type: text/plain" -d "data" http://localhost:8080/run
=#

#7

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.