🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Introduces context.Context in API controllers
Deluan2861a97a89ff
diff --git a/Makefile b/Makefileindex 9a4ff8eb7..77a187629 100644--- a/Makefile+++ b/Makefile@@ -1,6 +1,6 @@#@IgnoreInspection BashAddShebang-BINARY=gosonic+BINARY=sonic-serverSOURCES := $(shell find . -name '*.go')@@ -12,7 +12,7 @@ $(BINARY): $(SOURCES).PHONY: cleanclean:rm -f ${BINARY}-+.PHONY: setupsetup:go get -u github.com/beego/bee # bee command line tooldiff --git a/README.md b/README.mdindex 5067f1e1e..467ce5431 100644--- a/README.md+++ b/README.md@@ -6,9 +6,8 @@ CloudSonic Server__This is still a work in progress, and has no releases available__-CloudSonic is an application that implements the [Subsonic API](http://www.subsonic.org/pages/api.jsp), but instead of-having its own music library like the original [Subsonic application](http://www.subsonic.org), it interacts directly-with your iTunes library.+CloudSonic is a music collection server and streamer, optmized to run on cheap VPS servers. It implements the+[Subsonic](http://www.subsonic.org) APIThe project's main goals are:@@ -17,9 +16,8 @@ The project's main goals are:[DSub](http://www.subsonic.org/pages/apps.jsp#dsub),[SubFire](http://www.subsonic.org/pages/apps.jsp#subfire) and[Jamstash](http://www.subsonic.org/pages/apps.jsp#jamstash))-* Use all metadata from iTunes, so that you can keep using iTunes to manage your music-* Keep iTunes stats (play counts, last played dates, ratings, etc..) updated, at least on Mac OS X.- This allows smart playlists to be used in Subsonic Clients+* Import and use all metadata from iTunes, so that you can optionally keep using iTunes to manage your music+* Implement Smart Playlists, as iTunes* Help me learn Go ;) [](https://golang.org)@@ -41,7 +39,7 @@ The server should start listening on port 4533.### Development Environment-You will need to install [Go 1.7](https://golang.org/dl/)+You will need to install [Go 1.8](https://golang.org/dl/)Then install dependencies:```diff --git a/api/base_api_controller.go b/api/base_api_controller.goindex 03768da5f..f1fd6db11 100644--- a/api/base_api_controller.go+++ b/api/base_api_controller.go@@ -1,6 +1,7 @@package apiimport (+ "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")}diff --git a/api/validation.go b/api/validation.goindex e4fcb1831..a66d5b5f0 100644--- a/api/validation.go+++ b/api/validation.go@@ -1,6 +1,7 @@package apiimport (+ "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 == passwordcase token != "":t := fmt.Sprintf("%x", md5.Sum([]byte(password+salt)))- valid = (t == token)+ valid = t == token}if user != conf.Sonic.User || !valid {diff --git a/init/router.go b/init/router.goindex 3fb39bf8e..0cadeb2ca 100644--- a/init/router.go+++ b/init/router.go@@ -6,8 +6,11 @@ import ("github.com/astaxie/beego/plugins/cors""github.com/cloudsonic/sonic-server/api""github.com/cloudsonic/sonic-server/controllers"+ "github.com/twinj/uuid")+const requestidHeader = "X-Request-Id"+func init() {mapEndpoints()mapControllers()@@ -67,12 +70,23 @@ func mapControllers() {}func initFilters() {- var ValidateRequest = func(ctx *context.Context) {+ var requestIdFilter = func(ctx *context.Context) {+ id := ctx.Input.Header(requestidHeader)+ if id == "" {+ id = uuid.NewV4().String()+ }++ ctx.Input.SetData("requestId", id)+ }++ var validateRequest = func(ctx *context.Context) {c := api.BaseAPIController{}+ // TODO Find a way to not depend on a controller being passedc.Ctx = ctxc.Data = make(map[interface{}]interface{})api.Validate(c)}+beego.InsertFilter("/rest/*", beego.BeforeRouter, cors.Allow(&cors.Options{AllowOrigins: []string{"*"},AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},@@ -81,5 +95,6 @@ func initFilters() {AllowCredentials: true,}))- beego.InsertFilter("/rest/*", beego.BeforeRouter, ValidateRequest)+ beego.InsertFilter("/rest/*", beego.BeforeRouter, requestIdFilter)+ beego.InsertFilter("/rest/*", beego.BeforeRouter, validateRequest)}