56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
package rss
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"log/slog"
|
||
|
|
"net/http"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"go.dev.pztrn.name/rsser/configuration"
|
||
|
|
"go.dev.pztrn.name/rsser/outputs"
|
||
|
|
)
|
||
|
|
|
||
|
|
var (
|
||
|
|
errNoOutputs = errors.New("no outputs")
|
||
|
|
errRSS = errors.New("RSS controller")
|
||
|
|
)
|
||
|
|
|
||
|
|
// RSS is an RSS feeds parsing controlling structure.
|
||
|
|
type RSS struct {
|
||
|
|
cfg *configuration.Config
|
||
|
|
httpClient *http.Client
|
||
|
|
outputs []outputs.Output
|
||
|
|
}
|
||
|
|
|
||
|
|
// New creates new RSS parsing instance.
|
||
|
|
func New(cfg *configuration.Config, outs []outputs.Output) (*RSS, error) {
|
||
|
|
rss := &RSS{
|
||
|
|
cfg: cfg,
|
||
|
|
outputs: outs,
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := rss.initialize(); err != nil {
|
||
|
|
return nil, fmt.Errorf("%w: %w", errRSS, err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return rss, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *RSS) initialize() error {
|
||
|
|
if len(r.outputs) == 0 {
|
||
|
|
return fmt.Errorf("initialize: %w", errNoOutputs)
|
||
|
|
}
|
||
|
|
|
||
|
|
slog.Info(
|
||
|
|
"HTTP client configuration.",
|
||
|
|
"request_timeout_seconds", r.cfg.RSS.HTTPClient.RequestTimeoutSeconds,
|
||
|
|
)
|
||
|
|
|
||
|
|
r.httpClient = &http.Client{
|
||
|
|
Timeout: time.Second * time.Duration(r.cfg.RSS.HTTPClient.RequestTimeoutSeconds),
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|