-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample-go.go
141 lines (123 loc) · 3.59 KB
/
sample-go.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
134
135
136
137
138
139
140
141
package main
import (
"os"
"fmt"
"time"
"errors"
"math/rand"
ioutil "io/ioutil"
http "net/http"
)
// HealthBadCount suck it
var HealthBadCount = 0
func statusServer() {
http.HandleFunc("/ping", pingHandle)
http.HandleFunc("/health", healthHandle)
http.HandleFunc("/healthBad", healthBadHandle)
http.HandleFunc("/pvDataReturn", pvDataReturn)
http.HandleFunc("/pvDataSet", pvDataSet)
http.HandleFunc("/apiTest", downwardAPITester)
// create server that doesn't leave things open forever
s := &http.Server{
Addr: ":8080",
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
s.ListenAndServe()
}
func pingHandle(w http.ResponseWriter, r *http.Request){
if r.Method == "GET" {
fmt.Fprintf(w, "PONG\n")
}
}
func healthHandle(w http.ResponseWriter, r *http.Request){
if r.Method == "GET" {
fmt.Fprintf(w, "health\n")
}
}
func pvDataReturn(w http.ResponseWriter, r *http.Request){
if r.Method == "GET" {
pvData, err := ioutil.ReadFile("/mnt/cephfs/pvData")
if err != nil {
fmt.Fprintf(w, "PV not present\n")
} else {
fmt.Fprintf(w, string(pvData[:]) + "\n")
}
}
}
func pvDataSet(w http.ResponseWriter, r *http.Request){
if r.Method == "PUT" {
bytes := make([]byte, 100)
_, err := r.Body.Read(bytes)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Failed to read PUT data\n")
}
err = ioutil.WriteFile("/mnt/cephfs/pvData", bytes, os.ModePerm)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "PV not present\n")
} else {
fmt.Fprintf(w, "Data saved")
}
}
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func downwardAPITester(w http.ResponseWriter, r *http.Request){
bytes := make([]byte, 100)
_, err := r.Body.Read(bytes)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Failed to read PUT data\n")
}
err = ioutil.WriteFile("/var/log/containers/sample-go/" + randSeq(10), bytes, os.ModePerm)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "PV not present\n")
} else {
fmt.Fprintf(w, "Data saved")
}
}
func healthBadHandle(w http.ResponseWriter, r *http.Request){
if r.Method == "GET" {
HealthBadCount++
if HealthBadCount > 15 {
w.WriteHeader(http.StatusInternalServerError)
}
fmt.Fprintf(w, "health count %d next\n", HealthBadCount)
}
}
func namedOutput(filehandle *os.File) {
counter := 0
for {
time.Sleep(time.Second)
fmt.Fprintf(filehandle, "oh look! a message on %s! id: %d\n", filehandle.Name(), counter)
counter++
}
}
func forTesting(number, multiplier int) (calculated int, err error) {
if number * multiplier > 50 {
err = errors.New("Number too high")
} else {
calculated = number * multiplier
}
return
}
func main() {
custom, err := os.Create("/lorst")
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to open custom log handle")
} else {
go namedOutput(custom)
}
go namedOutput(os.Stderr)
go namedOutput(os.Stdout)
statusServer()
}