Introduces context.Context in API controllers
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"strconv"
|
||||
@@ -12,7 +13,10 @@ import (
|
||||
"github.com/cloudsonic/sonic-server/utils"
|
||||
)
|
||||
|
||||
type BaseAPIController struct{ beego.Controller }
|
||||
type BaseAPIController struct {
|
||||
beego.Controller
|
||||
context context.Context
|
||||
}
|
||||
|
||||
func (c *BaseAPIController) NewEmpty() responses.Subsonic {
|
||||
return responses.Subsonic{Status: "ok", Version: beego.AppConfig.String("apiVersion")}
|
||||
|
||||
+18
-2
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
@@ -18,6 +19,7 @@ type ControllerInterface interface {
|
||||
}
|
||||
|
||||
func Validate(controller BaseAPIController) {
|
||||
addNewContext(controller)
|
||||
if !conf.Sonic.DisableValidation {
|
||||
checkParameters(controller)
|
||||
authenticate(controller)
|
||||
@@ -25,6 +27,20 @@ func Validate(controller BaseAPIController) {
|
||||
}
|
||||
}
|
||||
|
||||
func getData(c BaseAPIController, name string) string {
|
||||
if v, ok := c.Ctx.Input.GetData(name).(string); ok {
|
||||
return v
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func addNewContext(c BaseAPIController) {
|
||||
ctx := context.Background()
|
||||
|
||||
id := getData(c, "requestId")
|
||||
c.context = context.WithValue(ctx, "requestId", id)
|
||||
}
|
||||
|
||||
func checkParameters(c BaseAPIController) {
|
||||
requiredParameters := []string{"u", "v", "c"}
|
||||
|
||||
@@ -56,10 +72,10 @@ func authenticate(c BaseAPIController) {
|
||||
pass = string(dec)
|
||||
}
|
||||
}
|
||||
valid = (pass == password)
|
||||
valid = pass == password
|
||||
case token != "":
|
||||
t := fmt.Sprintf("%x", md5.Sum([]byte(password+salt)))
|
||||
valid = (t == token)
|
||||
valid = t == token
|
||||
}
|
||||
|
||||
if user != conf.Sonic.User || !valid {
|
||||
|
||||
Reference in New Issue
Block a user