Improve logging

This commit is contained in:
Thorben Günther 2022-10-10 14:55:22 +02:00
parent 5cf4add40b
commit 61ed59cefa
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED

12
main.go
View file

@ -37,12 +37,12 @@ func (rcv *receiver) handleWebhooks(w http.ResponseWriter, r *http.Request) {
if rcv.cfg.User != "" && rcv.cfg.Password != "" {
user, pass, ok := r.BasicAuth()
if !ok {
rcv.logger.Error("basic auth failure")
rcv.logger.Debug("basic auth failure")
return
}
if user != rcv.cfg.User || pass != rcv.cfg.Password {
rcv.logger.Info("basic auth: wrong user or password")
rcv.logger.Debug("basic auth: wrong user or password")
return
}
}
@ -50,19 +50,19 @@ func (rcv *receiver) handleWebhooks(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
if r.Method != http.MethodPost {
rcv.logger.Errorf("illegal HTTP method: expected %q, got %q", "POST", r.Method)
rcv.logger.Debugf("illegal HTTP method: expected %q, got %q", "POST", r.Method)
return
}
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
rcv.logger.Errorf("illegal content type: %s", contentType)
rcv.logger.Debugf("illegal content type: %s", contentType)
return
}
var event payload
if err := json.NewDecoder(r.Body).Decode(&event); err != nil {
rcv.logger.Error(err)
rcv.logger.Debug(err)
return
}
@ -117,6 +117,7 @@ func (rcv *receiver) handleWebhooks(w http.ResponseWriter, r *http.Request) {
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
rcv.logger.Errorf("ntfy: received status code %d", resp.StatusCode)
return
}
}
@ -139,5 +140,6 @@ func main() {
receiver := &receiver{cfg: cfg, logger: logger}
http.HandleFunc("/", receiver.handleWebhooks)
logger.Infof("Listening on %s", cfg.HTTPAddress)
logger.Fatal(http.ListenAndServe(cfg.HTTPAddress, nil))
}