More refactoring of user http api, trying to reuse handlers for sign in user and admin operations

This commit is contained in:
Torkel Ödegaard
2015-05-18 19:06:19 +02:00
parent 62e8841e8c
commit fbc6bb2112
6 changed files with 45 additions and 64 deletions

View File

@ -26,12 +26,17 @@ type NormalResponse struct {
header http.Header
}
func wrap(action func(c *middleware.Context) Response) macaron.Handler {
func wrap(action interface{}) macaron.Handler {
return func(c *middleware.Context) {
res := action(c)
if res == nil {
var res Response
val, err := c.Invoke(action)
if err == nil && val != nil && len(val) > 0 {
res = val[0].Interface().(Response)
} else {
res = ServerError
}
res.WriteTo(c.Resp)
}
}
@ -64,6 +69,12 @@ func Json(status int, body interface{}) *NormalResponse {
return Respond(status, body).Header("Content-Type", "application/json")
}
func ApiSuccess(message string) *NormalResponse {
resp := make(map[string]interface{})
resp["message"] = message
return Respond(200, resp)
}
func ApiError(status int, message string, err error) *NormalResponse {
resp := make(map[string]interface{})