-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (50 loc) · 1.43 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
package main
import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
"gitlab.com/kamsandhu93/go-roulette/middleware"
"gitlab.com/kamsandhu93/go-roulette/roulette"
"os"
)
// Dependency injection of the spin wheel function used by the roulette route,
// allows tests to mock the winning number
func setupRouter(spinWheelFunc roulette.SpinWheelFunc) *gin.Engine {
router := gin.New() // without logger and recovery middleware
router.Use(gin.Logger())
router.Use(middleware.Logger()) //extra logging
router.Use(middleware.Auth())
// Recovery middleware recovers from any panics and writes a 500 if there was one.
router.Use(gin.Recovery())
router.GET("/health", func(c *gin.Context) {
c.String(200, "ok")
})
router.POST("/v1/roulette", func(context *gin.Context) {
roulette.PostHandler(context, spinWheelFunc)
})
return router
}
func SetUpBindValidation() {
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
err := v.RegisterValidation("validBetType", roulette.ValidBetType)
if err != nil {
panic(err)
}
}
}
func main() {
SetUpBindValidation()
router := setupRouter(roulette.SpinWheel)
port := getEnv("PORT", "8080")
host := getEnv("HOST", "localhost")
err := router.Run(host + ":" + port)
if err != nil {
panic(err)
}
}
func getEnv(key, _default string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return _default
}