-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (102 loc) · 2.76 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"caching-workshop/db"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"net/http"
"os"
"strings"
"time"
"log"
)
var (
ListenAddr = "localhost:8080"
RedisAddr = "localhost:6379"
RedisPasswd = os.Getenv("REDIS_PASSWORD")
FastRivals = strings.ToLower(os.Getenv("FAST_RIVALS"))
)
func initRouter(database *db.Database) *gin.Engine {
r := gin.Default()
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "PUT", "POST"},
AllowHeaders: []string{"*"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
AllowOriginFunc: func(origin string) bool {
return true
},
MaxAge: 12 * time.Hour,
}))
r.POST("/points", func(c *gin.Context) {
var userJson db.User
if err := c.ShouldBindJSON(&userJson); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
user, err := database.SaveUser(&userJson)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"user": user})
})
r.GET("/points/:username", func(c *gin.Context) {
username := c.Param("username")
user, err := database.GetUser(username)
if err != nil {
if err == db.ErrNil {
c.JSON(http.StatusNotFound, gin.H{"error": "No record found for " + username})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"user": user})
})
r.GET("/leaderboard", func(c *gin.Context) {
leaderboard, err := database.GetLeaderboard()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"leaderboard": leaderboard})
})
r.GET("/rival/:username", func(c *gin.Context) {
username := c.Param("username")
user, err := database.GetRival(username)
if err != nil {
if err == db.ErrNil {
c.JSON(http.StatusNotFound, gin.H{"error": "No record found for " + username})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"user": user})
})
return r
}
func backgroundTasks(database *db.Database) {
for range time.Tick(time.Second * 10) {
err := database.UpdateRivals()
if err != nil {
log.Printf("Background task failed: %s", err)
}
}
}
func main() {
database, err := db.NewDatabase(RedisAddr, RedisPasswd)
if err != nil {
log.Fatalf("Failed to connect to redis: %s", err.Error())
}
if FastRivals == "true" {
log.Println("Fast rivals enabled, will run background process to update rival cache")
go backgroundTasks(database)
}
router := initRouter(database)
err = router.Run(ListenAddr)
if err != nil {
log.Fatalf("Router crashed with: %s", err)
}
}