-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (77 loc) · 1.7 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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"encoding/json"
"github.com/ghodss/yaml"
)
func main() {
hooks := parseYamlConfig()
for i := range hooks {
go hooks[i].init()
}
http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
fmt.Fprintf(w, "pong")
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
w.WriteHeader(200)
response, _ := json.MarshalIndent(hooks, "", "\t")
fmt.Fprintf(w, "Configuration:\n\n%s", string(response))
})
port := envDefault("PORT", "8080")
cert, key := os.Getenv("CERT"), os.Getenv("KEY")
var err error
if cert != "" && key != "" {
fmt.Println("Starting TLS server on port", port)
err = http.ListenAndServeTLS(":"+port, cert, key, nil)
} else {
fmt.Println("Starting server on port", port)
err = http.ListenAndServe(":"+port, nil)
}
handleError(err)
runCmd("sleep", []string{"1000"})
}
func parseYamlConfig() []webhook {
hooks := make([]webhook, 0)
configFile := envDefault("CONFIG_FILE", "config.yml")
data, err := ioutil.ReadFile(configFile)
handleError(err)
err = yaml.Unmarshal(data, &hooks)
handleError(err)
fmt.Printf("%+v\n\n", hooks)
return hooks
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func handleError(err error, fatal ...bool) {
if err != nil {
if len(fatal) > 0 && fatal[0] {
log.Fatal(err)
} else {
log.Print(err)
}
}
}
func envDefault(env, fallback string) (val string) {
val = os.Getenv(env)
if val == "" {
val = fallback
}
return val
}