-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (72 loc) · 2.61 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
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"strings"
"time"
"github.com/gin-gonic/gin"
)
// api represents data about a record api.
type api struct {
ResponseText string `json:"Text,omitempty"`
Version string `json:"Version,omitempty"`
Timestamp string `json:"Timestamp,omitempty"`
Environment map[string]string `json:"Environment,omitempty"`
}
// api slice to seed record api data.
var (
listenFlag = flag.String("listen", "0.0.0.0:8080", "address and port to listen")
textFlag = flag.String("text", "", "text to put in response")
versionFlag = flag.String("version", "", "display version information")
successCodeFlag = flag.Int("status-code", 200, "http response code, e.g.: 200")
erroCodeFlag = flag.Int("error-code", 503, "http response code for errors, e.g.: 503")
errorRateFlag = flag.Int("error-rate", 2, "error rate between 0 and 100, e.g.: 20 for 20%")
includeEnvFlag = flag.String("include-env-vars", "", "set the name of environment variables to include in response separated by comma")
includeTimestampFlag = flag.Bool("include-timestamp", false, "set to true to include timestamp in response")
readinessDelayFlag = flag.Int("readiness-delay", 0, "delay in seconds before readiness endpoint returns 200")
readinessDelayGitterFlag = flag.Int("readiness-gitter", 0, "max readiness gitter in seconds (random delay between 0 and this value)")
)
func main() {
flag.Parse()
router := gin.Default()
router.GET("/", getRequest)
router.GET("/ready", readiness)
gitter := 0
if *readinessDelayGitterFlag > 0 {
gitter = rand.Intn(*readinessDelayGitterFlag)
}
sleepTime := *readinessDelayFlag + gitter
fmt.Printf("Sleeping for %v seconds", sleepTime)
time.Sleep(time.Duration(sleepTime) * time.Second)
router.Run(*listenFlag)
}
func readiness(c *gin.Context) {
c.JSON(200, gin.H{"status": "ok"})
}
// getapi responds with the list of all api as JSON.
func getRequest(c *gin.Context) {
reponseCode := *successCodeFlag
chance := rand.Intn(99)
apiResponse := api{ResponseText: *textFlag}
if chance < *errorRateFlag {
reponseCode = *erroCodeFlag
}
if *versionFlag != "" {
c.Header("X-App-Version", *versionFlag)
apiResponse.Version = *versionFlag
}
if *includeEnvFlag != "" {
envVars := strings.Split(*includeEnvFlag, ",")
envMap := make(map[string]string)
for i := range envVars {
envMap[envVars[i]] = os.Getenv(envVars[i])
}
apiResponse.Environment = envMap //os.Getenv(*includeEnvFlag)
}
if *includeTimestampFlag {
apiResponse.Timestamp = time.Now().Format(time.RFC3339)
}
c.JSON(reponseCode, apiResponse)
}