Implement a REST server in Julia?

Some personal experience of JSON processing in a request and generating JSON as a response:

Implementation with Mux:

using Mux
using JSON

@app test = (
  Mux.defaults,
  page(respond("<h1>Hello World!</h1>")),

  page("/user/:user", req -> "<h1>Hello, $(req[:params][:user])!</h1>"),

  route("/resource/process", req -> begin
    obj = JSON.parse(String(req[:data]))

    @show obj

    Dict(:body => String(JSON.json(obj)),
         :headers => Dict("Content-Type" => "application/json")
         )
  end),

  Mux.notfound())

serve(test, 8080)

Base.JLOptions().isinteractive == 0 && wait()

Implementation with Bukdu:

using Bukdu
using JSON

struct WelcomeController <: ApplicationController
    conn::Conn
end

function index(c::WelcomeController)
    render(JSON, "Hello World")
end

function process_resource(c::WelcomeController)
    json = JSON.parse(String(c.conn.request.body))

    @show json

    render(JSON, json)
end

routes() do
    get("/", WelcomeController, index)
    post("/resource/process", WelcomeController, process_resource)
end

Bukdu.start(8080)

Base.JLOptions().isinteractive == 0 && wait()
4 Likes