Add subscription, with notification
This commit is contained in:
parent
0e080dd0a2
commit
031a1ef7ce
|
@ -1,9 +1,13 @@
|
|||
package main
|
||||
|
||||
import "log"
|
||||
|
||||
//import "strconv"
|
||||
import "net/http"
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
//import "strconv"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type subscriptionHandler struct {
|
||||
}
|
||||
|
@ -20,9 +24,9 @@ func (handler *subscriptionHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
|
|||
return
|
||||
}
|
||||
|
||||
// mode := r.Form.Get("hub.mode")
|
||||
// callback := r.Form.Get("hub.callback")
|
||||
// topic := r.Form.Get("hub.topic")
|
||||
mode := r.Form.Get("hub.mode")
|
||||
callback := r.Form.Get("hub.callback")
|
||||
topic := r.Form.Get("hub.topic")
|
||||
// leaseSecondsStr := r.Form.Get("hub.lease_seconds")
|
||||
// leaseSeconds, err := strconv.ParseInt(leaseSecondsStr, 10, 64)
|
||||
// if leaseSecondsStr != "" && err != nil {
|
||||
|
@ -30,6 +34,39 @@ func (handler *subscriptionHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
|
|||
// return
|
||||
// }
|
||||
// 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
|
||||
}
|
||||
|
||||
topicURL, err := url.Parse(topic)
|
||||
if err != nil {
|
||||
http.Error(w, "Can't parse url", 400)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(202)
|
||||
fmt.Fprint(w, "Accepted")
|
||||
|
||||
go func() {
|
||||
client := http.Client{}
|
||||
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
|
||||
} else if mode == "unsubcribe" {
|
||||
return
|
||||
} else {
|
||||
http.Error(w, "Unknown hub.mode", 400)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
Loading…
Reference in New Issue
Block a user