forked from Polyconseil/k8s-proxy-image-swapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (71 loc) · 1.72 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
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
m "github.com/Polyconseil/k8s-proxy-image-swapper/mutate"
"gopkg.in/yaml.v2"
"html"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
func handleRoot(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello %q", html.EscapeString(r.URL.Path))
}
func handleMutation(w http.ResponseWriter, r *http.Request) {
debug := os.Getenv("LOGLEVEL") == "DEBUG"
if debug {
fmt.Println("handleMutation called")
}
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
log.Println(err)
fmt.Fprintf(w, "%s", err)
return
}
registry := os.Getenv("REGISTRY_URL")
mutated, err := m.Mutate(body, debug, registry)
if err != nil {
log.Println(err)
fmt.Fprintf(w, "%s", err)
return
}
w.WriteHeader(http.StatusOK)
_, err = w.Write(mutated)
if err != nil {
log.Println("Failed writing HTTP response")
}
}
func main() {
if len(os.Args) == 1 {
log.Fatalf("Usage : %v $CONFIG_FILE_PATH\n", os.Args[0])
return
}
configFile, err := os.Open(os.Args[1])
if err != nil {
log.Fatalf("Error opening %v : %v\n", os.Args[1], err)
}
var config m.Config
decoder := yaml.NewDecoder(configFile)
err = decoder.Decode(&config)
if err != nil {
log.Fatalf("Error reading config : %v\n", err)
}
configFile.Close()
m.Configuration = config
log.Println("Starting server ...")
mux := http.NewServeMux()
mux.HandleFunc("/", handleRoot)
mux.HandleFunc("/mutate", handleMutation)
s := &http.Server{
Addr: ":" + config.Port,
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20, // 1_048_576
}
log.Fatal(s.ListenAndServeTLS(config.TLSCertPath, config.TLSKeyPath))
}