This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (115 loc) · 2.71 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
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
142
143
package main
import (
"context"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"
)
func main() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
server, err := setup()
if err != nil {
panic(err)
}
go func() {
if err := server.ListenAndServe(); err != http.ErrServerClosed {
panic(err)
}
}()
<-sigc
timeout, timeoutCancel := context.WithTimeout(ctx, 10*time.Second)
defer timeoutCancel()
if err := server.Shutdown(timeout); err != nil {
panic(err)
}
}
func setup() (*http.Server, error) {
addr := ":" + os.Getenv("APP_PORT")
if addr == ":" {
addr += "8080"
}
mux := http.NewServeMux()
mux.Handle("/env", &envHandler{})
mux.Handle("/secrets", &secretHandler{})
mux.Handle("/", &defaultHandler{})
server := &http.Server{Addr: addr, Handler: mux}
return server, nil
}
type server struct{}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
type defaultHandler struct{}
func (dh *defaultHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(os.Stdout, "Received request for %s\n", r.URL)
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "OK")
}
type envHandler struct{}
type entry struct {
Key string
Value string
}
type envTable struct {
List []entry
}
func (eh *envHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(os.Stdout, "Received request for %s\n", r.URL)
envVars := os.Environ()
list := make([]entry, 0, len(envVars))
for _, envVar := range envVars {
pair := strings.SplitN(envVar, "=", 2)
list = append(list, entry{pair[0], pair[1]})
}
table := envTable{list}
t := template.Must(template.ParseFiles("templates/env.tmpl"))
err := t.Execute(w, table)
if err != nil {
panic(err)
}
}
type secretHandler struct{}
type secretTable struct {
Title string
List []secret
}
type secret struct {
Key string
Value string
}
func (sh *secretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(os.Stdout, "Received request for %s\n", r.URL)
dir := "/etc/secrets"
files, err := ioutil.ReadDir(dir)
if err != nil {
w.Write([]byte(err.Error()))
return
}
list := make([]secret, 0, len(files))
for _, file := range files {
if file.IsDir() {
continue
}
filepath := path.Join(dir, file.Name())
contents, err := ioutil.ReadFile(filepath)
if err != nil {
continue
}
list = append(list, secret{file.Name(), string(contents)})
}
table := secretTable{"Secrets", list}
t := template.Must(template.ParseFiles("templates/secrets.tmpl"))
err = t.Execute(w, table)
if err != nil {
panic(err)
}
}