diff --git a/cmd/podsync/config.go b/cmd/podsync/config.go index bedffd5..86c5672 100644 --- a/cmd/podsync/config.go +++ b/cmd/podsync/config.go @@ -13,23 +13,24 @@ import ( "github.com/mxpv/podsync/pkg/db" "github.com/mxpv/podsync/pkg/feed" "github.com/mxpv/podsync/pkg/model" + "github.com/mxpv/podsync/pkg/server" "github.com/mxpv/podsync/pkg/ytdl" ) -type ServerConfig struct { - // Hostname to use for download links - 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"` +type Config struct { + // Server is the web server configuration + Server server.Config `toml:"server"` + // Log is the optional logging configuration + Log Log `toml:"log"` + // Database configuration + Database db.Config `toml:"database"` + // Feeds is a list of feeds to host by this app. + // ID will be used as feed ID in http://podsync.net/{FEED_ID}.xml + Feeds map[string]*feed.Config + // Tokens is API keys to use to access YouTube/Vimeo APIs. + Tokens map[model.Provider][]string `toml:"tokens"` + // Downloader (youtube-dl) configuration + Downloader ytdl.Config `toml:"downloader"` } type Log struct { @@ -45,22 +46,6 @@ type Log struct { Compress bool `toml:"compress"` } -type Config struct { - // Server is the web server configuration - Server ServerConfig `toml:"server"` - // Log is the optional logging configuration - Log Log `toml:"log"` - // Database configuration - Database db.Config `toml:"database"` - // Feeds is a list of feeds to host by this app. - // ID will be used as feed ID in http://podsync.net/{FEED_ID}.xml - Feeds map[string]*feed.Config - // Tokens is API keys to use to access YouTube/Vimeo APIs. - Tokens map[model.Provider][]string `toml:"tokens"` - // Downloader (youtube-dl) configuration - Downloader ytdl.Config `toml:"downloader"` -} - // LoadConfig loads TOML configuration from a file path func LoadConfig(path string) (*Config, error) { data, err := ioutil.ReadFile(path) diff --git a/cmd/podsync/config_test.go b/cmd/podsync/config_test.go index c078cee..fe2dd9e 100644 --- a/cmd/podsync/config_test.go +++ b/cmd/podsync/config_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/require" "github.com/mxpv/podsync/pkg/model" + "github.com/mxpv/podsync/pkg/server" ) func TestLoadConfig(t *testing.T) { @@ -173,7 +174,7 @@ data_dir = "/data" func TestDefaultHostname(t *testing.T) { cfg := Config{ - Server: ServerConfig{}, + Server: server.Config{}, } t.Run("empty hostname", func(t *testing.T) { diff --git a/cmd/podsync/main.go b/cmd/podsync/main.go index 7d04867..c76a785 100644 --- a/cmd/podsync/main.go +++ b/cmd/podsync/main.go @@ -11,6 +11,7 @@ import ( "github.com/jessevdk/go-flags" "github.com/mxpv/podsync/pkg/feed" + "github.com/mxpv/podsync/pkg/server" "github.com/robfig/cron/v3" log "github.com/sirupsen/logrus" "golang.org/x/sync/errgroup" @@ -179,7 +180,7 @@ func main() { }) // Run web server - srv := NewServer(cfg, storage) + srv := server.New(cfg.Server, storage) group.Go(func() error { log.Infof("running listener at %s", srv.Addr) diff --git a/cmd/podsync/server.go b/cmd/podsync/server.go deleted file mode 100644 index 809e871..0000000 --- a/cmd/podsync/server.go +++ /dev/null @@ -1,36 +0,0 @@ -package main - -import ( - "fmt" - "net/http" - - log "github.com/sirupsen/logrus" -) - -type Server struct { - http.Server -} - -func NewServer(cfg *Config, storage http.FileSystem) *Server { - port := cfg.Server.Port - if port == 0 { - port = 8080 - } - - bindAddress := cfg.Server.BindAddress - if bindAddress == "*" { - bindAddress = "" - } - - srv := Server{} - - srv.Addr = fmt.Sprintf("%s:%d", bindAddress, port) - log.Debugf("using address: %s:%s", bindAddress, srv.Addr) - - fileServer := http.FileServer(storage) - - log.Debugf("handle path: /%s", cfg.Server.Path) - http.Handle(fmt.Sprintf("/%s", cfg.Server.Path), fileServer) - - return &srv -} diff --git a/pkg/server/server.go b/pkg/server/server.go new file mode 100644 index 0000000..5342417 --- /dev/null +++ b/pkg/server/server.go @@ -0,0 +1,52 @@ +package server + +import ( + "fmt" + "net/http" + + log "github.com/sirupsen/logrus" +) + +type Server struct { + http.Server +} + +type Config struct { + // Hostname to use for download links + 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"` +} + +func New(cfg Config, storage http.FileSystem) *Server { + port := cfg.Port + if port == 0 { + port = 8080 + } + + bindAddress := cfg.BindAddress + if bindAddress == "*" { + bindAddress = "" + } + + srv := Server{} + + srv.Addr = fmt.Sprintf("%s:%d", bindAddress, port) + log.Debugf("using address: %s:%s", bindAddress, srv.Addr) + + fileServer := http.FileServer(storage) + + log.Debugf("handle path: /%s", cfg.Path) + http.Handle(fmt.Sprintf("/%s", cfg.Path), fileServer) + + return &srv +}