perhaps a memory/performance improvement? by not recreating the same function over and over? maybe.

This commit is contained in:
Howl 2016-04-08 17:27:55 +02:00
parent ed2778e2cc
commit e3cb4aed09

View File

@ -11,56 +11,60 @@ import (
// Method wraps an API method to a HandlerFunc. // Method wraps an API method to a HandlerFunc.
func Method(f func(md common.MethodData) common.Response, db *sql.DB, privilegesNeeded ...int) gin.HandlerFunc { func Method(f func(md common.MethodData) common.Response, db *sql.DB, privilegesNeeded ...int) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
data, err := ioutil.ReadAll(c.Request.Body) initialCaretaker(c, f, db, privilegesNeeded...)
if err != nil { }
c.Error(err) }
}
c.Request.Body.Close() func initialCaretaker(c *gin.Context, f func(md common.MethodData) common.Response, db *sql.DB, privilegesNeeded ...int) {
data, err := ioutil.ReadAll(c.Request.Body)
token := "" if err != nil {
switch { c.Error(err)
case c.Request.Header.Get("X-Ripple-Token") != "": }
token = c.Request.Header.Get("X-Ripple-Token") c.Request.Body.Close()
case c.Query("token") != "":
token = c.Query("token") token := ""
case c.Query("k") != "": switch {
token = c.Query("k") case c.Request.Header.Get("X-Ripple-Token") != "":
} token = c.Request.Header.Get("X-Ripple-Token")
case c.Query("token") != "":
md := common.MethodData{ token = c.Query("token")
DB: db, case c.Query("k") != "":
RequestData: data, token = c.Query("k")
C: c, }
}
if token != "" { md := common.MethodData{
tokenReal, exists := GetTokenFull(token, db) DB: db,
if exists { RequestData: data,
md.User = tokenReal C: c,
} }
} if token != "" {
tokenReal, exists := GetTokenFull(token, db)
missingPrivileges := 0 if exists {
for _, privilege := range privilegesNeeded { md.User = tokenReal
if int(md.User.Privileges)&privilege == 0 { }
missingPrivileges |= privilege }
}
} missingPrivileges := 0
if missingPrivileges != 0 { for _, privilege := range privilegesNeeded {
c.IndentedJSON(401, common.Response{ if int(md.User.Privileges)&privilege == 0 {
Code: 401, missingPrivileges |= privilege
Message: "You don't have the privilege(s): " + common.Privileges(missingPrivileges).String() + ".", }
}) }
return if missingPrivileges != 0 {
} c.IndentedJSON(401, common.Response{
Code: 401,
resp := f(md) Message: "You don't have the privilege(s): " + common.Privileges(missingPrivileges).String() + ".",
if resp.Code == 0 { })
resp.Code = 500 return
} }
if _, exists := c.GetQuery("pls200"); exists {
c.IndentedJSON(200, resp) resp := f(md)
} else { if resp.Code == 0 {
c.IndentedJSON(resp.Code, resp) resp.Code = 500
} }
if _, exists := c.GetQuery("pls200"); exists {
c.IndentedJSON(200, resp)
} else {
c.IndentedJSON(resp.Code, resp)
} }
} }