From 1e0f192ab4961552ae22951641725a377a448ded Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Tue, 19 Mar 2019 21:55:07 +0100 Subject: [PATCH] Cleanup errors and documentation --- cmd/eksterd/auth.go | 26 +++++++------------------- cmd/eksterd/hubbackend.go | 20 ++------------------ cmd/eksterd/incoming.go | 27 ++++++++------------------- 3 files changed, 17 insertions(+), 56 deletions(-) diff --git a/cmd/eksterd/auth.go b/cmd/eksterd/auth.go index bd85703..76f946b 100644 --- a/cmd/eksterd/auth.go +++ b/cmd/eksterd/auth.go @@ -1,20 +1,3 @@ -/* - ekster - microsub server - Copyright (C) 2018 Peter Stuifzand - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ package main import ( @@ -116,10 +99,15 @@ func setCachedTokenResponseValue(conn redis.Conn, key string, r *auth.TokenRespo // getCachedValue gets the cached value from Redis func getCachedValue(conn redis.Conn, key string, r *auth.TokenResponse) (bool, error) { values, err := redis.Values(conn.Do("HGETALL", key)) - if err == nil && len(values) > 0 { + if err != nil { + return false, fmt.Errorf("error while getting value from backend: %v", err) + } + + if len(values) > 0 { if err = redis.ScanStruct(values, r); err == nil { return true, nil } } - return false, fmt.Errorf("error while getting value from backend: %v", err) + + return false, fmt.Errorf("no cached value available") } diff --git a/cmd/eksterd/hubbackend.go b/cmd/eksterd/hubbackend.go index 3018a41..4968fcc 100644 --- a/cmd/eksterd/hubbackend.go +++ b/cmd/eksterd/hubbackend.go @@ -1,20 +1,3 @@ -/* - ekster - microsub server - Copyright (C) 2018 Peter Stuifzand - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ package main import ( @@ -50,6 +33,7 @@ type hubIncomingBackend struct { baseURL string } +// Feed contains information about the feed subscriptions type Feed struct { ID int64 `redis:"id"` Channel string `redis:"channel"` @@ -214,7 +198,7 @@ func (h *hubIncomingBackend) run() error { if feed.Callback == "" { feed.Callback = fmt.Sprintf("%s/incoming/%d", h.baseURL, feed.ID) } - log.Printf("Send resubscribe for %q on %q\n", feed.URL, feed.Hub) + log.Printf("Send resubscribe for %q on %q with callback %q\n", feed.URL, feed.Hub, feed.Callback) err := h.Subscribe(&feed) if err != nil { log.Printf("Error while subscribing: %s", err) diff --git a/cmd/eksterd/incoming.go b/cmd/eksterd/incoming.go index 6e5028e..04c6d8e 100644 --- a/cmd/eksterd/incoming.go +++ b/cmd/eksterd/incoming.go @@ -1,20 +1,3 @@ -/* - Microsub server - Copyright (C) 2018 Peter Stuifzand - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ package main import ( @@ -41,7 +24,12 @@ var ( func (h *incomingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() - r.ParseForm() + err := r.ParseForm() + if err != nil { + http.Error(w, "could not parse form data", http.StatusBadRequest) + return + } + log.Printf("%s %s\n", r.Method, r.URL) log.Println(r.URL.Query()) log.Println(r.PostForm) @@ -73,7 +61,8 @@ func (h *incomingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } verify := values.Get("hub.challenge") - fmt.Fprint(w, verify) + _, err := fmt.Fprint(w, verify) + http.Error(w, fmt.Sprintf("could not write verification challenge: %v", err), 400) return }