Here’s a sample (this is on an older version of Genie, not sure if this still works but I can confirm it worked great at the time):
Genie.jl API
JuliaAPI/src/ ApiExample.jl:
using Genie
using Genie.Router
import Genie.Router: route
import Genie.Renderer: json
include("Sum.jl")
function launch_server(port)
Genie.config.run_as_server = true
Genie.config.server_host = "0.0.0.0"
Genie.config.server_port = port
Genie.config.cors_allowed_origins = ["*"]
route("/addxy") do
x = parse(Int, get(@params, :x, "0"))
y = parse(Int, get(@params, :y, "0"))
(:answer => addxy(x,y)) |> json
end
Genie.startup()
end
launch_server(parse(Int, ARGS[1]))
JuliaAPI/src/ Sum.jl:
function addxy(x::Int64, y::Int64)
return sum([x,y])
end
Vue.js frontend
The relevant code is just this:
import axios from 'axios'
export default {
name: 'HelloWorld',
data: () => ({
x: null,
y: null,
answer: null
}),
methods: {
async getAnswer() {
try {
const res = await axios.get('https://cors-anywhere.herokuapp.com/https://julia-api-example.herokuapp.com/addxy', {
params: {
x: this.x,
y: this.y
}
})
this.answer = res.data.answer
} catch (err) {
alert(err)
}
}
}
}
All of the code is in these two GitHub repos:
https://github.com/mthelm85/JuliaAPI
https://github.com/mthelm85/JuliaAPIClient
Also, I pushed both the API and the GUI to free Heroku dynos so you can test out the front-end here:
https://julia-api-client.herokuapp.com/
Since they are free dynos, they both go into “sleep mode” until someone hits the URL. It may take a few minutes for the GUI to “wake up” and then once you try to compute a sum it will reach out to the Julia API and it will also have to “wake up.” I also just tried it out and for the first few minutes it was giving me a CORS error which, oddly enough, stopped happening after a few minutes (it’s working fine at the time I’m writing this but will probably not work right for the first few minutes that you try). This was just a proof of concept so I never tried to work out the bugs (of which there appear to be many ).