Redis 的 WebAPI (Gin)

这是一个符合我们设定规格的服务端程序。
我们要创建一个 Redis 的 WebAPI。

我改造了下面的程序。
Gin的使用方法。

// ---------------------------------------------------------------
//
//  redis_api.go
//
//                  Jan/28/2021
// ---------------------------------------------------------------
package main

import "github.com/gin-gonic/gin"
import "net/http"
import (
    "os"
    "fmt"
    "net"
    "strings"
    "encoding/json"
)

// ---------------------------------------------------------------
func main() {
    router := gin.Default()

    router.GET("/user/:name", func(c *gin.Context) {
        name := c.Param("name")
        c.String(http.StatusOK, "Hello %s", name)
    })

    router.POST("/redis_read", func(c *gin.Context) {
        key := c.PostForm("key")
        message := read_proc(key)
        c.String(http.StatusOK, message)
    })

    router.POST("/redis_insert", func(c *gin.Context) {
        key := c.PostForm("key")
        value := c.PostForm("value")
        message := insert_proc(key,value)
        c.String(http.StatusOK, message)
    })

    router.POST("/redis_list", func(c *gin.Context) {
        message := list_proc()
        c.String(http.StatusOK, message)
    })
    router.Run(":8080")
}

// ---------------------------------------------------------------
// [4]:
func read_proc(key string) string {
    fmt.Fprintf (os.Stderr,"*** read_proc ***\n")
    hostname := "localhost"
    port := "6379"

    conn, err := net.Dial ("tcp", hostname + ":" + port)
    if err != nil {
        fmt.Println(err)
        return "err"
        }

    json_str := mcached_socket_read_proc (conn,key)
    conn.Close ()

    return json_str
}

// ---------------------------------------------------------------
// [4-6]:
func mcached_socket_read_proc (conn net.Conn,key_in string) string {
    json_str := ""
    str_received := socket_read_proc (conn,key_in)

    lines := strings.Split(str_received,"\n")

    if (! strings.Contains(lines[0],"END")) {
        json_str = lines[1]
        }

    return  json_str
}

// ---------------------------------------------------------------
// [4-6-8]:
func socket_read_proc (conn net.Conn,key_in string) string {
    str_received := ""
    _, err := conn.Write([]byte("get " + key_in + "\r\n"))
    if err != nil {
        fmt.Println(err)
        return str_received
        }

    buf := make([]byte, 1024)
    nn, err := conn.Read(buf[:])
    if err != nil {
        fmt.Println(err)
        return str_received
        }

    str_received = string(buf[0:nn])

    return  str_received
}

// ---------------------------------------------------------------
// [6]:
func insert_proc(key string, value string) string {
    fmt.Fprintf (os.Stderr,"*** insert_proc ***\n")
    hostname := "localhost"
    port := "6379"

    conn, err := net.Dial ("tcp", hostname + ":" + port)
    if err != nil {
        fmt.Println(err)
        return "err"
        }

    redis_socket_write_proc (conn,key,value)

    conn.Close ()

    return "ok"
}

// ----------------------------------------------------------------
func redis_socket_write_proc (conn net.Conn,key_in string,json_str string) {
    fmt.Println (key_in)
    fmt.Println (json_str)

    comm_aa := "set " + key_in + " '" + json_str + "'\r\n"
    conn.Write([]byte(comm_aa))

    buf := make ([]byte,1024)
    conn.Read (buf[:])

    fmt.Println (string(buf[0:10]))
}

// ----------------------------------------------------------------
// [8]:
func list_proc() string {
    fmt.Fprintf (os.Stderr,"*** list_proc ***\n")
    hostname := "localhost"
    port := "6379"

    conn, err := net.Dial ("tcp", hostname + ":" + port)
    if err != nil {
        fmt.Println(err)
        return "err"
        }


    keys := key_get_proc(conn)

    conn.Close ()

    output, _ := json.Marshal(keys)

    return string(output)
}

// ---------------------------------------------------------------
// [8-4]:
func key_get_proc (conn net.Conn) []string {
    var keys []string

    string_received := string_get_proc (conn)

    if 0 < len (string_received) {
        datax := strings.Split (string_received,"\n")
        for _, pp := range datax {
            if (0 < len (pp)) && (pp[:1] == "t") {
                pp = strings.TrimRight(pp, "\r")
                keys = append(keys, pp)
                }
            }
        }

    return keys
}

// ----------------------------------------------------------------
// [8-4-4]:
func string_get_proc (conn net.Conn) string{
    str_received := ""

    comm_aa := "keys *" + "\r\n"

    _, err := conn.Write([]byte(comm_aa))
    if err != nil {
        fmt.Println(err)
        return str_received
        }

    buf := make([]byte, 1024)
    nn, err := conn.Read(buf[:])
    if err != nil {
        fmt.Println(err)
        return str_received
        }

    str_received = string(buf[0:nn])

    return str_received
}

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

服务器执行中

go run redis_api.go

测试脚本 (Chinese translation)

阅读

curl -X POST http://localhost:8080/redis_read -d key=t1851

插入

curl -X POST http://localhost:8080/redis_insert -d key=t1855 \
    -d value='{"name": "宇都宮","population": 8751206,"date_mod": "2021-1-16"}'

请列举一下

curl -X POST http://localhost:8080/redis_list