🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Initial project skeleton
Deluan5d6fd4ee6b0e
diff --git a/.gitignore b/.gitignorenew file mode 100644index 000000000..7133ddc77--- /dev/null+++ b/.gitignore@@ -0,0 +1,3 @@+commentsRouter_*.go+lastupdate.tmp+gosonic\ No newline at end of filediff --git a/.gopmfile b/.gopmfilenew file mode 100644index 000000000..7660221fa--- /dev/null+++ b/.gopmfile@@ -0,0 +1,8 @@+[target]+path = github.com/deluan/gosonic++[deps]+github.com/astaxie/beego = tag:v1.6.0++[res]+include = confdiff --git a/README.md b/README.mdnew file mode 100644index 000000000..040e24376--- /dev/null+++ b/README.md@@ -0,0 +1,11 @@+FRAMEWORKS/PROJECTS+https://github.com/golang/go/wiki/Projects++REST/WEB+http://beego.me/++DB+https://github.com/HouzuoGuo/tiedot++SEARCH+https://github.com/blevesearch/blevediff --git a/conf/app.conf b/conf/app.confnew file mode 100644index 000000000..d3cbf4683--- /dev/null+++ b/conf/app.conf@@ -0,0 +1,6 @@+appname = github.com/deluan/gosonic+httpport = 8080+runmode = dev+autorender = false+copyrequestbody = true+EnableDocs = falsediff --git a/controllers/object.go b/controllers/object.gonew file mode 100644index 000000000..7c1ca5fd7--- /dev/null+++ b/controllers/object.go@@ -0,0 +1,92 @@+package controllers++import (+ "github.com/deluan/gosonic/models"+ "encoding/json"++ "github.com/astaxie/beego"+)++// Operations about object+type ObjectController struct {+ beego.Controller+}++// @Title create+// @Description create object+// @Param body body models.Object true "The object content"+// @Success 200 {string} models.Object.Id+// @Failure 403 body is empty+// @router / [post]+func (o *ObjectController) Post() {+ var ob models.Object+ json.Unmarshal(o.Ctx.Input.RequestBody, &ob)+ objectid := models.AddOne(ob)+ o.Data["json"] = map[string]string{"ObjectId": objectid}+ o.ServeJSON()+}++// @Title Get+// @Description find object by objectid+// @Param objectId path string true "the objectid you want to get"+// @Success 200 {object} models.Object+// @Failure 403 :objectId is empty+// @router /:objectId [get]+func (o *ObjectController) Get() {+ objectId := o.Ctx.Input.Param(":objectId")+ if objectId != "" {+ ob, err := models.GetOne(objectId)+ if err != nil {+ o.Data["json"] = err.Error()+ } else {+ o.Data["json"] = ob+ }+ }+ o.ServeJSON()+}++// @Title GetAll+// @Description get all objects+// @Success 200 {object} models.Object+// @Failure 403 :objectId is empty+// @router / [get]+func (o *ObjectController) GetAll() {+ obs := models.GetAll()+ o.Data["json"] = obs+ o.ServeJSON()+}++// @Title update+// @Description update the object+// @Param objectId path string true "The objectid you want to update"+// @Param body body models.Object true "The body"+// @Success 200 {object} models.Object+// @Failure 403 :objectId is empty+// @router /:objectId [put]+func (o *ObjectController) Put() {+ objectId := o.Ctx.Input.Param(":objectId")+ var ob models.Object+ json.Unmarshal(o.Ctx.Input.RequestBody, &ob)++ err := models.Update(objectId, ob.Score)+ if err != nil {+ o.Data["json"] = err.Error()+ } else {+ o.Data["json"] = "update success!"+ }+ o.ServeJSON()+}++// @Title delete+// @Description delete the object+// @Param objectId path string true "The objectId you want to delete"+// @Success 200 {string} delete success!+// @Failure 403 objectId is empty+// @router /:objectId [delete]+func (o *ObjectController) Delete() {+ objectId := o.Ctx.Input.Param(":objectId")+ models.Delete(objectId)+ o.Data["json"] = "delete success!"+ o.ServeJSON()+}+diff --git a/controllers/user.go b/controllers/user.gonew file mode 100644index 000000000..89b833d3b--- /dev/null+++ b/controllers/user.go@@ -0,0 +1,119 @@+package controllers++import (+ "github.com/deluan/gosonic/models"+ "encoding/json"++ "github.com/astaxie/beego"+)++// Operations about Users+type UserController struct {+ beego.Controller+}++// @Title createUser+// @Description create users+// @Param body body models.User true "body for user content"+// @Success 200 {int} models.User.Id+// @Failure 403 body is empty+// @router / [post]+func (u *UserController) Post() {+ var user models.User+ json.Unmarshal(u.Ctx.Input.RequestBody, &user)+ uid := models.AddUser(user)+ u.Data["json"] = map[string]string{"uid": uid}+ u.ServeJSON()+}++// @Title Get+// @Description get all Users+// @Success 200 {object} models.User+// @router / [get]+func (u *UserController) GetAll() {+ users := models.GetAllUsers()+ u.Data["json"] = users+ u.ServeJSON()+}++// @Title Get+// @Description get user by uid+// @Param uid path string true "The key for staticblock"+// @Success 200 {object} models.User+// @Failure 403 :uid is empty+// @router /:uid [get]+func (u *UserController) Get() {+ uid := u.GetString(":uid")+ if uid != "" {+ user, err := models.GetUser(uid)+ if err != nil {+ u.Data["json"] = err.Error()+ } else {+ u.Data["json"] = user+ }+ }+ u.ServeJSON()+}++// @Title update+// @Description update the user+// @Param uid path string true "The uid you want to update"+// @Param body body models.User true "body for user content"+// @Success 200 {object} models.User+// @Failure 403 :uid is not int+// @router /:uid [put]+func (u *UserController) Put() {+ uid := u.GetString(":uid")+ if uid != "" {+ var user models.User+ json.Unmarshal(u.Ctx.Input.RequestBody, &user)+ uu, err := models.UpdateUser(uid, &user)+ if err != nil {+ u.Data["json"] = err.Error()+ } else {+ u.Data["json"] = uu+ }+ }+ u.ServeJSON()+}++// @Title delete+// @Description delete the user+// @Param uid path string true "The uid you want to delete"+// @Success 200 {string} delete success!+// @Failure 403 uid is empty+// @router /:uid [delete]+func (u *UserController) Delete() {+ uid := u.GetString(":uid")+ models.DeleteUser(uid)+ u.Data["json"] = "delete success!"+ u.ServeJSON()+}++// @Title login+// @Description Logs user into the system+// @Param username query string true "The username for login"+// @Param password query string true "The password for login"+// @Success 200 {string} login success+// @Failure 403 user not exist+// @router /login [get]+func (u *UserController) Login() {+ username := u.GetString("username")+ password := u.GetString("password")+ if models.Login(username, password) {+ u.Data["json"] = "login success"+ } else {+ u.Data["json"] = "user not exist"+ }+ u.ServeJSON()+}++// @Title logout+// @Description Logs out current logged in user session+// @Success 200 {string} logout success+// @router /logout [get]+func (u *UserController) Logout() {+ u.Data["json"] = "logout success"+ u.ServeJSON()+}+diff --git a/docs/doc.go b/docs/doc.gonew file mode 100644index 000000000..06fb4ccde--- /dev/null+++ b/docs/doc.go@@ -0,0 +1 @@+package docs\ No newline at end of filediff --git a/main.go b/main.gonew file mode 100644index 000000000..7e96629c7--- /dev/null+++ b/main.go@@ -0,0 +1,16 @@+package main++import (+ _ "github.com/deluan/gosonic/docs"+ _ "github.com/deluan/gosonic/routers"++ "github.com/astaxie/beego"+)++func main() {+ if beego.BConfig.RunMode == "dev" {+ beego.BConfig.WebConfig.DirectoryIndex = true+ beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"+ }+ beego.Run()+}diff --git a/models/object.go b/models/object.gonew file mode 100644index 000000000..3ffb60850--- /dev/null+++ b/models/object.go@@ -0,0 +1,53 @@+package models++import (+ "errors"+ "strconv"+ "time"+)++var (+ Objects map[string]*Object+)++type Object struct {+ ObjectId string+ Score int64+ PlayerName string+}++func init() {+ Objects = make(map[string]*Object)+ Objects["hjkhsbnmn123"] = &Object{"hjkhsbnmn123", 100, "astaxie"}+ Objects["mjjkxsxsaa23"] = &Object{"mjjkxsxsaa23", 101, "someone"}+}++func AddOne(object Object) (ObjectId string) {+ object.ObjectId = "astaxie" + strconv.FormatInt(time.Now().UnixNano(), 10)+ Objects[object.ObjectId] = &object+ return object.ObjectId+}++func GetOne(ObjectId string) (object *Object, err error) {+ if v, ok := Objects[ObjectId]; ok {+ return v, nil+ }+ return nil, errors.New("ObjectId Not Exist")+}++func GetAll() map[string]*Object {+ return Objects+}++func Update(ObjectId string, Score int64) (err error) {+ if v, ok := Objects[ObjectId]; ok {+ v.Score = Score+ return nil+ }+ return errors.New("ObjectId Not Exist")+}++func Delete(ObjectId string) {+ delete(Objects, ObjectId)+}+diff --git a/models/user.go b/models/user.gonew file mode 100644index 000000000..d4bebb207--- /dev/null+++ b/models/user.go@@ -0,0 +1,86 @@+package models++import (+ "errors"+ "strconv"+ "time"+)++var (+ UserList map[string]*User+)++func init() {+ UserList = make(map[string]*User)+ u := User{"user_11111", "astaxie", "11111", Profile{"male", 20, "Singapore", "[email protected]"}}+ UserList["user_11111"] = &u+}++type User struct {+ Id string+ Username string+ Password string+ Profile Profile+}++type Profile struct {+ Gender string+ Age int+ Address string+ Email string+}++func AddUser(u User) string {+ u.Id = "user_" + strconv.FormatInt(time.Now().UnixNano(), 10)+ UserList[u.Id] = &u+ return u.Id+}++func GetUser(uid string) (u *User, err error) {+ if u, ok := UserList[uid]; ok {+ return u, nil+ }+ return nil, errors.New("User not exists")+}++func GetAllUsers() map[string]*User {+ return UserList+}++func UpdateUser(uid string, uu *User) (a *User, err error) {+ if u, ok := UserList[uid]; ok {+ if uu.Username != "" {+ u.Username = uu.Username+ }+ if uu.Password != "" {+ u.Password = uu.Password+ }+ if uu.Profile.Age != 0 {+ u.Profile.Age = uu.Profile.Age+ }+ if uu.Profile.Address != "" {+ u.Profile.Address = uu.Profile.Address+ }+ if uu.Profile.Gender != "" {+ u.Profile.Gender = uu.Profile.Gender+ }+ if uu.Profile.Email != "" {+ u.Profile.Email = uu.Profile.Email+ }+ return u, nil+ }+ return nil, errors.New("User Not Exist")+}++func Login(username, password string) bool {+ for _, u := range UserList {+ if u.Username == username && u.Password == password {+ return true+ }+ }+ return false+}++func DeleteUser(uid string) {+ delete(UserList, uid)+}diff --git a/routers/router.go b/routers/router.gonew file mode 100644index 000000000..53b07f952--- /dev/null+++ b/routers/router.go@@ -0,0 +1,30 @@+// @APIVersion 1.0.0+// @Title beego Test API+// @Description beego has a very cool tools to autogenerate documents for your API+// @Contact [email protected]+// @TermsOfServiceUrl http://beego.me/+// @License Apache 2.0+// @LicenseUrl http://www.apache.org/licenses/LICENSE-2.0.html+package routers++import (+ "github.com/deluan/gosonic/controllers"++ "github.com/astaxie/beego"+)++func init() {+ ns := beego.NewNamespace("/v1",+ beego.NSNamespace("/object",+ beego.NSInclude(+ &controllers.ObjectController{},+ ),+ ),+ beego.NSNamespace("/user",+ beego.NSInclude(+ &controllers.UserController{},+ ),+ ),+ )+ beego.AddNamespace(ns)+}diff --git a/tests/default_test.go b/tests/default_test.gonew file mode 100644index 000000000..8362ad795--- /dev/null+++ b/tests/default_test.go@@ -0,0 +1,38 @@+package test++import (+ "net/http"+ "net/http/httptest"+ "testing"+ "runtime"+ "path/filepath"+ _ "github.com/deluan/gosonic/routers"++ "github.com/astaxie/beego"+ . "github.com/smartystreets/goconvey/convey"+)++func init() {+ _, file, _, _ := runtime.Caller(1)+ apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))+ beego.TestBeegoInit(apppath)+}++// TestGet is a sample to run an endpoint test+func TestGet(t *testing.T) {+ r, _ := http.NewRequest("GET", "/v1/object", nil)+ w := httptest.NewRecorder()+ beego.BeeApp.Handlers.ServeHTTP(w, r)++ beego.Trace("testing", "TestGet", "Code[%d]\n%s", w.Code, w.Body.String())++ Convey("Subject: Test Station Endpoint\n", t, func() {+ Convey("Status Code Should Be 200", func() {+ So(w.Code, ShouldEqual, 200)+ })+ Convey("The Result Should Not Be Empty", func() {+ So(w.Body.Len(), ShouldBeGreaterThan, 0)+ })+ })+}+