-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
55 lines (43 loc) · 1.23 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
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/penglongli/gin-metrics/ginmetrics"
)
func main() {
engine := gin.Default()
// get global Monitor object
m := ginmetrics.GetMonitor()
// +optional set metric path, default /debug/metrics
m.SetMetricPath("/metrics")
// +optional set slow time, default 5s
m.SetSlowTime(10)
// +optional set request duration, default {0.1, 0.3, 1.2, 5, 10}
// used to p95, p99
m.SetDuration([]float64{0.1, 0.3, 1.2, 5, 10})
// set middleware for gin
m.Use(engine)
// configuration for static files and templates
engine.LoadHTMLFiles("templates/index.html")
engine.StaticFile("/favicon.ico", "favicon.ico")
engine.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"title": "Weather Service",
})
})
engine.GET("/api/weather", queryWeather) // get weather for city
engine.GET("/healthz", healthz)
engine.GET("/readyz", readyz)
ConnectDatabase()
engine.Run(port())
}
func queryWeather(c *gin.Context) {
weather := GetOrRetrieveWeather(c.Query("city"))
c.JSON(http.StatusOK, weather)
}
func healthz(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "UP"})
}
func readyz(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "UP"})
}