mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-03 20:47:35 +03:00
Introduces context.Context in API controllers
This commit is contained in:
parent
dc98b187d9
commit
2861a97a89
5 changed files with 47 additions and 14 deletions
4
Makefile
4
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
|
||||
|
|
12
README.md
12
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 ;) [](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:
|
||||
```
|
||||
|
|
|
@ -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")}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue