Skip to content

Commit 142b0e0

Browse files
committed
Add server package
1 parent 16315c0 commit 142b0e0

File tree

5 files changed

+71
-68
lines changed

5 files changed

+71
-68
lines changed

cmd/podsync/config.go

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,13 @@ import (
1313
"github.com/mxpv/podsync/pkg/db"
1414
"github.com/mxpv/podsync/pkg/feed"
1515
"github.com/mxpv/podsync/pkg/model"
16+
"github.com/mxpv/podsync/pkg/server"
1617
"github.com/mxpv/podsync/pkg/ytdl"
1718
)
1819

19-
type ServerConfig struct {
20-
// Hostname to use for download links
21-
Hostname string `toml:"hostname"`
22-
// Port is a server port to listen to
23-
Port int `toml:"port"`
24-
// Bind a specific IP addresses for server
25-
// "*": bind all IP addresses which is default option
26-
// localhost or 127.0.0.1 bind a single IPv4 address
27-
BindAddress string `toml:"bind_address"`
28-
// Specify path for reverse proxy and only [A-Za-z0-9]
29-
Path string `toml:"path"`
30-
// DataDir is a path to a directory to keep XML feeds and downloaded episodes,
31-
// that will be available to user via web server for download.
32-
DataDir string `toml:"data_dir"`
33-
}
34-
35-
type Log struct {
36-
// Filename to write the log to (instead of stdout)
37-
Filename string `toml:"filename"`
38-
// MaxSize is the maximum size of the log file in MB
39-
MaxSize int `toml:"max_size"`
40-
// MaxBackups is the maximum number of log file backups to keep after rotation
41-
MaxBackups int `toml:"max_backups"`
42-
// MaxAge is the maximum number of days to keep the logs for
43-
MaxAge int `toml:"max_age"`
44-
// Compress old backups
45-
Compress bool `toml:"compress"`
46-
}
47-
4820
type Config struct {
4921
// Server is the web server configuration
50-
Server ServerConfig `toml:"server"`
22+
Server server.Config `toml:"server"`
5123
// Log is the optional logging configuration
5224
Log Log `toml:"log"`
5325
// Database configuration
@@ -61,6 +33,19 @@ type Config struct {
6133
Downloader ytdl.Config `toml:"downloader"`
6234
}
6335

36+
type Log struct {
37+
// Filename to write the log to (instead of stdout)
38+
Filename string `toml:"filename"`
39+
// MaxSize is the maximum size of the log file in MB
40+
MaxSize int `toml:"max_size"`
41+
// MaxBackups is the maximum number of log file backups to keep after rotation
42+
MaxBackups int `toml:"max_backups"`
43+
// MaxAge is the maximum number of days to keep the logs for
44+
MaxAge int `toml:"max_age"`
45+
// Compress old backups
46+
Compress bool `toml:"compress"`
47+
}
48+
6449
// LoadConfig loads TOML configuration from a file path
6550
func LoadConfig(path string) (*Config, error) {
6651
data, err := ioutil.ReadFile(path)

cmd/podsync/config_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/stretchr/testify/require"
1111

1212
"github.com/mxpv/podsync/pkg/model"
13+
"github.com/mxpv/podsync/pkg/server"
1314
)
1415

1516
func TestLoadConfig(t *testing.T) {
@@ -173,7 +174,7 @@ data_dir = "/data"
173174

174175
func TestDefaultHostname(t *testing.T) {
175176
cfg := Config{
176-
Server: ServerConfig{},
177+
Server: server.Config{},
177178
}
178179

179180
t.Run("empty hostname", func(t *testing.T) {

cmd/podsync/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/jessevdk/go-flags"
1313
"github.com/mxpv/podsync/pkg/feed"
14+
"github.com/mxpv/podsync/pkg/server"
1415
"github.com/robfig/cron/v3"
1516
log "github.com/sirupsen/logrus"
1617
"golang.org/x/sync/errgroup"
@@ -179,7 +180,7 @@ func main() {
179180
})
180181

181182
// Run web server
182-
srv := NewServer(cfg, storage)
183+
srv := server.New(cfg.Server, storage)
183184

184185
group.Go(func() error {
185186
log.Infof("running listener at %s", srv.Addr)

cmd/podsync/server.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

pkg/server/server.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package server
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
log "github.com/sirupsen/logrus"
8+
)
9+
10+
type Server struct {
11+
http.Server
12+
}
13+
14+
type Config struct {
15+
// Hostname to use for download links
16+
Hostname string `toml:"hostname"`
17+
// Port is a server port to listen to
18+
Port int `toml:"port"`
19+
// Bind a specific IP addresses for server
20+
// "*": bind all IP addresses which is default option
21+
// localhost or 127.0.0.1 bind a single IPv4 address
22+
BindAddress string `toml:"bind_address"`
23+
// Specify path for reverse proxy and only [A-Za-z0-9]
24+
Path string `toml:"path"`
25+
// DataDir is a path to a directory to keep XML feeds and downloaded episodes,
26+
// that will be available to user via web server for download.
27+
DataDir string `toml:"data_dir"`
28+
}
29+
30+
func New(cfg Config, storage http.FileSystem) *Server {
31+
port := cfg.Port
32+
if port == 0 {
33+
port = 8080
34+
}
35+
36+
bindAddress := cfg.BindAddress
37+
if bindAddress == "*" {
38+
bindAddress = ""
39+
}
40+
41+
srv := Server{}
42+
43+
srv.Addr = fmt.Sprintf("%s:%d", bindAddress, port)
44+
log.Debugf("using address: %s:%s", bindAddress, srv.Addr)
45+
46+
fileServer := http.FileServer(storage)
47+
48+
log.Debugf("handle path: /%s", cfg.Path)
49+
http.Handle(fmt.Sprintf("/%s", cfg.Path), fileServer)
50+
51+
return &srv
52+
}

0 commit comments

Comments
 (0)