changeset 26:d584798666b9

More context propagation
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 10 Dec 2016 12:07:11 +0100
parents 74f0036928e2
children 4a84e6d53800
files handlers.go status.go webhook.go
diffstat 3 files changed, 19 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/handlers.go	Sat Dec 10 12:06:07 2016 +0100
+++ b/handlers.go	Sat Dec 10 12:07:11 2016 +0100
@@ -37,7 +37,7 @@
 		quoteCmd:    {missingHandler, "Zitat speichern/abfragen"},
 		remindCmd:   {missingHandler, "Wecker"},
 		statusCmd:   {statusHandler, "Status anfragen"},
-		todoCmd:     {missingHandler, "Aufgabenliste"},
+		todoCmd:     {todoHandler, "Aufgabenliste"},
 		todoTestCmd: {todoButtonTest, "TODO test (internal)"},
 	}
 
@@ -78,7 +78,7 @@
 }
 
 func statusHandler(ctx context.Context, msg message) (replyContent, error) {
-	return replyContent{text: srvStatus.String()}, nil
+	return replyContent{text: srvStatus.Format(ctx)}, nil
 }
 
 // Dispatch an incoming update to the right handler. dispatch() only selects between messages and callbacks; dispatchMessage
--- a/status.go	Sat Dec 10 12:06:07 2016 +0100
+++ b/status.go	Sat Dec 10 12:07:11 2016 +0100
@@ -1,6 +1,9 @@
 package main
 
-import "fmt"
+import (
+	"context"
+	"fmt"
+)
 
 var srvStatus = serverStatus{ok: true}
 
@@ -16,8 +19,8 @@
 	apiCalls    uint
 }
 
-func (ss serverStatus) String() string {
-	webhookInfo, _ := getWebhookInfo()
+func (ss serverStatus) Format(ctx context.Context) string {
+	webhookInfo, _ := getWebhookInfo(ctx)
 	return fmt.Sprintf("ok=%t db=%t dbname=%s cmds=%d callbacks=%d errs=%d api-errs=%d api-calls=%d %s",
 		ss.ok, ss.dbConnected, ss.database, ss.commands, ss.callbacks, ss.errors, ss.apiErrors, ss.apiCalls, webhookInfo)
 }
--- a/webhook.go	Sat Dec 10 12:06:07 2016 +0100
+++ b/webhook.go	Sat Dec 10 12:07:11 2016 +0100
@@ -53,10 +53,19 @@
 	}
 }
 
-func getWebhookInfo() (string, error) {
+func getWebhookInfo(ctx context.Context) (string, error) {
 	srvStatus.apiCalls++
 
-	rp, err := defaultClient.Get(buildURL(getWebhookInfoMethod))
+	rq, err := http.NewRequest(http.MethodGet, buildURL(getWebhookInfoMethod), bytes.NewBuffer(nil))
+
+	if err != nil {
+		log.Println("Couldn't create request for getWebhookInfo:", err)
+		return "", err
+	}
+
+	rq = rq.WithContext(ctx)
+
+	rp, err := defaultClient.Do(rq)
 
 	if err != nil {
 		log.Println("Couldn't getWebhookInfo:", err)