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

This commit is contained in:
Marvin Steadfast 2021-05-06 10:16:12 +02:00
parent af068f70e8
commit 88d550f947
2 changed files with 18 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

25
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)
@ -74,9 +75,21 @@ func (l Logic) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
jd.LocalAddress = l.ExternalURL
// jd.LocalAddress = l.ExternalURL
var nd PublicInfo
nd.ServerName = jd.ServerName
nd.Version = jd.Version
nd.ProductName = jd.ProductName
nd.OperatingSystem = jd.OperatingSystem
nd.ID = jd.ID
nd.StartupWizardCompleted = jd.StartupWizardCompleted
nd, err := json.Marshal(jd)
// 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 +97,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 +126,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"),