Skip to content

Commit

Permalink
Makes configuration reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
waybackarchiver committed Mar 6, 2023
1 parent 1ffabac commit 0ff8c0a
Show file tree
Hide file tree
Showing 54 changed files with 557 additions and 479 deletions.
13 changes: 10 additions & 3 deletions cmd/playback/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,33 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/wabarc/logger"
"github.com/wabarc/playback"
"github.com/wabarc/wayback"
"github.com/wabarc/wayback/config"
)

func main() {
opts, err := config.NewParser().ParseEnvironmentVariables()
if err != nil {
logger.Fatal("Parse environment variables or flags failed, error: %v", err)
}

var rootCmd = &cobra.Command{
Use: "playback",
Short: "A toolkit to playback archived webpage from time capsules.",
Example: ` playback https://example.com https://example.org`,
Version: playback.Version,
Run: func(cmd *cobra.Command, args []string) {
handle(cmd, args)
handle(cmd, opts, args)
},
}

// nolint:errcheck
rootCmd.Execute()
}

func handle(cmd *cobra.Command, args []string) {
func handle(cmd *cobra.Command, opts *config.Options, args []string) {
if len(args) < 1 {
// nolint:errcheck
cmd.Usage()
Expand All @@ -42,7 +49,7 @@ func handle(cmd *cobra.Command, args []string) {
os.Exit(1)
}

collects, err := wayback.Playback(context.TODO(), urls...)
collects, err := wayback.Playback(context.TODO(), opts, urls...)
if err != nil {
cmd.Println(err)
os.Exit(1)
Expand Down
19 changes: 10 additions & 9 deletions cmd/wayback/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,28 +163,29 @@ func handle(cmd *cobra.Command, args []string) {
setToEnv(cmd)
parser := config.NewParser()

var opts *config.Options
if len(daemon) > 0 {
logger.Info("Run wayback using configuration file")
if config.Opts, err = parser.ParseFile(configFile); err != nil {
if opts, err = parser.ParseFile(configFile); err != nil {
logger.Fatal("Parse configuration file failed, error: %v", err)
}
}

if config.Opts, err = parser.ParseEnvironmentVariables(); err != nil {
if opts, err = parser.ParseEnvironmentVariables(); err != nil {
logger.Fatal("Parse environment variables or flags failed, error: %v", err)
}

if !config.Opts.LogTime() {
if !opts.LogTime() {
logger.DisableTime()
}

logger.SetLogLevel(config.Opts.LogLevel())
if debug || config.Opts.HasDebugMode() {
logger.SetLogLevel(opts.LogLevel())
if debug || opts.HasDebugMode() {
profiling()
logger.EnableDebug()
}

if config.Opts.EnabledMetrics() {
if opts.EnabledMetrics() {
metrics.Gather = metrics.NewCollector()
}

Expand All @@ -194,7 +195,7 @@ func handle(cmd *cobra.Command, args []string) {
}

if print {
cmd.Println(spew.Sdump(config.Opts))
cmd.Println(spew.Sdump(opts))
return
}

Expand All @@ -204,9 +205,9 @@ func handle(cmd *cobra.Command, args []string) {
hasArgs := len(args) > 0
switch {
case hasDaemon:
serve(cmd, args)
serve(cmd, opts, args)
case hasArgs:
archive(cmd, args)
archive(cmd, opts, args)
default:
// nolint:errcheck
cmd.Help()
Expand Down
38 changes: 19 additions & 19 deletions cmd/wayback/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ type services struct {
targets []target
}

func serve(_ *cobra.Command, _ []string) {
store, err := storage.Open("")
func serve(_ *cobra.Command, opts *config.Options, _ []string) {
store, err := storage.Open(opts, "")
if err != nil {
logger.Fatal("open storage failed: %v", err)
}
defer store.Close()

ctx, cancel := context.WithCancel(context.Background())
pool := pooling.New(ctx, config.Opts.PoolingSize())
pool := pooling.New(ctx, opts)
go pool.Roll()

if config.Opts.EnabledMeilisearch() {
endpoint := config.Opts.WaybackMeiliEndpoint()
indexing := config.Opts.WaybackMeiliIndexing()
apikey := config.Opts.WaybackMeiliApikey()
if opts.EnabledMeilisearch() {
endpoint := opts.WaybackMeiliEndpoint()
indexing := opts.WaybackMeiliIndexing()
apikey := opts.WaybackMeiliApikey()
meili := service.NewMeili(endpoint, apikey, indexing)
if err := meili.Setup(); err != nil {
logger.Error("setup meilisearch failed: %v", err)
Expand All @@ -61,7 +61,7 @@ func serve(_ *cobra.Command, _ []string) {
}

srv := &services{}
_ = srv.run(ctx, store, pool)
_ = srv.run(ctx, store, opts, pool)

if systemd.HasNotifySocket() {
logger.Info("sending readiness notification to Systemd")
Expand All @@ -71,20 +71,20 @@ func serve(_ *cobra.Command, _ []string) {
}
}

go srv.stop(pool, cancel)
go srv.daemon(pool, cancel)
<-ctx.Done()

logger.Info("wayback service stopped.")
}

// nolint:gocyclo
func (srv *services) run(ctx context.Context, store *storage.Storage, pool *pooling.Pool) *services {
func (srv *services) run(ctx context.Context, store *storage.Storage, opts *config.Options, pool *pooling.Pool) *services {
size := len(daemon)
srv.targets = make([]target, 0, size)
for _, s := range daemon {
switch s {
case "irc":
irc := relaychat.New(ctx, store, pool)
irc := relaychat.New(ctx, store, opts, pool)
go func() {
if err := irc.Serve(); err != relaychat.ErrServiceClosed {
logger.Error("%v", err)
Expand All @@ -95,7 +95,7 @@ func (srv *services) run(ctx context.Context, store *storage.Storage, pool *pool
name: s,
})
case "slack":
sl := slack.New(ctx, store, pool)
sl := slack.New(ctx, store, opts, pool)
go func() {
if err := sl.Serve(); err != slack.ErrServiceClosed {
logger.Error("%v", err)
Expand All @@ -106,7 +106,7 @@ func (srv *services) run(ctx context.Context, store *storage.Storage, pool *pool
name: s,
})
case "discord":
d := discord.New(ctx, store, pool)
d := discord.New(ctx, store, opts, pool)
go func() {
if err := d.Serve(); err != discord.ErrServiceClosed {
logger.Error("%v", err)
Expand All @@ -117,7 +117,7 @@ func (srv *services) run(ctx context.Context, store *storage.Storage, pool *pool
name: s,
})
case "mastodon", "mstdn":
m := mastodon.New(ctx, store, pool)
m := mastodon.New(ctx, store, opts, pool)
go func() {
if err := m.Serve(); err != mastodon.ErrServiceClosed {
logger.Error("%v", err)
Expand All @@ -128,7 +128,7 @@ func (srv *services) run(ctx context.Context, store *storage.Storage, pool *pool
name: s,
})
case "telegram":
t := telegram.New(ctx, store, pool)
t := telegram.New(ctx, store, opts, pool)
go func() {
if err := t.Serve(); err != telegram.ErrServiceClosed {
logger.Error("%v", err)
Expand All @@ -139,7 +139,7 @@ func (srv *services) run(ctx context.Context, store *storage.Storage, pool *pool
name: s,
})
case "twitter":
t := twitter.New(ctx, store, pool)
t := twitter.New(ctx, store, opts, pool)
go func() {
if err := t.Serve(); err != twitter.ErrServiceClosed {
logger.Error("%v", err)
Expand All @@ -150,7 +150,7 @@ func (srv *services) run(ctx context.Context, store *storage.Storage, pool *pool
name: s,
})
case "matrix":
m := matrix.New(ctx, store, pool)
m := matrix.New(ctx, store, opts, pool)
go func() {
if err := m.Serve(); err != matrix.ErrServiceClosed {
logger.Error("%v", err)
Expand All @@ -161,7 +161,7 @@ func (srv *services) run(ctx context.Context, store *storage.Storage, pool *pool
name: s,
})
case "web", "httpd":
h := httpd.New(ctx, store, pool)
h := httpd.New(ctx, store, opts, pool)
go func() {
if err := h.Serve(); err != httpd.ErrServiceClosed {
logger.Error("%v", err)
Expand All @@ -179,7 +179,7 @@ func (srv *services) run(ctx context.Context, store *storage.Storage, pool *pool
return srv
}

func (srv *services) stop(pool *pooling.Pool, cancel context.CancelFunc) {
func (srv *services) daemon(pool *pooling.Pool, cancel context.CancelFunc) {
// SIGINT handles Ctrl+C locally.
// SIGTERM handles termination signal from cloud service.
signal.Notify(
Expand Down
7 changes: 4 additions & 3 deletions cmd/wayback/wayback.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/spf13/cobra"
"github.com/wabarc/helper"
"github.com/wabarc/wayback"
"github.com/wabarc/wayback/config"
"github.com/wabarc/wayback/errors"
"github.com/wabarc/wayback/reduxer"
"golang.org/x/sync/errgroup"
Expand All @@ -34,15 +35,15 @@ func assets(art reduxer.Artifact) []reduxer.Asset {
}
}

func archive(cmd *cobra.Command, args []string) {
func archive(cmd *cobra.Command, opts *config.Options, args []string) {
// TODO: clean the auto-created temporary directory.
archiving := func(ctx context.Context, urls []*url.URL) error {
g, ctx := errgroup.WithContext(ctx)
rdx, err := reduxer.Do(ctx, urls...)
rdx, err := reduxer.Do(ctx, opts, urls...)
if err != nil {
return errors.Wrap(err, "reduxer unexpected")
}
cols, err := wayback.Wayback(ctx, rdx, urls...)
cols, err := wayback.Wayback(ctx, rdx, opts, urls...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package config // import "github.com/wabarc/wayback/config"

// Opts holds parsed configuration options.
var Opts *Options
// var Opts *Options

// nolint:stylecheck
const (
Expand Down
7 changes: 4 additions & 3 deletions pooling/pooling.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ func newResource(id int) *resource {

// New a resource pool of the specified capacity
// Resources are created concurrently to save resource initialization time
func New(ctx context.Context, capacity int) *Pool {
func New(ctx context.Context, opts *config.Options) *Pool {
p := new(Pool)
capacity := opts.PoolingSize()
p.resource = make(chan *resource, capacity)
wg := new(sync.WaitGroup)
wg.Add(capacity)
Expand All @@ -78,8 +79,8 @@ func New(ctx context.Context, capacity int) *Pool {
wg.Wait()

p.closed = make(chan bool, 1)
p.timeout = config.Opts.WaybackTimeout()
p.maxRetries = config.Opts.WaybackMaxRetries() + 1
p.timeout = opts.WaybackTimeout()
p.maxRetries = opts.WaybackMaxRetries() + 1
p.multiplier = 0.75
p.context = ctx

Expand Down
Loading

0 comments on commit 0ff8c0a

Please sign in to comment.