Cleanup hub handler
This commit is contained in:
parent
4c55813366
commit
ddbff28d8a
|
|
@ -20,42 +20,34 @@ func RandStringBytes(n int) string {
|
|||
}
|
||||
|
||||
type subscriptionHandler struct {
|
||||
Subscribers []string
|
||||
}
|
||||
|
||||
func (handler *subscriptionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
|
||||
http.Error(w, "Bad Request", 400)
|
||||
return
|
||||
func (handler *subscriptionHandler) handlePublish(w http.ResponseWriter, r *http.Request) error {
|
||||
topic := r.Form.Get("hub.topic")
|
||||
log.Printf("Topic = %s\n", topic)
|
||||
return nil
|
||||
}
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
http.Error(w, "Bad Request", 400)
|
||||
return
|
||||
}
|
||||
|
||||
mode := r.Form.Get("hub.mode")
|
||||
func (handler *subscriptionHandler) handleSubscription(w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Form.Encode())
|
||||
callback := r.Form.Get("hub.callback")
|
||||
topic := r.Form.Get("hub.topic")
|
||||
leaseSecondsStr := r.Form.Get("hub.lease_seconds")
|
||||
_, err = strconv.ParseInt(leaseSecondsStr, 10, 64)
|
||||
_, err := strconv.ParseInt(leaseSecondsStr, 10, 64)
|
||||
if leaseSecondsStr != "" && err != nil {
|
||||
http.Error(w, "hub.lease_seconds is used, but not valid", 400)
|
||||
return
|
||||
return nil
|
||||
}
|
||||
//secret := r.Form.Get("hub.secret")
|
||||
|
||||
if mode == "subscribe" {
|
||||
callbackURL, err := url.Parse(callback)
|
||||
if err != nil {
|
||||
http.Error(w, "Can't parse url", 400)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
topicURL, err := url.Parse(topic)
|
||||
if err != nil {
|
||||
http.Error(w, "Can't parse url", 400)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
w.WriteHeader(202)
|
||||
|
|
@ -77,16 +69,33 @@ func (handler *subscriptionHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
|
|||
req, err := http.NewRequest(http.MethodGet, callbackURL.String(), nil)
|
||||
res, err := client.Do(req)
|
||||
|
||||
// req, err := http.NewRequest("POST", callbackURL.String(), strings.NewReader("TEST"))
|
||||
// req.Header.Add("Content-Type", "text/plain")
|
||||
// req.Header.Add("Link", fmt.Sprintf("<https://hub.stuifzandapp.com/>; rel=hub, <%s>; rel=self", topicURL.String()))
|
||||
// res, err := client.Do(req)
|
||||
log.Println(res, err)
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (handler *subscriptionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
|
||||
http.Error(w, "Bad Request", 400)
|
||||
return
|
||||
}
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
http.Error(w, "Bad Request", 400)
|
||||
return
|
||||
}
|
||||
|
||||
mode := r.Form.Get("hub.mode")
|
||||
|
||||
if mode == "subscribe" {
|
||||
handler.handleSubscription(w, r)
|
||||
return
|
||||
} else if mode == "unsubcribe" {
|
||||
return
|
||||
} else if mode == "publish" {
|
||||
handler.handlePublish(w, r)
|
||||
} else {
|
||||
http.Error(w, "Unknown hub.mode", 400)
|
||||
return
|
||||
|
|
@ -94,6 +103,9 @@ func (handler *subscriptionHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
|
|||
}
|
||||
|
||||
func main() {
|
||||
http.Handle("/", &subscriptionHandler{})
|
||||
handler := &subscriptionHandler{}
|
||||
//handler.Subscribers
|
||||
|
||||
http.Handle("/", handler)
|
||||
log.Fatal(http.ListenAndServe(":80", nil))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user