Redis の WebAPI (Gin)

こちらで定めた仕様を満たすサーバーサイドのプログラムです。
[Redis の WebAPI を作成](https://qiita.com/ekzemplaro/items/1746b31beef382023f2d)

次のプログラムを改造しました。
[Gin の使い方](https://qiita.com/ekzemplaro/items/4285f95535b616fd6b28)

```go:redis_api.go
// ---------------------------------------------------------------
//
//	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
}

// ---------------------------------------------------------------
```

サーバーの実行

```bash
go run redis_api.go
```

テストスクリプト

read

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

insert

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

list

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