-
Notifications
You must be signed in to change notification settings - Fork 0
/
micro-leaderboard.go
70 lines (58 loc) · 1.66 KB
/
micro-leaderboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"fmt"
"os"
"time"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis"
"github.com/maguec/micro-leaderboard/handlers/app"
"github.com/maguec/micro-leaderboard/handlers/healthcheck"
"github.com/shokunin/contrib/ginrus"
"github.com/sirupsen/logrus"
)
// APIMiddleware will add the redis connection to the context
func APIMiddleware(r *redis.Client) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("redisConn", r)
c.Next()
}
}
func main() {
router := gin.New()
var redisHost string
var redisPort string
var listenPort string
if len(os.Getenv("REDIS_HOST")) > 0 {
redisHost = os.Getenv("REDIS_HOST")
} else {
redisHost = "localhost"
}
if len(os.Getenv("REDIS_PORT")) > 0 {
redisPort = os.Getenv("REDIS_PORT")
} else {
redisPort = "6379"
}
if len(os.Getenv("LISTEN_PORT")) > 0 {
listenPort = os.Getenv("LISTEN_PORT")
} else {
listenPort = "8080"
}
rClient := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", redisHost, redisPort),
Password: "", // no password set
DB: 0, // use default DB
})
router.Use(APIMiddleware(rClient))
logrus.SetFormatter(&logrus.JSONFormatter{})
router.Use(ginrus.Ginrus(logrus.StandardLogger(), time.RFC3339, true, "micro-leaderboard"))
// Start routes
router.GET("/health", healthcheck.HealthCheck)
router.GET("/", app.Root)
router.GET("/inc/:set/:member", app.Incr)
router.GET("/inc/:set/:member/:count", app.Incr)
router.GET("/member/:set/:member", app.GetRank)
router.GET("/board/:set", app.ShowBoard)
router.GET("/board/:set/:count", app.ShowBoard)
// RUN rabit run
router.Run(fmt.Sprintf(":%s", listenPort)) // listen and serve on 0.0.0.0:8080
}