package main

import (
	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis/v8"
	"net/http"
)

var client *redis.Client

func main() {

	client = redis.NewClient(&redis.Options{
		Addr:     "localhost:6379", // Redis服务器地址
		Password: "",               // 密码，如果没有密码就留空
		DB:       0,                // 使用的数据库
	})

	router := gin.Default()
	router.GET("/set", func(c *gin.Context) {
		err := client.Set(c, "name", "ZhangSan", 0).Err()
		if err != nil {
			c.String(http.StatusInternalServerError, err.Error())
			return
		}
		c.String(http.StatusOK, "Set key-value pair successfully")
	})

	router.GET("/get", func(c *gin.Context) {
		val, err := client.Get(c, "name").Result()
		if err != nil {
			c.String(http.StatusInternalServerError, err.Error())
			return
		}
		c.String(http.StatusOK, "name: "+val)
	})

	router.Run(":8080")

}
