0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Redis の WebAPI (Genie)

Posted at

こちらで定めた仕様を満たすサーバーサイドのプログラムです。
Genie で実装しました。
Redis の WebAPI を作成

redis01.jl
# --------------------------------------------------------------------
#	redis01.jl
#
#						Jan/09/2022
# --------------------------------------------------------------------
using Genie
using Genie.Router
using Genie.Renderer.Json
using Genie.Requests
using Redis

# --------------------------------------------------------------------
function redis_get(key_in)
	conn = RedisConnection()
	str_json = get(conn, key_in)
	disconnect(conn)
#
	str_json
end

# --------------------------------------------------------------------
function redis_list()
	conn = RedisConnection()
	set_keys = keys(conn,"t*")
	disconnect(conn)
#
	json(set_keys)
end

# --------------------------------------------------------------------
function redis_insert(key_in,value_in)
	conn = RedisConnection()
	set(conn,key_in,value_in)
	disconnect(conn)
#
	key_in
end

# --------------------------------------------------------------------
function launchServer(port)

	Genie.config.run_as_server = true
	Genie.config.server_host = "0.0.0.0"
	Genie.config.server_port = port
#
	println("port set to $(port)")

	route("/") do
		"Hello."
	end

	route("/redis_get", method = POST) do
		key = jsonpayload()["key"]
		redis_get(key)
#		"redis_get key = " * String(key)
	end

	route("/redis_list", method = POST) do
		redis_list()
	end

	route("/redis_insert", method = POST) do
#		"redis_list"
		key = jsonpayload()["key"]
		value = jsonpayload()["value"]
		redis_insert(key,value)
	end

	Genie.AppServer.startup()
end

# --------------------------------------------------------------------
launchServer(parse(Int, ARGS[1]))

# --------------------------------------------------------------------

サーバーの起動

julia redis01.jl 8000

#クライアントからアクセス#

キーの一覧

$ http POST http://127.0.0.1:8000/redis_list/
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Genie/Julia/1.7.1
Transfer-Encoding: chunked

[
    "t1855",
    "t1857",
    "t1853",
    "t1854",
    "t1852",
    "t1859",
    "t1856",
    "t1858",
    "t1851"
]

キーを指定して値の取得

$ http POST http://127.0.0.1:8000/redis_get/ key=t1852
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Genie/Julia/1.7.1
Transfer-Encoding: chunked

{
    "date_mod": "2013-1-16",
    "name": "敦賀",
    "population": 17395
}

値の挿入

$ http POST http://127.0.0.1:8000/redis_insert/ key=t1853 value='{"name": "宇都宮","population": 8751600,"date_mod": "2021-3-17"}'
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Genie/Julia/1.7.1
Transfer-Encoding: chunked

t1853
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?