forked from DanielStefanK/fitx-utilization-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
133 lines (105 loc) · 2.97 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"net/http"
"strconv"
"time"
"github.com/DanielStefanK/fitx-utilization-proxy/responses"
"github.com/DanielStefanK/fitx-utilization-proxy/store"
ginzap "github.com/gin-contrib/zap"
"go.uber.org/zap"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.New()
store := store.NewStore()
logger, _ := zap.NewProduction()
router.Use(ginzap.Ginzap(logger, time.RFC3339, true))
logger.Info("creating new interval to fetch store info")
ticker := time.NewTicker(6 * time.Hour)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
logger.Info("updating store info")
store.UpdateStudios()
case <-quit:
ticker.Stop()
return
}
}
}()
router.GET("/api/utilization/:studioId", func(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("studioId"), 10, 64)
if err != nil {
c.PureJSON(404, &responses.ErrorResponse{Message: "could not parse studio id"})
return
}
if !store.StudioExists(id) {
c.PureJSON(404, responses.ErrorResponse{
Message: "studio with provided id could not be found",
})
return
}
logger.Info("getting utilization for studio", zap.Uint64("studioId", id), zap.String("operation", "getUtilization"))
resp := store.Get(id)
if resp == nil {
c.PureJSON(404, responses.ErrorResponse{
Message: "Could not get utilization for provided studio id",
})
return
}
c.PureJSON(200, resp)
})
router.GET("/api/utilization-by-lat-lon", func(c *gin.Context) {
lat, err := strconv.ParseFloat(c.Query("lat"), 64)
if err != nil {
c.PureJSON(404, &responses.ErrorResponse{Message: "could not parse latitude"})
return
}
long, err := strconv.ParseFloat(c.Query("lon"), 64)
if err != nil {
c.PureJSON(404, &responses.ErrorResponse{Message: "could not parse longitude"})
return
}
logger.Info("get studios by lat and long", zap.Float64("lat", lat), zap.Float64("long", long), zap.String("operation", "getStudioByLocation"))
resp := store.GetClosest(lat, long)
if resp == nil {
c.PureJSON(404, responses.ErrorResponse{
Message: "Could not get utilization for provided studio id",
})
return
}
c.PureJSON(200, resp)
})
router.GET("/api/studios", func(c *gin.Context) {
resp := store.GetStudios()
if resp == nil {
c.PureJSON(404, responses.ErrorResponse{
Message: "Could not get studio infos",
})
return
}
c.PureJSON(200, resp)
})
router.Use(middleware("/", "./static"))
router.Run(":8080")
}
func middleware(urlPrefix, spaDirectory string) gin.HandlerFunc {
directory := static.LocalFile(spaDirectory, true)
fileserver := http.FileServer(directory)
if urlPrefix != "" {
fileserver = http.StripPrefix(urlPrefix, fileserver)
}
return func(c *gin.Context) {
if directory.Exists(urlPrefix, c.Request.URL.Path) {
fileserver.ServeHTTP(c.Writer, c.Request)
c.Abort()
} else {
c.Request.URL.Path = "/"
fileserver.ServeHTTP(c.Writer, c.Request)
c.Abort()
}
}
}