This commit is contained in:
Peter Stuifzand 2018-12-10 19:34:30 +01:00
commit 309b034180
2 changed files with 15 additions and 19 deletions

View File

@ -2,8 +2,8 @@ pipeline:
build: build:
image: golang image: golang
commands: commands:
- go get github.com/pstuifzand/websub-hub/cmd/hubserver - go get p83.nl/go/websub-hub/cmd/hubserver
- go build github.com/pstuifzand/websub-hub/cmd/hubserver - go build p83.nl/go/websub-hub/cmd/hubserver
publish: publish:
image: plugins/docker image: plugins/docker

View File

@ -70,12 +70,12 @@ func (handler *subscriptionHandler) handlePublish(w http.ResponseWriter, r *http
return err return err
} }
handler.incStat(fmt.Sprintf("publish.%s", topic)) // handler.incStat(fmt.Sprintf("publish.%s", topic))
if subs, e := handler.Subscribers[topic]; e { if subs, e := handler.Subscribers[topic]; e {
for _, sub := range subs { for _, sub := range subs {
handler.incStat(fmt.Sprintf("publish.post.%s.%s", topic, sub.Callback)) // handler.incStat(fmt.Sprintf("publish.post.%s.%s", topic, sub.Callback))
log.Printf("publish: creating post to %s\n", sub.Callback) log.Printf("publish: creating post to %s\n", sub.Callback)
postReq, err := http.NewRequest("POST", sub.Callback, strings.NewReader(string(feedContent))) postReq, err := http.NewRequest("POST", sub.Callback, strings.NewReader(string(feedContent)))
if err != nil { if err != nil {
@ -257,23 +257,23 @@ func (handler *subscriptionHandler) addSubscriberCallback(topic string, subscrib
} }
func (handler *subscriptionHandler) incStat(name string) { func (handler *subscriptionHandler) incStat(name string) {
if v, e := handler.Stats[name]; e { // if v, e := handler.Stats[name]; e {
handler.Stats[name] = Stat{LastUpdate: time.Now(), Updates: v.Updates + 1} // handler.Stats[name] = Stat{LastUpdate: time.Now(), Updates: v.Updates + 1}
} else { // } else {
handler.Stats[name] = Stat{LastUpdate: time.Now(), Updates: 1} // handler.Stats[name] = Stat{LastUpdate: time.Now(), Updates: 1}
} // }
handler.saveStats() // handler.saveStats()
} }
func (handler *subscriptionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (handler *subscriptionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet { if r.Method == http.MethodGet {
fmt.Fprintln(w, "WebSub hub") fmt.Fprintln(w, "WebSub hub")
if r.URL.Query().Get("debug") == "1" { if r.URL.Query().Get("debug") == "1" {
handler.incStat("http.index.debug") // handler.incStat("http.index.debug")
enc := json.NewEncoder(w) enc := json.NewEncoder(w)
enc.SetIndent("", " ") enc.SetIndent("", " ")
enc.Encode(handler.Subscribers) enc.Encode(handler.Subscribers)
enc.Encode(handler.Stats) //enc.Encode(handler.Stats)
} }
return return
} }
@ -310,9 +310,7 @@ func (handler *subscriptionHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
func (handler *subscriptionHandler) loadStats() error { func (handler *subscriptionHandler) loadStats() error {
file, err := os.Open("./stats.json") file, err := os.Open("./stats.json")
if err != nil { if err != nil {
if os.IsExist(err) { if os.IsNotExist(err) {
return err
} else {
handler.Stats = make(map[string]Stat) handler.Stats = make(map[string]Stat)
return nil return nil
} }
@ -329,9 +327,7 @@ func (handler *subscriptionHandler) loadSubscriptions() error {
file, err := os.Open("./subscription.json") file, err := os.Open("./subscription.json")
if err != nil { if err != nil {
if os.IsExist(err) { if os.IsNotExist(err) {
return err
} else {
handler.Subscribers = make(map[string][]Subscriber) handler.Subscribers = make(map[string][]Subscriber)
return nil return nil
} }
@ -347,7 +343,7 @@ func (handler *subscriptionHandler) load() error {
if err != nil { if err != nil {
return err return err
} }
return handler.loadStats() return nil //handler.loadStats()
} }
func (handler *subscriptionHandler) saveStats() error { func (handler *subscriptionHandler) saveStats() error {