Home > other >  Why is my Julia HTTP.jl handler function throwing an error?
Why is my Julia HTTP.jl handler function throwing an error?

Time:12-09

I am trying to turn some Julia code into a very basic local microservice, which accepts a POST request with some options supplied via JSON, runs a program, and returns a 200 response. My microservice code is here:

const ROUTERS = Dict()

function __init__()
    """Initialisation function to populate the global variables."""
    ROUTERS["ROUTER"] = HTTP.Router()
    HTTP.register!(ROUTERS["ROUTER"], "POST", "/run", run_program)
end

function run_program(req)
    """Takes a run request and passes it to the main program, before sending a 200 reponse to say no errors have occured."""
    setup = JSON3.read(req.body, Dict{String, Any})
    run_program(setup) 
    return HTTP.Response(200, JSON3.write("Success."))
end

function requestHandler(req)
    """Recieves incoming requests and passes them to the router. Returns the response object."""
    local resp
    resp = HTTP.handle(ROUTERS["ROUTER"], req)
    return resp
end

function run_service(port=8080)
    """Runs the microservice at the specified port."""
    HTTP.serve(requestHandler, "0.0.0.0", port)
end

__init__()

This code works with HTTP version 0.9.17, but I updated it to the new version 1.5.5. Now I receive this error whenever I make a request to the running service:

 LogLevel(1999): handle_connection handler error
│   exception =
│    UndefVarError: handle not defined

What am I missing here? Have I defined my handler function incorrectly somehow?

CodePudding user response:

There's no HTTP.handle function anymore in HTTP.jl version 1.x.

You'll probably want something like

"""
Receives incoming requests and passes them to the router. Returns the response object.
"""
function requestHandler(req)
    local resp
    resp = ROUTERS["ROUTER"](req)
    return resp
end

instead. Also note that docstrings need to be inserted before the function for the docsystem to pick them up.

  • Related