-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
152 lines (129 loc) · 3.86 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
// TODO : add statistic on /stat url
// cleanup code (grep TODO/FIXME/XXX)
// add flags
// Add tests
// MAYBE: change topic/channels naming schema (add prefix, or allow real names)
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"go/build"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"text/template"
"github.com/gorilla/mux"
)
var (
addr = flag.String("addr", ":8080", "http service address")
assets = flag.String("assets", defaultAssetPath(), "path to assets")
addrNsqd = flag.String("nsqd-http", "localhost:4151", "nsqd HTTP address")
addrNsqdTCP = flag.String("nsqd", "", "nsqd TCP address")
addrNsqlookupd = flag.String("lookupd-http", "localhost:4161", "lookupd HTTP address")
homeTempl *template.Template
pid = os.Getpid()
uinqId = ""
// nsqd instances HTTP-balancer (or concrete nsqd HTTP-endpoint)
// nsqlookupd instances HTTP-balancer
addrNsqlookupdHTTP = ""
addrNsqdHTTP = ""
)
func init() {
hostName, err := os.Hostname()
if err != nil {
panic(err)
}
uinqId = (hostName + "_" + strconv.Itoa(pid))
if len(uinqId) > ChannelMaxLen {
uinqId = uinqId[0 : ChannelMaxLen-1]
}
}
func defaultAssetPath() string {
p, err := build.Default.Import("github.com/nordicdyno/go-pubsub", "", build.FindOnly)
if err != nil {
return "."
}
return filepath.Join(p.Dir, "resources")
}
func homeHandler(c http.ResponseWriter, req *http.Request) {
homeTempl.Execute(c, req.Host)
}
type PostMessage struct {
Channel string `json:"channel"`
//Data json.RawMessage `json:"data"`
Data map[string]interface{}
}
/*
type PostMessageData struct {
Content string
Timestamp uint64
Type string
UserEmail string `json:"user_email"`
UserId string `json:"user_id"`
UserProfileUrl string `json:"user_profile_url"`
}
*/
func main() {
flag.Parse()
homePath := filepath.Join(*assets, "home.html")
log.Println("homePath: " + homePath)
homeTempl = template.Must(template.ParseFiles(homePath))
addrNsqlookupdHTTP = "http://" + *addrNsqlookupd
addrNsqdHTTP = "http://" + *addrNsqd
log.Printf("vars: %s %s\n", addrNsqdHTTP, addrNsqlookupdHTTP)
go h.run()
r := mux.NewRouter()
r.HandleFunc("/", homeHandler)
r.HandleFunc("/channel/{channel}/event/chat_message/", postHandler).
Methods("POST")
http.HandleFunc("/socket/websocket", wsHandler)
http.Handle("/", r)
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
func postHandler(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
requestBody, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Println("ERROR: can't read http body")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
message := PostMessage{}
err = json.Unmarshal(requestBody, &message)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Println("ERROR: invalid JSON data: " + string(requestBody))
return
}
vars := mux.Vars(req)
log.Printf("postHandler/channel => %s\n", vars["channel"])
log.Printf("postHandler/message: %+v\n", message)
/*
bJSON, err := json.Marshal(message.Data)
if err != nil {
log.Println("json marshaling error: " + err.Error())
return
}
*/
httpclient := &http.Client{}
url := fmt.Sprintf(addrNsqdHTTP+"/pub?topic=%s", GenNSQtopicName(vars["channel"]))
//log.Printf("POST to %s; bJSON => «%s»\n", url, string(bJSON))
//nsqReq, err := http.NewRequest("POST", url, bytes.NewBuffer(bJSON))
log.Printf("POST to %s; bJSON => «%s»\n", url, string(requestBody))
nsqReq, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
nsqResp, err := httpclient.Do(nsqReq)
defer nsqResp.Body.Close()
// FIXME : use timeouts or other http client
if err != nil {
log.Println("NSQ publish error: " + err.Error())
return
}
log.Println("NSQ publish probably ok :)")
}