From fad12ea9d7ec2cbc60316fe28932035eaeb5413d Mon Sep 17 00:00:00 2001 From: Ashley Davis Date: Wed, 20 Mar 2024 14:13:39 +0100 Subject: [PATCH] add ntfy support to guestbook Signed-off-by: Ashley Davis --- guestbook/main.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/guestbook/main.go b/guestbook/main.go index a168a24..39e92e4 100644 --- a/guestbook/main.go +++ b/guestbook/main.go @@ -36,6 +36,8 @@ var ( dbPath = flag.String("db-path", "guestbook.sqlite", "Path to sqlite database") initDB = flag.Bool("init-db", false, "If set, initialise a fresh database at db-path") + + ntfyTopic = flag.String("ntfy-topic", "", "If set, a ntfy.sh topic to post to when a new guestbook entry is made") ) func indexPage(db *sql.DB) http.Handler { @@ -123,7 +125,7 @@ func getUserAgent(r *http.Request) string { return agent } -func writePage(db *sql.DB) http.Handler { +func writePage(db *sql.DB, ntfyTopic *string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { logger := LoggerFromContext(r.Context()).With("handler", "write") err := r.ParseForm() @@ -143,6 +145,13 @@ func writePage(db *sql.DB) http.Handler { return } + if ntfyTopic != nil { + err := ntfy(*ntfyTopic, email, userAgent, message) + if err != nil { + logger.Error("failed to send ntfy message", "error", err) + } + } + logger.Info("added message", "email", email, "contents", message, "user-agent", userAgent) w.WriteHeader(http.StatusOK) @@ -150,6 +159,20 @@ func writePage(db *sql.DB) http.Handler { }) } +// This function taken from a MIT-licensed project from github.com/SgtCoDFish +func ntfy(topic string, email string, userAgent string, message string) error { + client := &http.Client{ + Timeout: 5 * time.Second, + } + + ntfyMessage := strings.NewReader( + fmt.Sprintf("%s (%s) posted to the guestbook: %q", email, userAgent, message), + ) + + _, err := client.Post(fmt.Sprintf("https://ntfy.sh/%s", topic), "text/plain", ntfyMessage) + return err +} + func loadCACerts(path string) (*x509.CertPool, error) { certBytes, err := os.ReadFile(path) if err != nil { @@ -271,7 +294,7 @@ func run(ctx context.Context) error { serveMux := http.NewServeMux() serveMux.Handle("GET /", certExtractMiddleware(indexPage(db))) - serveMux.Handle("POST /write", certExtractMiddleware(writePage(db))) + serveMux.Handle("POST /write", certExtractMiddleware(writePage(db, ntfyTopic))) server := &http.Server{ Handler: serveMux, BaseContext: func(l net.Listener) context.Context { return ctx },