1
0
mirror of https://github.com/mxpv/podsync.git synced 2024-05-11 05:55:04 +00:00
2022-01-02 14:57:10 +02:00

167 lines
4.3 KiB
Go

package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"github.com/hashicorp/go-multierror"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
"github.com/mxpv/podsync/pkg/db"
"github.com/mxpv/podsync/pkg/feed"
"github.com/mxpv/podsync/pkg/model"
"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 Log struct {
// Filename to write the log to (instead of stdout)
Filename string `toml:"filename"`
// MaxSize is the maximum size of the log file in MB
MaxSize int `toml:"max_size"`
// MaxBackups is the maximum number of log file backups to keep after rotation
MaxBackups int `toml:"max_backups"`
// MaxAge is the maximum number of days to keep the logs for
MaxAge int `toml:"max_age"`
// Compress old backups
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)
if err != nil {
return nil, errors.Wrapf(err, "failed to read config file: %s", path)
}
config := Config{}
if err := toml.Unmarshal(data, &config); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal toml")
}
for id, f := range config.Feeds {
f.ID = id
}
config.applyDefaults(path)
if err := config.validate(); err != nil {
return nil, err
}
return &config, nil
}
func (c *Config) validate() error {
var result *multierror.Error
if c.Server.DataDir == "" {
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 specified"))
}
for id, f := range c.Feeds {
if f.URL == "" {
result = multierror.Append(result, errors.Errorf("URL is required for %q", id))
}
}
return result.ErrorOrNil()
}
func (c *Config) applyDefaults(configPath string) {
if c.Server.Hostname == "" {
if c.Server.Port != 0 && c.Server.Port != 80 {
c.Server.Hostname = fmt.Sprintf("http://localhost:%d", c.Server.Port)
} else {
c.Server.Hostname = "http://localhost"
}
}
if c.Log.Filename != "" {
if c.Log.MaxSize == 0 {
c.Log.MaxSize = model.DefaultLogMaxSize
}
if c.Log.MaxAge == 0 {
c.Log.MaxAge = model.DefaultLogMaxAge
}
if c.Log.MaxBackups == 0 {
c.Log.MaxBackups = model.DefaultLogMaxBackups
}
}
if c.Database.Dir == "" {
c.Database.Dir = filepath.Join(filepath.Dir(configPath), "db")
}
for _, feed := range c.Feeds {
if feed.UpdatePeriod == 0 {
feed.UpdatePeriod = model.DefaultUpdatePeriod
}
if feed.Quality == "" {
feed.Quality = model.DefaultQuality
}
if feed.Custom.CoverArtQuality == "" {
feed.Custom.CoverArtQuality = model.DefaultQuality
}
if feed.Format == "" {
feed.Format = model.DefaultFormat
}
if feed.PageSize == 0 {
feed.PageSize = model.DefaultPageSize
}
if feed.PlaylistSort == "" {
feed.PlaylistSort = model.SortingAsc
}
}
}