-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (61 loc) · 1.56 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
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/deadmanssnitch/go-dmswebhooks"
"github.com/goji/httpauth"
)
type Config struct {
Token string
Room string
Hostname string
Password string
}
func main() {
var err error
cfg := &Config{
Token: os.Getenv("HIPCHAT_TOKEN"),
Room: os.Getenv("HIPCHAT_ROOM"),
Hostname: os.Getenv("HIPCHAT_HOSTNAME"),
Password: os.Getenv("DMS_PASSWORD"),
}
// Default to HipChat cloud
if cfg.Hostname == "" {
cfg.Hostname = "api.hipchat.com"
}
// HIPCHAT_TOKEN is required and is used for authentication. This example
// requires a User token as the add-on tokens require oauth.
if cfg.Token == "" {
log.Fatalf("Missing HIPCHAT_TOKEN. Generate a new User API token.")
}
// HIPCHAT_ROOM is required an specifies which room to notify with alerts.
if cfg.Room == "" {
log.Fatalf("Missing HIPCHAT_ROOM. Set this to the Room ID to post to.")
}
handler := dmswebhooks.NewHandler(
func(alert *dmswebhooks.Alert) error {
notice := newNotificiation(alert)
return notifyHipchat(cfg, notice)
},
)
if cfg.Password != "" {
handler = httpauth.SimpleBasicAuth(cfg.Password, "")(handler)
}
var port = "4000"
if envPort := os.Getenv("PORT"); envPort != "" {
port = envPort
}
log.Printf("Started Listening on tcp://0.0.0.0:%v\n", port)
server := http.Server{
Addr: ":" + port,
Handler: handler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
}
err = server.ListenAndServe()
if err != nil {
log.Printf("HTTP Server Error: %q\n", err.Error())
}
}