-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (66 loc) · 1.5 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
package main
import (
"C"
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/bondj/slslack/config"
"github.com/bondj/slslack/slack"
"github.com/bondj/slslack/softlayer"
"log"
"net/http"
)
var cfg = config.LoadConfig()
var (
client *http.Client
pool *x509.CertPool
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Println()
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
r.ParseForm()
var cn string
var cmd string
var token string
for key, values := range r.Form { // range over map
for _, value := range values { // range over []string
fmt.Println(key, value)
if key == "channel_name" {
cn = value
} else if key == "command" {
cmd = value
} else if key == "token" {
token = value
}
}
}
if token != cfg.Slack.Slacktoken {
http.Error(w, "Invalid Request", 401)
}
if r.Method == "POST" {
go func() {
fmt.Println("Channel ", cn)
fmt.Println("Command ", cmd)
var m slack.Message
m.Text = softlayer.GetNextInvoiceTotalAmount(client, cfg.Softlayer.User, cfg.Softlayer.Key)
m.Channel = "#" + cn
slack.SendMessage(client, m, cfg.Slack.Target)
}()
} else {
http.Error(w, "Invalid request method.", 405)
}
})
pool = x509.NewCertPool()
fmt.Println(pool.AppendCertsFromPEM(pemCerts))
client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{RootCAs: pool},
},
}
fmt.Println("Starting server")
log.Fatal(http.ListenAndServe(":8080", nil))
}