Skip to content

Commit

Permalink
robot: serve api inside the errgroup
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrtronium committed Nov 20, 2024
1 parent 34a35b6 commit 86cd18f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
8 changes: 1 addition & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"runtime"
Expand Down Expand Up @@ -135,12 +134,7 @@ func cliRun(ctx context.Context, cmd *cli.Command) error {
}
}

if cfg.HTTP.Listen != "" {
// TODO(zeph): this should be in the errgroup inside Run
go api(ctx, cfg.HTTP.Listen, new(http.ServeMux), robo.Metrics.Collectors())
}

return robo.Run(ctx)
return robo.Run(ctx, cfg.HTTP.Listen)
}

func cliSpeak(ctx context.Context, cmd *cli.Command) error {
Expand Down
6 changes: 5 additions & 1 deletion robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"log/slog"
"net/http"
"strings"
"time"

Expand Down Expand Up @@ -82,12 +83,15 @@ func New(usersKey []byte, poolSize int) *Robot {
}
}

func (robo *Robot) Run(ctx context.Context) error {
func (robo *Robot) Run(ctx context.Context, listen string) error {
group, ctx := errgroup.WithContext(ctx)
// TODO(zeph): stdin?
if robo.tmi != nil {
group.Go(func() error { return robo.runTwitch(ctx, group) })
}
if listen != "" {
group.Go(func() error { return api(ctx, listen, new(http.ServeMux), robo.Metrics.Collectors()) })
}
err := group.Wait()
if err == context.Canceled {
// If the first error is context canceled, then we are shutting down
Expand Down

0 comments on commit 86cd18f

Please sign in to comment.