Skip to content

Commit

Permalink
Allow CLI to be hidden behind reverse proxy #47 #43
Browse files Browse the repository at this point in the history
  • Loading branch information
mxpv committed Nov 23, 2019
1 parent be3fe06 commit 5a85228
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ dist/
venv/

.DS_Store
podsync
/podsync
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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

Expand Down
17 changes: 11 additions & 6 deletions cmd/podsync/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions cmd/podsync/updater_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
7 changes: 6 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"time"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -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)

This comment has been minimized.

Copy link
@realgabriele

realgabriele Mar 8, 2021

			c.Server.Hostname = fmt.Sprintf("http://%s:%d", c.Server.BindAddress, c.Server.Port)

use the configured bind address instead of localhost.
The same whoud be done in line 110.

} else {
c.Server.Hostname = "http://localhost"
}
}

for _, feed := range c.Feeds {
Expand Down
25 changes: 25 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
1 change: 0 additions & 1 deletion pkg/model/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
)

const (
DefaultHostname = "http://localhost"
DefaultFormat = FormatVideo
DefaultQuality = QualityHigh
DefaultPageSize = 50
Expand Down

0 comments on commit 5a85228

Please sign in to comment.