-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
61 lines (50 loc) · 1.24 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
package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
// Set the directory from which to load templates
r.LoadHTMLGlob("templates/*")
serviceName := os.Getenv("SERVICE")
version := os.Getenv("VERSION")
name := os.Getenv("NAME")
hostname, _ := os.Hostname()
color := os.Getenv("COLOR")
r.GET("/", func(c *gin.Context) {
data := map[string]interface{}{
"color": color,
"service": serviceName,
"version": version,
"instance": hostname,
"host": c.Request.Host,
"timestamp": time.Now().Format(time.RFC3339),
}
c.HTML(http.StatusOK, "index.html.tmpl", data)
})
r.GET("/hello", func(c *gin.Context) {
message := "Hello!"
if name != "" {
message = fmt.Sprintf("Hello, %s!", name)
}
message = fmt.Sprintf("%s, Instance: %s\n", message, hostname)
c.String(http.StatusOK, message)
})
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
"service": serviceName,
"version": version,
"instance": hostname,
"timestamp": time.Now().Format(time.RFC3339),
})
})
r.GET("/health", func(c *gin.Context) {
c.String(http.StatusOK, "HEALTHY")
})
r.Run() // listen and serve on 0.0.0.0:8080
}