This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
100 lines (75 loc) · 1.76 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
100
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
type yoedConfig struct {
Listen string `json:"listen"`
Handlers struct {
Slack *struct {
WebhookUrl string `json:"webhook_url"`
} `json:"slack"`
Yoback *struct {
ApiToken string `json:"api_token"`
} `json:"yoback"`
} `json:"handlers"`
}
type yoedHandler interface {
Handle(username string)
}
func loadConfig(configPath string) (*yoedConfig, error) {
configFile, err := os.Open(configPath)
if err != nil {
return nil, err
}
configJson, err := ioutil.ReadAll(configFile)
if err != nil {
return nil, err
}
config := &yoedConfig{}
if err := json.Unmarshal(configJson, config); err != nil {
return nil, err
}
return config, nil
}
func main() {
config, err := loadConfig("./config.json")
if err != nil {
panic(fmt.Sprintf("failed loading config: %s", err))
}
handlers := make(map[string]yoedHandler)
log.Println("Adding handlers")
if slack := config.Handlers.Slack; slack != nil {
log.Println("Adding slack handler")
handlers["slack"] = &slackHandler{
webhookUrl: slack.WebhookUrl,
}
}
if yoback := config.Handlers.Yoback; yoback != nil {
log.Println("Adding yoback handler")
handlers["yoback"] = &yobackHandler{
apiToken: yoback.ApiToken,
}
}
mux := http.NewServeMux()
mux.HandleFunc("/yoed", func(w http.ResponseWriter, r *http.Request) {
username := r.FormValue("username")
log.Printf("got a YO from %s", username)
for name, handler := range handlers {
log.Printf("handling using %s for %s", name, username)
handler.Handle(username)
}
})
server := http.Server{
Addr: config.Listen,
Handler: mux,
}
log.Printf("Listening...")
if err := server.ListenAndServe(); err != nil {
fmt.Println(err)
}
}