-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.go
95 lines (84 loc) · 2.04 KB
/
demo.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)
var pod, container, call string
func id() string {
return fmt.Sprintf("I am pod:%s container:%s", pod, container)
}
func logId() {
t := time.NewTicker(time.Second)
go func() {
for {
<-t.C
log.Printf("%d %s\n", time.Now().Unix(), id())
}
}()
}
func callRemote(w http.ResponseWriter, _ *http.Request) {
resp, err := http.Get(call)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return
}
w.Write([]byte(fmt.Sprintf("Remote response: %s", string(body))))
}
func config(w http.ResponseWriter, _ *http.Request) {
dat, err := ioutil.ReadFile("/etc/app-config/file.conf")
if err != nil {
w.Write([]byte(err.Error()))
return
}
w.Write(dat)
}
func filesystem(w http.ResponseWriter, r *http.Request) {
var content []string
for k, v := range r.URL.Query() {
file := fmt.Sprintf("/tmp/%s", k)
if v[0] != "" {
err := ioutil.WriteFile(file, []byte(v[0]), 0644)
if err != nil {
log.Println(err)
}
}
if dat, err := ioutil.ReadFile(file); err != nil {
content = append(content, fmt.Sprintf("%s=%s", file, err.Error()))
} else {
content = append(content, fmt.Sprintf("%s=%s", file, dat))
}
}
w.Write([]byte(strings.Join(content, "\n")))
}
func hello(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(id()))
}
func main() {
bind := flag.String("listen", ":8080", "Address to listen on")
flag.StringVar(&call, "call", "http://localhost:8081", "Address to call when using 'call-remote'")
flag.StringVar(&pod, "pod", "", "Name of this pod")
flag.StringVar(&container, "container", "", "Name of container in the pod")
flag.Parse()
logId()
http.HandleFunc("/cfg", config)
http.HandleFunc("/fs", filesystem)
http.HandleFunc("/call", callRemote)
http.HandleFunc("/", hello)
log.Printf("Starting on %s\n", *bind)
err := http.ListenAndServe(*bind, nil)
if err != http.ErrServerClosed {
log.Println(err)
}
log.Println("bye")
}