Removed Beego routing/controllers, converted to Chi.

Also introduced Wire for dependency injection
This commit is contained in:
Deluan
2020-01-07 14:56:26 -05:00
committed by Deluan Quintão
parent 1f4dfcb853
commit 79701caca3
31 changed files with 1603 additions and 1188 deletions
+41 -21
View File
@@ -1,6 +1,8 @@
package api
import (
"net/http"
"github.com/astaxie/beego"
"github.com/cloudsonic/sonic-server/api/responses"
"github.com/cloudsonic/sonic-server/domain"
@@ -9,34 +11,41 @@ import (
)
type StreamController struct {
BaseAPIController
repo domain.MediaFileRepository
id string
mf *domain.MediaFile
}
func (c *StreamController) Prepare() {
utils.ResolveDependencies(&c.repo)
func NewStreamController(repo domain.MediaFileRepository) *StreamController {
return &StreamController{repo: repo}
}
c.id = c.RequiredParamString("id", "id parameter required")
func (c *StreamController) Prepare(r *http.Request) (err error) {
c.id, err = RequiredParamString(r, "id", "id parameter required")
if err != nil {
return err
}
mf, err := c.repo.Get(c.id)
c.mf, err = c.repo.Get(c.id)
switch {
case err == domain.ErrNotFound:
beego.Error("MediaFile", c.id, "not found!")
c.SendError(responses.ErrorDataNotFound)
return NewError(responses.ErrorDataNotFound)
case err != nil:
beego.Error("Error reading mediafile", c.id, "from the database", ":", err)
c.SendError(responses.ErrorGeneric, "Internal error")
return NewError(responses.ErrorGeneric, "Internal error")
}
c.mf = mf
return nil
}
// TODO Still getting the "Conn.Write wrote more than the declared Content-Length" error.
// Don't know if this causes any issues
func (c *StreamController) Stream() {
maxBitRate := c.ParamInt("maxBitRate", 0)
func (c *StreamController) Stream(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
err := c.Prepare(r)
if err != nil {
return nil, err
}
maxBitRate := ParamInt(r, "maxBitRate", 0)
maxBitRate = utils.MinInt(c.mf.BitRate, maxBitRate)
beego.Debug("Streaming file", c.id, ":", c.mf.Path)
@@ -47,29 +56,40 @@ func (c *StreamController) Stream() {
//if maxBitRate > 0 {
// contentLength = strconv.Itoa((c.mf.Duration + 1) * maxBitRate * 1000 / 8)
//}
c.Ctx.Output.Header("Content-Length", c.mf.Size)
c.Ctx.Output.Header("Content-Type", "audio/mpeg")
c.Ctx.Output.Header("Expires", "0")
c.Ctx.Output.Header("Cache-Control", "must-revalidate")
c.Ctx.Output.Header("Pragma", "public")
h := w.Header()
h.Set("Content-Length", c.mf.Size)
h.Set("Content-Type", "audio/mpeg")
h.Set("Expires", "0")
h.Set("Cache-Control", "must-revalidate")
h.Set("Pragma", "public")
if c.Ctx.Request.Method == "HEAD" {
if r.Method == "HEAD" {
beego.Debug("Just a HEAD. Not streaming", c.mf.Path)
return
return nil, nil
}
err := engine.Stream(c.mf.Path, c.mf.BitRate, maxBitRate, c.Ctx.ResponseWriter)
err = engine.Stream(c.mf.Path, c.mf.BitRate, maxBitRate, w)
if err != nil {
beego.Error("Error streaming file", c.id, ":", err)
}
beego.Debug("Finished streaming of", c.mf.Path)
return nil, nil
}
func (c *StreamController) Download() {
func (c *StreamController) Download(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
err := c.Prepare(r)
if err != nil {
return nil, err
}
beego.Debug("Sending file", c.mf.Path)
engine.Stream(c.mf.Path, 0, 0, c.Ctx.ResponseWriter)
err = engine.Stream(c.mf.Path, 0, 0, w)
if err != nil {
beego.Error("Error downloading file", c.mf.Path, ":", err.Error())
}
beego.Debug("Finished sending", c.mf.Path)
return nil, nil
}