Major refactor
parent
914268a320
commit
2d9b59beb0
@ -0,0 +1,16 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch Package",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${fileDirname}",
|
||||
"args": ["-s"]
|
||||
}
|
||||
],
|
||||
}
|
@ -1,47 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/go-yaml/yaml"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Root string
|
||||
BaseURL string
|
||||
MaxSize int64 // maximum feed size before rotating
|
||||
Feeds map[string]string // name -> url
|
||||
Addr string
|
||||
Debug bool
|
||||
|
||||
path string // path to config file that was loaded used by .Save()
|
||||
}
|
||||
DataDir string
|
||||
BaseURL string
|
||||
FeedsFile string
|
||||
MaxFeedSize int64 // maximum feed size before rotating
|
||||
|
||||
func (conf *Config) Parse(data []byte) error {
|
||||
return yaml.Unmarshal(data, conf)
|
||||
Feeds map[string]*Feed // name -> url
|
||||
}
|
||||
|
||||
func (conf *Config) Save() error {
|
||||
data, err := yaml.Marshal(conf)
|
||||
func (conf *Config) LoadFeeds() error {
|
||||
f, err := os.Open(conf.FeedsFile)
|
||||
if err != nil {
|
||||
return err
|
||||
log.WithError(err).Errorf("error opening feeds file %s", conf.FeedsFile)
|
||||
return fmt.Errorf("error opening feeds file %s: %w", conf.FeedsFile, err)
|
||||
}
|
||||
data = append([]byte("---\n"), data...)
|
||||
return ioutil.WriteFile(conf.path, data, 0644)
|
||||
defer f.Close()
|
||||
|
||||
data, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("error reading feeds file %s", conf.FeedsFile)
|
||||
return fmt.Errorf("error reading feeds file %s: %w", conf.FeedsFile, err)
|
||||
}
|
||||
|
||||
if err := yaml.Unmarshal(data, conf.Feeds); err != nil {
|
||||
log.WithError(err).Errorf("error parsing feeds file %s", conf.FeedsFile)
|
||||
return fmt.Errorf("error parsing feeds file %s: %w", conf.FeedsFile, err)
|
||||
}
|
||||
|
||||
for _, feed := range conf.Feeds {
|
||||
fn := filepath.Join(conf.DataDir, fmt.Sprintf("%s.png", feed.Name))
|
||||
log.Debugf("Exists(fn): %t", Exists(fn))
|
||||
log.Debugf("feed.Avatar: %q", feed.Avatar)
|
||||
if Exists(fn) && feed.Avatar == "" {
|
||||
feed.Avatar = fmt.Sprintf("%s/%s/avatar.png", conf.BaseURL, feed.Name)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoadConfig(filename string) (*Config, error) {
|
||||
data, err := ioutil.ReadFile(filename)
|
||||
func (conf *Config) SaveFeeds() error {
|
||||
f, err := os.OpenFile(conf.FeedsFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
log.WithError(err).Errorf("error opening feeds file %s", conf.FeedsFile)
|
||||
return fmt.Errorf("error opening feeds file %s: %w", conf.FeedsFile, err)
|
||||
}
|
||||
conf := &Config{}
|
||||
if err := conf.Parse(data); err != nil {
|
||||
return nil, err
|
||||
defer f.Close()
|
||||
|
||||
data, err := yaml.Marshal(conf.Feeds)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("error serializing feeds")
|
||||
return fmt.Errorf("error serializing feeds: %w", err)
|
||||
}
|
||||
conf.path = filename
|
||||
|
||||
if conf.Feeds == nil {
|
||||
conf.Feeds = make(map[string]string)
|
||||
data = append([]byte("---\n"), data...)
|
||||
|
||||
if _, err := f.Write(data); err != nil {
|
||||
log.WithError(err).Errorf("error writing feeds file %s", conf.FeedsFile)
|
||||
return fmt.Errorf("error writing feeds file %s: %w", conf.FeedsFile, err)
|
||||
}
|
||||
|
||||
return conf, nil
|
||||
return nil
|
||||
}
|
||||
|
@ -1,6 +0,0 @@
|
||||
---
|
||||
root: ./feeds
|
||||
baseurl: /
|
||||
maxsize: 1048576
|
||||
feeds:
|
||||
unexplained_mysteries: http://www.unexplained-mysteries.com/news/umnews.xml
|
@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
// DefaultDebug is the default debug mode
|
||||
DefaultDebug = false
|
||||
|
||||
// DefaultDataDir is the default data directory for storage
|
||||
DefaultDataDir = "./data"
|
||||
|
||||
// DefaultAddr is the default bind address of the server
|
||||
DefaultAddr = "0.0.0.0:8000"
|
||||
|
||||
// DefaultBaseURL is the default Base URL for the app used to construct feed URLs
|
||||
DefaultBaseURL = "http://0.0.0.0:8000"
|
||||
|
||||
// DefaultFeedsFile is the default feeds configuration filename used by the server
|
||||
DefaultFeedsFile = "feeds.yaml"
|
||||
|
||||
// DefaultMaxFeedSize is the default maximum feed size before rotation
|
||||
DefaultMaxFeedSize = 1 << 20 // ~1MB
|
||||
)
|
||||
|
||||
func NewConfig() *Config {
|
||||
return &Config{
|
||||
Addr: DefaultAddr,
|
||||
Debug: DefaultDebug,
|
||||
|
||||
DataDir: DefaultDataDir,
|
||||
BaseURL: DefaultBaseURL,
|
||||
FeedsFile: DefaultFeedsFile,
|
||||
MaxFeedSize: DefaultMaxFeedSize,
|
||||
|
||||
Feeds: make(map[string]*Feed),
|
||||
}
|
||||
}
|
||||
|
||||
// Option is a function that takes a config struct and modifies it
|
||||
type Option func(*Config) error
|
||||
|
||||
// WithDebug sets the debug mode lfag
|
||||
func WithDebug(debug bool) Option {
|
||||
return func(cfg *Config) error {
|
||||
cfg.Debug = debug
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithBind sets the server's listening bind address
|
||||
func WithBind(addr string) Option {
|
||||
return func(cfg *Config) error {
|
||||
cfg.Addr = addr
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithDataDir sets the data directory to use for storage
|
||||
func WithDataDir(dataDir string) Option {
|
||||
return func(cfg *Config) error {
|
||||
if err := os.MkdirAll(dataDir, 0755); err != nil {
|
||||
return fmt.Errorf("error creating data-dir %s: %w", dataDir, err)
|
||||
}
|
||||
cfg.DataDir = dataDir
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithFeedsFile set the feeds configuration file used by the server
|
||||
func WithFeedsFile(feedsFile string) Option {
|
||||
return func(cfg *Config) error {
|
||||
cfg.FeedsFile = feedsFile
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithBaseURL sets the Base URL used for constructing feed URLs
|
||||
func WithBaseURL(baseURL string) Option {
|
||||
return func(cfg *Config) error {
|
||||
cfg.BaseURL = baseURL
|
||||
return nil
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue