_examples/status/main.go
/*
Package main provides an example that uses sarah.CurrentStatus() to get current go-sarah and its belonging Bot's status via HTTP server.
In this example two bots, slack and nullBot, are registered to go-sarah and become subject to supervise.
See handler.go for Runner.Status() usage.
*/
package main
import (
"context"
"flag"
"fmt"
"github.com/oklahomer/go-kasumi/logger"
"github.com/oklahomer/go-kasumi/worker"
"github.com/oklahomer/go-sarah/v4"
"github.com/oklahomer/go-sarah/v4/slack"
"os"
"os/signal"
"time"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
// Parse and check command line flags
var path = flag.String("config", "", "path to application configuration file.")
flag.Parse()
if *path == "" {
panic("./path/to/executable -config=/path/to/config/app.yml")
}
// Initialize config with given file content
cfg, err := readConfig(*path)
if err != nil {
panic(err)
}
// Setup a bot
nullBot := &nullBot{}
sarah.RegisterBot(nullBot)
// Setup another bot
slackBot, err := setupSlackBot(cfg)
if err != nil {
panic(err)
}
sarah.RegisterBot(slackBot)
// Setup worker
workerReporter := &workerStats{}
reporterOpt := worker.WithReporter(workerReporter)
wkr := worker.Run(ctx, cfg.Worker, reporterOpt)
sarah.RegisterWorker(wkr)
// Setup a Runner to run and supervise above bots
err = sarah.Run(ctx, cfg.Runner)
if err != nil {
panic(err)
}
// Run HTTP server that reports current status
server := newServer(workerReporter)
go server.Run(ctx)
// Wait til signal reception
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
// Stop
logger.Info("Stopping due to signal reception.")
cancel()
time.Sleep(1 * time.Second) // Wait a bit til things finish
}
func setupSlackBot(cfg *config) (sarah.Bot, error) {
storage := sarah.NewUserContextStorage(cfg.ContextCache)
slackAdapter, err := slack.NewAdapter(cfg.Slack)
if err != nil {
return nil, fmt.Errorf("failed to initialize Slack adapter: %w", err)
}
slackBot := sarah.NewBot(slackAdapter, sarah.BotWithStorage(storage))
return slackBot, nil
}