Better log and less noise in prod mode

This commit is contained in:
Deluan
2016-03-15 17:06:23 -04:00
parent 714b100d03
commit cdefabf760
5 changed files with 46 additions and 20 deletions
+11 -2
View File
@@ -1,10 +1,12 @@
package api
import (
"fmt"
"time"
"github.com/astaxie/beego"
"github.com/deluan/gosonic/api/responses"
"github.com/deluan/gosonic/domain"
"github.com/deluan/gosonic/itunesbridge"
"github.com/deluan/gosonic/utils"
)
@@ -12,10 +14,11 @@ import (
type MediaAnnotationController struct {
BaseAPIController
itunes itunesbridge.ItunesControl
mfRepo domain.MediaFileRepository
}
func (c *MediaAnnotationController) Prepare() {
utils.ResolveDependencies(&c.itunes)
utils.ResolveDependencies(&c.itunes, &c.mfRepo)
}
func (c *MediaAnnotationController) Scrobble() {
@@ -24,7 +27,13 @@ func (c *MediaAnnotationController) Scrobble() {
submission := c.ParamBool("submission", true)
if submission {
beego.Debug("Scrobbling", id, "at", time)
mf, err := c.mfRepo.Get(id)
if err != nil || mf == nil {
beego.Error("Id", id, "not found!")
c.SendError(responses.ERROR_DATA_NOT_FOUND, "Id not found")
}
beego.Info(fmt.Sprintf(`Scrobbling (%s) "%s" at %v`, id, mf.Title, time))
if err := c.itunes.Scrobble(id, time); err != nil {
beego.Error("Error scrobbling:", err)
c.SendError(responses.ERROR_GENERIC, "Internal error")
+12 -6
View File
@@ -4,6 +4,8 @@ import (
"encoding/hex"
"strings"
"fmt"
"github.com/astaxie/beego"
"github.com/deluan/gosonic/api/responses"
)
@@ -14,7 +16,7 @@ type ControllerInterface interface {
SendError(errorCode int, message ...interface{})
}
func Validate(controller ControllerInterface) {
func Validate(controller BaseAPIController) {
if beego.AppConfig.String("disableValidation") != "true" {
checkParameters(controller)
authenticate(controller)
@@ -22,18 +24,18 @@ func Validate(controller ControllerInterface) {
}
}
func checkParameters(c ControllerInterface) {
func checkParameters(c BaseAPIController) {
requiredParameters := []string{"u", "p", "v", "c"}
for _, p := range requiredParameters {
if c.GetString(p) == "" {
beego.Warn("Missing required parameter:", p)
logWarn(c, fmt.Sprintf(`Missing required parameter "%s"`, p))
abortRequest(c, responses.ERROR_MISSING_PARAMETER)
}
}
}
func authenticate(c ControllerInterface) {
func authenticate(c BaseAPIController) {
user := c.GetString("u")
pass := c.GetString("p")
if strings.HasPrefix(pass, "enc:") {
@@ -43,11 +45,15 @@ func authenticate(c ControllerInterface) {
}
}
if user != beego.AppConfig.String("user") || pass != beego.AppConfig.String("password") {
beego.Warn("Invalid login:", user)
logWarn(c, fmt.Sprintf(`Invalid login for user "%s"`, user))
abortRequest(c, responses.ERROR_AUTHENTICATION_FAIL)
}
}
func abortRequest(c ControllerInterface, code int) {
func abortRequest(c BaseAPIController, code int) {
c.SendError(code)
}
func logWarn(c BaseAPIController, msg string) {
beego.Warn(fmt.Sprintf("%s?%s: %s", c.Ctx.Request.URL.Path, c.Ctx.Request.URL.RawQuery, msg))
}