diff --git a/.gitignore b/.gitignore index b6f8f39..eb418d1 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,4 @@ dist/ venv/ .DS_Store -podsync +/podsync diff --git a/README.md b/README.md index 102bf69..fea3e76 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,6 @@ In order to query YouTube or Vimeo API you have to obtain an API token first. [server] port = 8080 data_dir = "/path/to/data/directory" -hostname = "hostname" [tokens] youtube = "{YOUTUBE_API_TOKEN}" @@ -54,9 +53,21 @@ vimeo = "{VIMEO_API_TOKEN}" format = "video" # or "audio" ``` -Episodes files will be kept at: `/path/to/data/directory/ID1` +Episodes files will be kept at: `/path/to/data/directory/ID1`, feed will be accessible from: `http://localhost/ID1.xml` -Feed will be accessible from: `http://hostname:8080/ID1.xml` +If you want to hide Podsync behind reverse proxy like nginx, you can use `hostname` field: + +```toml +[server] +port = 8080 +hostname = "https://my.test.host:4443" + +[feeds] + [feeds.ID1] + ... +``` + +Server will be accessible from `http://localhost:8080`, but episode links will point to `https://my.test.host:4443/ID1/...` ## How to run diff --git a/cmd/podsync/updater.go b/cmd/podsync/updater.go index 895aa10..94a6fa8 100644 --- a/cmd/podsync/updater.go +++ b/cmd/podsync/updater.go @@ -219,13 +219,9 @@ func (u *Updater) makeEnclosure(feed *model.Feed, episode *model.Episode, cfg *c contentType = itunes.MP3 } - // Make sure there is no http:// prefix - hostname := strings.TrimPrefix(u.config.Server.Hostname, "http://") - url := fmt.Sprintf( - "http://%s:%d/%s/%s.%s", - hostname, - u.config.Server.Port, + "%s/%s/%s.%s", + u.hostname(), cfg.ID, episode.ID, ext, @@ -234,6 +230,15 @@ func (u *Updater) makeEnclosure(feed *model.Feed, episode *model.Episode, cfg *c return url, contentType, episode.Size } +func (u *Updater) hostname() string { + hostname := strings.TrimSuffix(u.config.Server.Hostname, "/") + if !strings.HasPrefix(hostname, "http") { + hostname = fmt.Sprintf("http://%s", hostname) + } + + return hostname +} + func (u *Updater) episodeName(feedConfig *config.Feed, episode *model.Episode) string { ext := "mp4" if feedConfig.Format == model.FormatAudio { diff --git a/cmd/podsync/updater_test.go b/cmd/podsync/updater_test.go new file mode 100644 index 0000000..d0f8076 --- /dev/null +++ b/cmd/podsync/updater_test.go @@ -0,0 +1,26 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/mxpv/podsync/pkg/config" +) + +func TestUpdater_hostname(t *testing.T) { + u := Updater{ + config: &config.Config{ + Server: config.Server{ + Hostname: "localhost", + Port: 7979, + }, + }, + } + + assert.Equal(t, "http://localhost", u.hostname()) + + // Trim end slash + u.config.Server.Hostname = "https://localhost:8080/" + assert.Equal(t, "https://localhost:8080", u.hostname()) +} diff --git a/pkg/config/config.go b/pkg/config/config.go index dcaa8e0..654cf0c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "time" "github.com/BurntSushi/toml" @@ -103,7 +104,11 @@ func (c *Config) validate() error { func (c *Config) applyDefaults() { if c.Server.Hostname == "" { - c.Server.Hostname = model.DefaultHostname + if c.Server.Port != 0 && c.Server.Port != 80 { + c.Server.Hostname = fmt.Sprintf("http://localhost:%d", c.Server.Port) + } else { + c.Server.Hostname = "http://localhost" + } } for _, feed := range c.Feeds { diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 2076396..c6c09e3 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -89,3 +89,28 @@ data_dir = "/data" assert.EqualValues(t, feed.Quality, "high") assert.EqualValues(t, feed.Format, "video") } + +func TestDefaultHostname(t *testing.T) { + cfg := Config{ + Server: Server{}, + } + + t.Run("empty hostname", func(t *testing.T) { + cfg.applyDefaults() + assert.Equal(t, "http://localhost", cfg.Server.Hostname) + }) + + t.Run("empty hostname with port", func(t *testing.T) { + cfg.Server.Hostname = "" + cfg.Server.Port = 7979 + cfg.applyDefaults() + assert.Equal(t, "http://localhost:7979", cfg.Server.Hostname) + }) + + t.Run("skip overwrite", func(t *testing.T) { + cfg.Server.Hostname = "https://my.host:4443" + cfg.Server.Port = 80 + cfg.applyDefaults() + assert.Equal(t, "https://my.host:4443", cfg.Server.Hostname) + }) +} diff --git a/pkg/model/defaults.go b/pkg/model/defaults.go index bdbe784..db0a938 100644 --- a/pkg/model/defaults.go +++ b/pkg/model/defaults.go @@ -5,7 +5,6 @@ import ( ) const ( - DefaultHostname = "http://localhost" DefaultFormat = FormatVideo DefaultQuality = QualityHigh DefaultPageSize = 50