mirror of
https://gitlab.com/commento/commento.git
synced 2025-06-29 22:56:37 -04:00
api: Add go files
I know this is a huge commit, but I can't be bothered to check this in part by part.
This commit is contained in:
45
api/utils_http.go
Normal file
45
api/utils_http.go
Normal file
@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type response map[string]interface{}
|
||||
|
||||
// TODO: Add tests in utils_http_test.go
|
||||
|
||||
func unmarshalBody(r *http.Request, x interface{}) error {
|
||||
b, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
logger.Errorf("cannot read POST body: %v\n", err)
|
||||
return errorInternal
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(b, x); err != nil {
|
||||
return errorInvalidJSONBody
|
||||
}
|
||||
|
||||
xv := reflect.Indirect(reflect.ValueOf(x))
|
||||
for i := 0; i < xv.NumField(); i++ {
|
||||
if xv.Field(i).IsNil() {
|
||||
return errorMissingField
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeBody(w http.ResponseWriter, x map[string]interface{}) error {
|
||||
resp, err := json.Marshal(x)
|
||||
if err != nil {
|
||||
w.Write([]byte(`{"success":false,"message":"Some internal error occurred"}`))
|
||||
logger.Errorf("cannot marshal response: %v\n")
|
||||
return errorInternal
|
||||
}
|
||||
|
||||
w.Write(resp)
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user