-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (89 loc) · 2.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
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
package main
import (
"io/ioutil"
"log"
"net/http"
"os"
"time"
"github.com/go-redis/redis"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/", func(c echo.Context) error {
return c.JSON(200, map[string]interface{}{
"pod": os.Getenv("HOSTNAME"),
"message": os.Getenv("SYSTEM_NAME"),
})
})
e.GET("/helloworld", func(c echo.Context) error {
response, err := http.Get("http://rm-service")
if err != nil {
return c.JSON(500, map[string]interface{}{
"error": err.Error(),
})
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
return c.JSON(500, map[string]interface{}{
"error": err.Error(),
})
}
return c.JSON(200, map[string]interface{}{
"message": string(contents),
})
})
e.GET("/secret", func(c echo.Context) error {
return c.JSON(200, map[string]interface{}{
"message": os.Getenv("SECRET_KEY"),
})
})
e.GET("/configmap", func(c echo.Context) error {
return c.JSON(200, map[string]interface{}{
"message": os.Getenv("ENV"),
})
})
e.GET("/sleep", func(c echo.Context) error {
if c.QueryParam("isSleep") == "true" {
time.Sleep(10 * time.Second)
return c.JSON(200, map[string]interface{}{
"pod": os.Getenv("HOSTNAME"),
"message": "sleep 10 second",
})
}
return c.JSON(200, map[string]interface{}{
"pod": os.Getenv("HOSTNAME"),
"message": "no sleep",
})
})
e.GET("/panic", func(c echo.Context) error {
log.Println("hostname: ", os.Getenv("HOSTNAME"))
panic("here")
return c.JSON(200, map[string]interface{}{
"message": os.Getenv("HOSTNAME"),
"currentPod": os.Getenv("HOSTNAME"),
})
})
e.GET("/reset", func(c echo.Context) error {
client := redis.NewClient(&redis.Options{
Addr: "rm-redis:6379",
Password: "", // no password set
DB: 0, // use default DB
})
_, err := client.Ping().Result()
if err != nil {
return c.JSON(500, map[string]interface{}{
"error": err.Error(),
})
}
client.Del("RM_COUNT").Err()
return c.JSON(200, map[string]interface{}{
"message": "Reset the redis count to 0",
})
})
e.Logger.Fatal(e.Start(":5000"))
}