90 lines
2 KiB
Go
90 lines
2 KiB
Go
package main
|
|
|
|
import (
|
|
"github.com/pelletier/go-toml/v2"
|
|
"os"
|
|
"io/ioutil"
|
|
"sync"
|
|
"log"
|
|
"strings"
|
|
)
|
|
|
|
type GenericError struct { msg string }
|
|
func (e *GenericError) Error() string { return e.msg }
|
|
|
|
type MPVPair struct {
|
|
Option string `toml:"option"`
|
|
Value string `toml:"value"`
|
|
}
|
|
|
|
type PodcastConfig struct {
|
|
Name string `toml:"name"`
|
|
URL string `toml:"url"`
|
|
UseAuth bool
|
|
User string `toml:"user"`
|
|
Pass string `toml:"pass"`
|
|
Convert string `toml:"convert"`
|
|
FilterType string `toml:"filter-type"`
|
|
FilterDirAsc bool `toml:"filter-direction-asc"`
|
|
FilterLimit uint `toml:"filter-limit"`
|
|
MPVOptions []MPVPair `toml:"mpv-options"`
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
MediaProxyBaseURL string `toml:"media-proxy-base-url"`
|
|
Port int `toml:"port"`
|
|
Feeds []PodcastConfig `toml:"podcasts"`
|
|
FileRoot string `toml:"file-root"`
|
|
UpdateTimeout uint `toml:"update-timeout"`
|
|
}
|
|
|
|
func (cfg *ServerConfig) Load(path string) error {
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
return err;
|
|
}
|
|
file, err := os.Open(path)
|
|
if err!=nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
content, err := ioutil.ReadAll(file)
|
|
if err!=nil {
|
|
return err
|
|
}
|
|
if err := toml.Unmarshal(content, cfg); err!=nil {
|
|
return err
|
|
}
|
|
for _, feed := range cfg.Feeds {
|
|
if strings.Contains(feed.Name, "/") {
|
|
log.Fatal("invalid feed name contains '/': "+feed.Name)
|
|
}
|
|
if feed.User!="" && feed.Pass!="" {
|
|
feed.UseAuth = true
|
|
} else {
|
|
feed.UseAuth = false
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type FeedCacheEntry struct {
|
|
Mutex sync.Mutex
|
|
Text string
|
|
}
|
|
|
|
type FeedCacheS struct {
|
|
Texts map[string]string
|
|
sync.Mutex
|
|
}
|
|
|
|
type ServerContext struct {
|
|
Config ServerConfig
|
|
WG sync.WaitGroup
|
|
IsActive bool
|
|
FeedCache FeedCacheS
|
|
}
|
|
|
|
func (ctx *ServerContext) LoadConfig(path string) error {
|
|
ctx.FeedCache.Texts = make(map[string]string)
|
|
return ctx.Config.Load(path);
|
|
}
|