Compare commits

...

2 Commits

Author SHA1 Message Date
2a708c8d1a cosmetic changes
All checks were successful
continuous-integration/drone/push Build is passing
2021-05-06 10:19:39 +02:00
88d550f947 removes LocalAddress if its not defined from response
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2021-05-06 10:16:12 +02:00
2 changed files with 19 additions and 9 deletions

View File

@ -4,7 +4,7 @@ a little container to fix [this bug](https://github.com/jellyfin/jellyfin/issues
## Usage
it can take a config file as first argument.
it can take a config file as first argument. if `external_url` is not defined, the JSON response wont include it at all!
## config example

26
main.go
View File

@ -24,7 +24,7 @@ type Logic struct {
// PublicInfo Chromecast configuration Payload.
type PublicInfo struct {
LocalAddress string `json:"LocalAddress"`
LocalAddress string `json:"LocalAddress,omitempty"`
ServerName string `json:"ServerName"`
Version string `json:"Version"`
ProductName string `json:"ProductName"`
@ -33,6 +33,7 @@ type PublicInfo struct {
StartupWizardCompleted bool `json:"StartupWizardCompleted"`
}
// nolint:funlen
func (l Logic) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logger := logginghandler.Logger(r)
@ -67,6 +68,7 @@ func (l Logic) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logger.Debug().Str("body", string(body)).Msg("read body")
var jd PublicInfo
if err := json.Unmarshal(body, &jd); err != nil {
logger.Error().Msg(err.Error())
http.Error(w, "could not unmarshal body", http.StatusInternalServerError)
@ -74,9 +76,21 @@ func (l Logic) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
jd.LocalAddress = l.ExternalURL
var nd PublicInfo
nd, err := json.Marshal(jd)
nd.ServerName = jd.ServerName
nd.Version = jd.Version
nd.ProductName = jd.ProductName
nd.OperatingSystem = jd.OperatingSystem
nd.ID = jd.ID
nd.StartupWizardCompleted = jd.StartupWizardCompleted
// When there is no external url defined, it wont include it in the response.
if l.ExternalURL != "" {
nd.LocalAddress = l.ExternalURL
}
bd, err := json.Marshal(nd)
if err != nil {
logger.Error().Msg(err.Error())
http.Error(w, "could not marshal body", http.StatusInternalServerError)
@ -84,7 +98,7 @@ func (l Logic) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
if _, err := w.Write(nd); err != nil {
if _, err := w.Write(bd); err != nil {
logger.Error().Msg(err.Error())
}
}
@ -113,10 +127,6 @@ func main() {
log.Fatal().Msg("needs key 'internal_url'")
}
if viper.GetString("external_url") == "" {
log.Fatal().Msg("needs key 'external_url'")
}
l := Logic{
InternalURL: viper.GetString("internal_url"),
ExternalURL: viper.GetString("external_url"),