Skip to content

🎧 Your Personal Streaming Service

Introduces context.Context in API controllers

Deluan2861a97a89ff
diff --git a/Makefile b/Makefile
index 9a4ff8eb7..77a187629 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
#@IgnoreInspection BashAddShebang
-BINARY=gosonic
+BINARY=sonic-server
SOURCES := $(shell find . -name '*.go')
@@ -12,7 +12,7 @@ $(BINARY): $(SOURCES)
.PHONY: clean
clean:
rm -f ${BINARY}
-
+
.PHONY: setup
setup:
go get -u github.com/beego/bee # bee command line tool
diff --git a/README.md b/README.md
index 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) API
The 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 ;) [![Gopher](https://blog.golang.org/favicon.ico)](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.go
index 03768da5f..f1fd6db11 100644
--- a/api/base_api_controller.go
+++ b/api/base_api_controller.go
@@ -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")}
diff --git a/api/validation.go b/api/validation.go
index e4fcb1831..a66d5b5f0 100644
--- a/api/validation.go
+++ b/api/validation.go
@@ -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 {
diff --git a/init/router.go b/init/router.go
index 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 passed
c.Ctx = ctx
c.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)
}