wip
This commit is contained in:
parent
5c2842c995
commit
1d6ba99a36
8 changed files with 945 additions and 83 deletions
41
request_response.go
Normal file
41
request_response.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
// External
|
||||
werr "git.gibonuddevalla.se/go/wrappederror"
|
||||
|
||||
// Standard
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
ID string
|
||||
}
|
||||
|
||||
func responseError(w http.ResponseWriter, err error) {
|
||||
res := map[string]interface{}{
|
||||
"OK": false,
|
||||
"Error": err.Error(),
|
||||
}
|
||||
resJSON, _ := json.Marshal(res)
|
||||
|
||||
|
||||
werr.Wrap(err).Log()
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.Write(resJSON)
|
||||
}
|
||||
|
||||
func responseData(w http.ResponseWriter, data interface{}) {
|
||||
resJSON, _ := json.Marshal(data)
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.Write(resJSON)
|
||||
}
|
||||
|
||||
func parseRequest(r *http.Request, data interface{}) (err error) {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
defer r.Body.Close()
|
||||
err = json.Unmarshal(body, data)
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue