Skip to content

Instantly share code, notes, and snippets.

@clsung
Created September 27, 2016 04:40
Show Gist options
  • Save clsung/b69a154a09623cd743affbed26077033 to your computer and use it in GitHub Desktop.
Save clsung/b69a154a09623cd743affbed26077033 to your computer and use it in GitHub Desktop.
gin with redigo
package main
import (
"flag"
"github.com/garyburd/redigo/redis"
"github.com/gin-gonic/gin"
)
var (
redisAddress = flag.String("redis-address", ":6379", "Address to the Redis server")
)
func main() {
router := gin.Default()
router.GET("/ready", checkRedis)
router.GET("/ready/:auth", checkRedisWithAuth)
router.Run()
}
func checkRedisWithAuth(c *gin.Context) {
auth := c.Param("auth")
doRedis(c, "PING", auth)
}
func checkRedis(c *gin.Context) {
doRedis(c, "PING", "")
}
func doRedis(c *gin.Context, cmd, auth string) {
client, err := redis.Dial("tcp", *redisAddress)
if err != nil {
c.JSON(500, gin.H{
"message": err,
})
}
defer client.Close()
if auth != "" {
if _, err := client.Do("AUTH", auth); err != nil {
c.JSON(400, gin.H{
"message": "Invalid password",
})
return
}
}
ret, err := client.Do(cmd)
if err != nil {
c.JSON(500, gin.H{
"message": err,
})
}
c.JSON(200, gin.H{
"message": ret,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment