forked from navidrome/insights
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
37 lines (31 loc) · 760 Bytes
/
handler.go
File metadata and controls
37 lines (31 loc) · 760 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"database/sql"
"errors"
"log"
"net/http"
"git.happydns.org/happyDomain/model"
)
func handler(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var data happydns.Insights
err := decodeJSONBody(w, r, &data)
if err != nil {
var mr *malformedRequest
if errors.As(err, &mr) {
http.Error(w, mr.msg, mr.status)
} else {
log.Printf("error decoding payload: %s", err.Error())
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
return
}
err = saveToDB(db, data)
if err != nil {
log.Printf("Error handling request: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
}