-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (91 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 (
"context"
"encoding/json"
"errors"
"flag"
"github.com/elnormous/contenttype"
"log/slog"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"time"
)
var prefixFlag string
var httpAddrFlag string
func init() {
flag.StringVar(&prefixFlag, "p", "CONFIG_", "prefix for filtering")
flag.StringVar(&httpAddrFlag, "http", ":8080", "http address")
}
func main() {
flag.Parse()
if prefixFlag == "" {
panic("prefix is required")
}
if httpAddrFlag == "" {
panic("http address is required")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
filteredEnv := make(map[string]string)
for _, e := range os.Environ() {
if i := strings.Index(e, "="); i >= 0 {
if envName, found := strings.CutPrefix(e[:i], prefixFlag); found {
filteredEnv[envName] = e[i+1:]
}
}
}
srv := &http.Server{Addr: httpAddrFlag}
srv.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
availableMediaTypes := []contenttype.MediaType{
contenttype.NewMediaType("plain/text"),
contenttype.NewMediaType("application/json"),
}
accepted, _, acceptError := contenttype.GetAcceptableMediaType(r, availableMediaTypes)
if acceptError != nil {
http.Error(w, acceptError.Error(), http.StatusInternalServerError)
return
}
switch accepted.String() {
case "plain/text":
w.Header().Set("Content-Type", "text/plain")
for k, v := range filteredEnv {
_, _ = w.Write([]byte(k + "=" + v + "\n"))
}
return
case "application/json":
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(filteredEnv)
return
}
})
g := sync.WaitGroup{}
g.Add(1)
go func() {
<-ctx.Done()
timeout, c := context.WithTimeout(context.Background(), 5*time.Second)
defer c()
defer slog.Info("server stopped")
defer g.Done()
_ = srv.Shutdown(timeout)
}()
go func() {
if err := srv.ListenAndServe(); err != nil {
if !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}
}()
slog.Info("server started", slog.String("prefix", prefixFlag), slog.String("http", httpAddrFlag))
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
select {
case <-stop:
slog.Info("signal captured, stopping")
cancel()
case <-ctx.Done():
}
g.Wait()
}