From 1cf8d3037bf17f4de33c6e1c9a023cf29cae3b01 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Mon, 29 Mar 2021 20:57:13 +0100 Subject: [PATCH] remove dependency on common from mediabrowser --- common/common.go | 2 +- main.go | 4 ++-- mediabrowser/go.mod | 4 ---- mediabrowser/mediabrowser.go | 27 +++++++++++++++++++++++---- mediabrowser/models.go | 7 ------- setup.go | 3 +-- 6 files changed, 27 insertions(+), 20 deletions(-) diff --git a/common/common.go b/common/common.go index ed94ad1..7355d24 100644 --- a/common/common.go +++ b/common/common.go @@ -5,7 +5,7 @@ import ( "log" ) -// TimeoutHandler recovers from an http timeout. +// TimeoutHandler recovers from an http timeout or panic. type TimeoutHandler func() // NewTimeoutHandler returns a new Timeout handler. diff --git a/main.go b/main.go index 6b628a3..ed6d639 100644 --- a/main.go +++ b/main.go @@ -421,10 +421,10 @@ func start(asDaemon, firstCall bool) { server := app.config.Section("jellyfin").Key("server").String() cacheTimeout := int(app.config.Section("jellyfin").Key("cache_timeout").MustUint(30)) stringServerType := app.config.Section("jellyfin").Key("type").String() - timeoutHandler := common.NewTimeoutHandler("Jellyfin", server, true) + timeoutHandler := mediabrowser.NewNamedTimeoutHandler("Jellyfin", server, true) if stringServerType == "emby" { serverType = mediabrowser.EmbyServer - timeoutHandler = common.NewTimeoutHandler("Emby", server, true) + timeoutHandler = mediabrowser.NewNamedTimeoutHandler("Emby", server, true) app.info.Println("Using Emby server type") fmt.Println(warning("WARNING: Emby compatibility is experimental, and support is limited.\nPassword resets are not available.")) } else { diff --git a/mediabrowser/go.mod b/mediabrowser/go.mod index aa7ff90..5c022cc 100644 --- a/mediabrowser/go.mod +++ b/mediabrowser/go.mod @@ -1,7 +1,3 @@ module github.com/hrfee/jfa-go/mediabrowser go 1.15 - -replace github.com/hrfee/jfa-go/common => ../common - -require github.com/hrfee/jfa-go/common v0.0.0-20210105184019-fdc97b4e86cc diff --git a/mediabrowser/mediabrowser.go b/mediabrowser/mediabrowser.go index 8104fe9..f5c4daf 100644 --- a/mediabrowser/mediabrowser.go +++ b/mediabrowser/mediabrowser.go @@ -10,13 +10,32 @@ import ( "fmt" "io" "io/ioutil" + "log" "net/http" "strings" "time" - - "github.com/hrfee/jfa-go/common" ) +// TimeoutHandler should recover from an http timeout or panic. +type TimeoutHandler func() + +// NewNamedTimeoutHandler returns a new Timeout handler that logs the error. +// name is the name of the server to use in the log (e.g Jellyfin/Emby) +// addr is the address of the server being accessed +// if noFail is false, the program will exit on a timeout. +func NewNamedTimeoutHandler(name, addr string, noFail bool) TimeoutHandler { + return func() { + if r := recover(); r != nil { + out := fmt.Sprintf("Failed to authenticate with %s @ %s: Timed out", name, addr) + if noFail { + log.Print(out) + } else { + log.Fatalf(out) + } + } + } +} + type serverType int const ( @@ -57,11 +76,11 @@ type MediaBrowser struct { noFail bool Hyphens bool serverType serverType - timeoutHandler common.TimeoutHandler + timeoutHandler TimeoutHandler } // NewServer returns a new Mediabrowser object. -func NewServer(st serverType, server, client, version, device, deviceID string, timeoutHandler common.TimeoutHandler, cacheTimeout int) (*MediaBrowser, error) { +func NewServer(st serverType, server, client, version, device, deviceID string, timeoutHandler TimeoutHandler, cacheTimeout int) (*MediaBrowser, error) { mb := &MediaBrowser{} mb.serverType = st mb.Server = server diff --git a/mediabrowser/models.go b/mediabrowser/models.go index 9e56814..2f3078c 100644 --- a/mediabrowser/models.go +++ b/mediabrowser/models.go @@ -14,7 +14,6 @@ type Time struct { } func (t *Time) UnmarshalJSON(b []byte) (err error) { - // str := strings.TrimSuffix(strings.TrimPrefix(string(b), "\""), "\"") // Trim quotes from beginning and end, and any number of Zs (indicates UTC). for b[0] == '"' { b = b[1:] @@ -31,12 +30,6 @@ func (t *Time) UnmarshalJSON(b []byte) (err error) { b = b[:i] } t.Time, err = time.Parse("2006-01-02T15:04:05", string(b)) - // str := string(b) + "Z" - // timeJSON := []byte("{ \"parseme\": \"" + str + "\" }") - // var parsed magicParse - // // Magically turn it into a time.Time - // err = json.Unmarshal(timeJSON, &parsed) - // t.Time = parsed.Parsed return } diff --git a/setup.go b/setup.go index b4e9142..067fb48 100644 --- a/setup.go +++ b/setup.go @@ -7,7 +7,6 @@ import ( "strings" "github.com/gin-gonic/gin" - "github.com/hrfee/jfa-go/common" "github.com/hrfee/jfa-go/mediabrowser" ) @@ -60,7 +59,7 @@ func (app *appContext) TestJF(gc *gin.Context) { if req.ServerType == "emby" { serverType = mediabrowser.EmbyServer } - tempjf, _ := mediabrowser.NewServer(serverType, req.Server, "jfa-go-setup", app.version, "auth", "auth", common.NewTimeoutHandler("authJF", req.Server, true), 30) + tempjf, _ := mediabrowser.NewServer(serverType, req.Server, "jfa-go-setup", app.version, "auth", "auth", mediabrowser.NewNamedTimeoutHandler("authJF", req.Server, true), 30) _, status, err := tempjf.Authenticate(req.Username, req.Password) if !(status == 200 || status == 204) || err != nil { app.info.Printf("Auth failed with code %d (%s)", status, err)