0
0

More than 3 years have passed since last update.

Redis の WebAPI (oak)

Posted at

こちらで定めた仕様を満たすサーバーのプログラムです。
Redis の WebAPI を作成

サーバープログラム

oak_webapi.ts
// ---------------------------------------------------------------
//  oak_webapi.ts
//
//                  May/21/2020
//
// ---------------------------------------------------------------
import { Application,Router } from "https://deno.land/x/oak/mod.ts"
import { connect } from "https://denopkg.com/keroxp/deno-redis/mod.ts"

const router = new Router()
router
  .post("/", (context) => {
    context.response.body = "WebAPI"
  })
  .post("/read", async context => {
    const body = await context.request.body()
    const key:string = body.value.key
    const rvalue = await read_proc(key)
    context.response.body = rvalue
    })
  .post("/insert", async context => {
    const body = await context.request.body()
    const key:string = body.value.key
    const value:string = JSON.stringify(body.value.value)
    const ok = await insert_proc(key,value)
    context.response.body = ok
    })
  .post("/list", async context => {
    const json_str = await list_proc()
    context.response.body = json_str
    })

const app = new Application()
app.use(router.routes())
app.use(router.allowedMethods())

console.log("*** start ***")
await app.listen({ port: 8000 })

// ---------------------------------------------------------------
async function read_proc(key:string)
{
    const client = await connect({ hostname: "127.0.0.1", port: 6379 })
    const reply:any = await client.get(key)

    return reply
}

// ---------------------------------------------------------------
async function insert_proc(key:string, value:string)
{
    console.log("*** insert_proc *** start ***")
    console.log("key = " + key)
    console.log("value = " + value)

    const client = await connect({ hostname: "127.0.0.1", port: 6379 })

    const ok = await client.set(key, value)

    console.log(ok)
    console.log("*** insert_proc *** end ***")

    return ok
}

// ---------------------------------------------------------------
async function list_proc()
{
    const client = await connect({ hostname: "127.0.0.1", port: 6379 })
    const rvalue = await client.executor.exec("keys", "*")

    return rvalue[1]
}

// ---------------------------------------------------------------

サーバーの実行コマンド

deno run --allow-net oak_webapi.ts

テストスクリプト

Read

curl -X POST -H "Content-Type: application/json" \
    -d '{"key": "t1751"}' http://localhost:8000/read

Insert

curl -X POST -H "Content-Type: application/json" \
    -d '{"key": "t1751", "value": {"name": "宇都宮","population": 37516,"date_mod": "2001-5-18"}}' \
    http://localhost:8000/insert

List

curl -X POST -H "Content-Type: application/json" \
        http://localhost:8000/list
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