-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
116 lines (90 loc) · 2.41 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"github.com/gorilla/mux"
)
type yoedConfig struct {
Listen string `json:"listen"`
}
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 dispatch(handler, username, handle string) {
resp, err := http.PostForm(handler, url.Values{
"username": {username},
"handle": {handle},
})
if err != nil {
log.Printf("Error while dispatching message to %s: %s", handler, err)
log.Printf("Remove handler %s", handler)
} else {
log.Printf("Handler %s status: %s", handler, resp.Status)
}
}
func main() {
config, err := loadConfig("./config.json")
if err != nil {
panic(fmt.Sprintf("failed loading config: %s", err))
}
handlers := make(map[string]map[string]bool, 0)
router := mux.NewRouter()
router.HandleFunc("/yo", func(w http.ResponseWriter, r *http.Request) {
strHandles := r.FormValue("handles")
callbackUrl := r.FormValue("callback_url")
if strHandles == "" || callbackUrl == "" {
errorMsg := "Handles and callback_url are mandatory"
log.Printf("Error on subcribe: %s", errorMsg)
http.Error(w, errorMsg, 400)
return
}
handles := strings.Split(strHandles, ",")
for _, handle := range handles {
log.Printf("Subscribe %s: %s", handle, callbackUrl)
if handlers[handle] == nil {
handlers[handle] = make(map[string]bool, 0)
}
handlers[handle][callbackUrl] = true
}
})
router.HandleFunc(`/yoed/{handle:[a-z0-9]+}`, func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
handle := vars["handle"]
username := r.FormValue("username")
log.Printf("got a YO from %s on %s", username, handle)
if 0 == len(handlers) || nil == handlers[handle] || 0 == len(handlers[handle]) {
log.Printf("No handler registered for handle %s", handle)
return
}
for handler, _ := range handlers[handle] {
log.Printf("Dispatching to handler %s", handler)
go dispatch(handler, username, handle)
}
})
server := http.Server{
Addr: config.Listen,
Handler: router,
}
log.Printf("Listening...")
if err := server.ListenAndServe(); err != nil {
fmt.Println(err)
}
}