Skip to content

Commit 2751c4b

Browse files
authored
Add server bind option (#190)
* add Server.path and Server.bindAddress two options to special http server handle path and Bind Address. * Update server.go Co-authored-by: op69qs <L7Itt9N1VfByhUEs>
1 parent d39e464 commit 2751c4b

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ Here is an example how configuration might look like:
5757
```toml
5858
[server]
5959
port = 8080
60+
# 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
61+
bind_address = "172.20.10.2"
62+
# Specify path for reverse proxy and only [A-Za-z0-9]
63+
path = "test"
6064
data_dir = "/app/data" # Don't change if you run podsync via docker
6165

6266
# Tokens from `Access tokens` section

cmd/podsync/server.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@ func NewServer(cfg *config.Config) *Server {
1818
if port == 0 {
1919
port = 8080
2020
}
21-
21+
bindAddress := cfg.Server.BindAddress
22+
if bindAddress == "*" {
23+
bindAddress = ""
24+
}
2225
srv := Server{}
2326

24-
srv.Addr = fmt.Sprintf(":%d", port)
25-
log.Debugf("using address: %s", srv.Addr)
27+
srv.Addr = fmt.Sprintf("%s:%d", bindAddress, port)
28+
log.Debugf("using address: %s:%s", bindAddress, srv.Addr)
2629

2730
fs := http.FileServer(http.Dir(cfg.Server.DataDir))
28-
http.Handle("/", fs)
31+
path := cfg.Server.Path
32+
http.Handle(fmt.Sprintf("/%s", path), fs)
33+
log.Debugf("handle path: /%s", path)
2934

3035
return &srv
3136
}

pkg/config/config.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io/ioutil"
66
"path/filepath"
7+
"regexp"
78

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

173+
if c.Server.Path != "" {
174+
var pathReg = regexp.MustCompile(model.PathRegex)
175+
if !pathReg.MatchString(c.Server.Path) {
176+
result = multierror.Append(result, errors.Errorf("Server handle path must be match %s or empty", model.PathRegex))
177+
}
178+
}
179+
166180
if len(c.Feeds) == 0 {
167-
result = multierror.Append(result, errors.New("at least one feed must be speficied"))
181+
result = multierror.Append(result, errors.New("at least one feed must be specified"))
168182
}
169183

170184
for id, feed := range c.Feeds {

pkg/config/config_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,31 @@ data_dir = "/data"
135135
assert.EqualValues(t, feed.Format, "video")
136136
}
137137

138+
func TestHttpServerListenAddress(t *testing.T) {
139+
const file = `
140+
[server]
141+
bind_address = "172.20.10.2"
142+
port = 8080
143+
path = "test"
144+
data_dir = "/data"
145+
146+
[feeds]
147+
[feeds.A]
148+
url = "https://youtube.com/watch?v=ygIUF678y40"
149+
150+
[database]
151+
badger = { truncate = true, file_io = true }
152+
`
153+
path := setup(t, file)
154+
defer os.Remove(path)
155+
156+
config, err := LoadConfig(path)
157+
assert.NoError(t, err)
158+
require.NotNil(t, config)
159+
require.NotNil(t, config.Server.BindAddress)
160+
require.NotNil(t, config.Server.Path)
161+
}
162+
138163
func TestDefaultHostname(t *testing.T) {
139164
cfg := Config{
140165
Server: Server{},

pkg/model/defaults.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ const (
1212
DefaultLogMaxSize = 50 // megabytes
1313
DefaultLogMaxAge = 30 // days
1414
DefaultLogMaxBackups = 7
15+
PathRegex = `^[A-Za-z0-9]+$`
1516
)

0 commit comments

Comments
 (0)