こちらと同様の機能を Genie で実装します。
FastAPI: 簡単な WebAPI を作成
get02.jl
# --------------------------------------------------------------------
# get02.jl
#
# Jan/06/2022
# --------------------------------------------------------------------
using Genie
using Genie.Router
using Genie.Renderer.Json
using Genie.Requests
function launchServer(port)
Genie.config.run_as_server = true
Genie.config.server_host = "0.0.0.0"
Genie.config.server_port = port
#
data = Dict("t101" => Dict("name" => "草枕", "author" => "夏目漱石"),
"t102" => Dict("name" => "走れメロス", "author" => "太宰治"),
"t103" => Dict("name" => "千曲川のスケッチ", "author" => "島崎藤村"),
"t104" => Dict("name" => "高瀬舟", "author" => "森鴎外")
)
#
println("port set to $(port)")
route("/") do
"Hello."
end
route("/authors/:author_id") do
key = payload(:author_id)
json(data[key])
end
route("/all") do
json(data)
end
Genie.AppServer.startup()
end
# --------------------------------------------------------------------
launchServer(parse(Int, ARGS[1]))
# --------------------------------------------------------------------
サーバーの実行
julia get02.jl 8000
クライアントからアクセス
全件取得
$ http http://127.0.0.1:8000/all
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Genie/Julia/1.7.1
Transfer-Encoding: chunked
{
"t101": {
"author": "夏目漱石",
"name": "草枕"
},
"t102": {
"author": "太宰治",
"name": "走れメロス"
},
"t103": {
"author": "島崎藤村",
"name": "千曲川のスケッチ"
},
"t104": {
"author": "森鴎外",
"name": "高瀬舟"
}
}
id を指定して取得
$ http http://127.0.0.1:8000/authors/t102
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Genie/Julia/1.7.1
Transfer-Encoding: chunked
{
"author": "太宰治",
"name": "走れメロス"
}