Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server bind option #190

Merged
merged 3 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ Here is an example how configuration might look like:
```toml
[server]
port = 8080
# Bind a specific IP addresses for server ,"*": bind all IP addresses which is default option, localhost or 127.0.0.1 bind a single IPv4 address
bind_address = "172.20.10.2"
# Specify path for reverse proxy and only [A-Za-z0-9]
path = "test"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please update the comment that this is related to URL path specifically? Possibly with en example.
(I was confused for a moment why we need both path and data_dir until I referred to Go's documentation).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it

data_dir = "/app/data" # Don't change if you run podsync via docker

# Tokens from `Access tokens` section
Expand Down
13 changes: 9 additions & 4 deletions cmd/podsync/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ func NewServer(cfg *config.Config) *Server {
if port == 0 {
port = 8080
}

bindAddress := cfg.Server.BindAddress
if bindAddress == "*" {
bindAddress = ""
}
srv := Server{}

srv.Addr = fmt.Sprintf(":%d", port)
log.Debugf("using address: %s", srv.Addr)
srv.Addr = fmt.Sprintf("%s:%d", bindAddress, port)
log.Debugf("using address: %s:%s", bindAddress, srv.Addr)

fs := http.FileServer(http.Dir(cfg.Server.DataDir))
http.Handle("/", fs)
path := cfg.Server.Path
http.Handle(fmt.Sprintf("/%s", path), fs)
log.Debugf("handle path: /%s", path)

return &srv
}
16 changes: 15 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"path/filepath"
"regexp"

"github.com/hashicorp/go-multierror"
"github.com/naoina/toml"
Expand Down Expand Up @@ -71,6 +72,12 @@ type Server struct {
Hostname string `toml:"hostname"`
// Port is a server port to listen to
Port int `toml:"port"`
// Bind a specific IP addresses for server
// "*": bind all IP addresses which is default option
// localhost or 127.0.0.1 bind a single IPv4 address
BindAddress string `toml:"bind_address"`
// Specify path for reverse proxy and only [A-Za-z0-9]
Path string `toml:"path"`
// DataDir is a path to a directory to keep XML feeds and downloaded episodes,
// that will be available to user via web server for download.
DataDir string `toml:"data_dir"`
Expand Down Expand Up @@ -163,8 +170,15 @@ func (c *Config) validate() error {
result = multierror.Append(result, errors.New("data directory is required"))
}

if c.Server.Path != "" {
var pathReg = regexp.MustCompile(model.PathRegex)
if !pathReg.MatchString(c.Server.Path) {
result = multierror.Append(result, errors.Errorf("Server handle path must be match %s or empty", model.PathRegex))
}
}

if len(c.Feeds) == 0 {
result = multierror.Append(result, errors.New("at least one feed must be speficied"))
result = multierror.Append(result, errors.New("at least one feed must be specified"))
}

for id, 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 @@ -135,6 +135,31 @@ data_dir = "/data"
assert.EqualValues(t, feed.Format, "video")
}

func TestHttpServerListenAddress(t *testing.T) {
const file = `
[server]
bind_address = "172.20.10.2"
port = 8080
path = "test"
data_dir = "/data"

[feeds]
[feeds.A]
url = "https://youtube.com/watch?v=ygIUF678y40"

[database]
badger = { truncate = true, file_io = true }
`
path := setup(t, file)
defer os.Remove(path)

config, err := LoadConfig(path)
assert.NoError(t, err)
require.NotNil(t, config)
require.NotNil(t, config.Server.BindAddress)
require.NotNil(t, config.Server.Path)
}

func TestDefaultHostname(t *testing.T) {
cfg := Config{
Server: Server{},
Expand Down
1 change: 1 addition & 0 deletions pkg/model/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ const (
DefaultLogMaxSize = 50 // megabytes
DefaultLogMaxAge = 30 // days
DefaultLogMaxBackups = 7
PathRegex = `^[A-Za-z0-9]+$`
)