Moved Rest controllers to 'api' package. Also removed annotation routes
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/deluan/gosonic/controllers/responses"
|
||||
)
|
||||
|
||||
type GetLicenseController struct{ beego.Controller }
|
||||
|
||||
// @router /rest/getLicense.view [get]
|
||||
func (this *GetLicenseController) Get() {
|
||||
response := responses.NewXML(&responses.License{Valid: true})
|
||||
this.Ctx.Output.Body(response)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/deluan/gosonic/controllers/responses"
|
||||
"github.com/deluan/gosonic/repositories"
|
||||
)
|
||||
|
||||
type GetMusicFoldersController struct{ beego.Controller }
|
||||
|
||||
// @router /rest/getMusicFolders.view [get]
|
||||
func (this *GetMusicFoldersController) Get() {
|
||||
repository := new(repositories.MediaFolderRepository)
|
||||
mediaFolderList, _ := repository.GetAll()
|
||||
folders := make([]responses.MusicFolder, len(mediaFolderList))
|
||||
for i, f := range mediaFolderList {
|
||||
folders[i].Id = f.Id
|
||||
folders[i].Name = f.Name
|
||||
}
|
||||
musicFolders := &responses.MusicFolders{Folders: folders}
|
||||
response := responses.NewXML(musicFolders)
|
||||
this.Ctx.Output.Body(response)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
type MainController struct{ beego.Controller }
|
||||
|
||||
|
||||
// @router / [get]
|
||||
func (this *MainController) Get() {
|
||||
this.Ctx.Redirect(302, "/static/Jamstash/")
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
"encoding/xml"
|
||||
"github.com/deluan/gosonic/controllers/responses"
|
||||
)
|
||||
|
||||
type PingController struct{ beego.Controller }
|
||||
|
||||
// @router /rest/ping.view [get]
|
||||
func (this *PingController) Get() {
|
||||
response := responses.NewEmpty()
|
||||
xmlBody, _ := xml.Marshal(response)
|
||||
this.Ctx.Output.Body([]byte(xml.Header + string(xmlBody)))
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
package responses
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
)
|
||||
|
||||
const (
|
||||
ERROR_GENERIC = iota * 10
|
||||
ERROR_MISSING_PARAMETER
|
||||
ERROR_CLIENT_TOO_OLD
|
||||
ERROR_SERVER_TOO_OLD
|
||||
ERROR_AUTHENTICATION_FAIL
|
||||
ERROR_AUTHORIZATION_FAIL
|
||||
ERROR_TRIAL_EXPIRED
|
||||
ERROR_DATA_NOT_FOUND
|
||||
)
|
||||
|
||||
var (
|
||||
errors map[int]string
|
||||
)
|
||||
|
||||
func init() {
|
||||
errors = make(map[int]string)
|
||||
errors[ERROR_GENERIC] = "A generic error"
|
||||
errors[ERROR_MISSING_PARAMETER] = "Required parameter is missing"
|
||||
errors[ERROR_CLIENT_TOO_OLD] = "Incompatible Subsonic REST protocol version. Client must upgrade"
|
||||
errors[ERROR_SERVER_TOO_OLD] = "Incompatible Subsonic REST protocol version. Server must upgrade"
|
||||
errors[ERROR_AUTHENTICATION_FAIL] = "Wrong username or password"
|
||||
errors[ERROR_AUTHORIZATION_FAIL] = "User is not authorized for the given operation"
|
||||
errors[ERROR_TRIAL_EXPIRED] = "The trial period for the Subsonic server is over. Please upgrade to Subsonic Premium. Visit subsonic.org for details"
|
||||
errors[ERROR_DATA_NOT_FOUND] = "The requested data was not found"
|
||||
}
|
||||
|
||||
type error struct {
|
||||
XMLName xml.Name`xml:"error"`
|
||||
Code int `xml:"code,attr"`
|
||||
Message string `xml:"message,attr"`
|
||||
}
|
||||
|
||||
func NewError(errorCode int) []byte {
|
||||
response := NewEmpty()
|
||||
response.Status = "fail"
|
||||
if errors[errorCode] == "" {
|
||||
errorCode = ERROR_GENERIC
|
||||
}
|
||||
xmlBody, _ := xml.Marshal(&error{Code: errorCode, Message: errors[errorCode]})
|
||||
response.Body = xmlBody
|
||||
xmlResponse, _ := xml.Marshal(response)
|
||||
return []byte(xml.Header + string(xmlResponse))
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package responses
|
||||
|
||||
import "encoding/xml"
|
||||
|
||||
type License struct {
|
||||
XMLName xml.Name `xml:"license"`
|
||||
Valid bool `xml:"valid,attr"`
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package responses
|
||||
|
||||
import "encoding/xml"
|
||||
|
||||
type MusicFolder struct {
|
||||
XMLName xml.Name `xml:"musicFolder"`
|
||||
Id string `xml:"id,attr"`
|
||||
Name string `xml:"name,attr"`
|
||||
}
|
||||
|
||||
type MusicFolders struct {
|
||||
XMLName xml.Name `xml:"musicFolders"`
|
||||
Folders []MusicFolder `xml:"musicFolders"`
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package responses
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
type Subsonic struct {
|
||||
XMLName xml.Name `xml:"http://subsonic.org/restapi subsonic-response"`
|
||||
Status string `xml:"status,attr"`
|
||||
Version string `xml:"version,attr"`
|
||||
Body []byte `xml:",innerxml"`
|
||||
}
|
||||
|
||||
func NewEmpty() Subsonic {
|
||||
return Subsonic{Status: "ok", Version: beego.AppConfig.String("apiVersion")}
|
||||
}
|
||||
|
||||
func NewXML(body interface{}) []byte {
|
||||
response := NewEmpty()
|
||||
xmlBody, _ := xml.Marshal(body)
|
||||
response.Body = xmlBody
|
||||
xmlResponse, _ := xml.Marshal(response)
|
||||
return []byte(xml.Header + string(xmlResponse))
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/deluan/gosonic/controllers/responses"
|
||||
)
|
||||
|
||||
type ControllerInterface interface {
|
||||
GetString(key string, def ...string) string
|
||||
CustomAbort(status int, body string)
|
||||
}
|
||||
|
||||
func Validate(controller ControllerInterface) {
|
||||
if beego.AppConfig.String("disableValidation") != "true" {
|
||||
checkParameters(controller)
|
||||
authenticate(controller)
|
||||
// TODO Validate version
|
||||
}
|
||||
}
|
||||
|
||||
func checkParameters(c ControllerInterface) {
|
||||
requiredParameters := []string {"u", "p", "v", "c",}
|
||||
|
||||
for _,p := range requiredParameters {
|
||||
if c.GetString(p) == "" {
|
||||
cancel(c, responses.ERROR_MISSING_PARAMETER)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func authenticate(c ControllerInterface) {
|
||||
user := c.GetString("u")
|
||||
pass := c.GetString("p") // TODO Handle hex-encoded password
|
||||
if (user != beego.AppConfig.String("user") || pass != beego.AppConfig.String("password")) {
|
||||
cancel(c, responses.ERROR_AUTHENTICATION_FAIL)
|
||||
}
|
||||
}
|
||||
|
||||
func cancel(c ControllerInterface, code int) {
|
||||
c.CustomAbort(200, string(responses.NewError(code)))
|
||||
}
|
||||
Reference in New Issue
Block a user