forked from fluxcd/flux-recv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (52 loc) · 1.38 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
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
flag "github.com/spf13/pflag"
)
const defaultApiBase = "http://localhost:3030/api/flux"
func main() {
mainArgs(os.Args[1:])
}
func mainArgs(args []string) {
var (
configFile string
listen string
)
flags := flag.NewFlagSet("flux-recv", flag.ExitOnError)
flags.StringVar(&configFile, "config", "fluxrecv.yaml", "path to config file for flux-recv") // TODO(michael): `flux-recv help config`
flags.StringVar(&listen, "listen", ":8080", "address to listen on")
bail := func(msg string) {
fmt.Fprintln(os.Stderr, msg)
os.Exit(1)
}
flags.Parse(args)
config, err := ConfigFromFile(configFile)
if err != nil {
bail(err.Error())
}
configDir := filepath.Dir(configFile)
apiBase := config.API
if apiBase == "" {
apiBase = defaultApiBase
}
for _, ep := range config.Endpoints {
digest, handler, err := HandlerFromEndpoint(configDir, apiBase, ep)
if err != nil {
bail(err.Error())
}
route := "/hook/" + digest
http.Handle(route, handler)
println("endpoint", ep.Source, "using key", filepath.Join(configDir, ep.KeyPath), "at", route)
}
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
})
http.ListenAndServe(listen, nil)
}