check / check (push) Successful in 1m13s
The bot now serves an HTTP API on `PORT` (default 8080) beside the chat client, whose WebSocket stays on 127.0.0.1 inside the container. Every request needs `Authorization: Bearer` with the credential from the file named by `API_TOKEN_FILE`, compared in constant time; with no credential configured every request is refused, `OPTIONS *` included. `GET /api/v1/chats` lists the bot's chats. Responses carry the security headers from the repository policies; bodies, requests and the server are time- and size-bounded. The chat client stops only after the API has finished its requests. Disclosures: `contact_deleted` is an extra field; 404 and 405 answer in JSON; requests net/http cannot parse are refused by net/http without the security headers; three gosec findings are suppressed as false positives. Model: opus-5-5
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"runtime"
|
|
"syscall"
|
|
|
|
"github.com/spf13/cobra"
|
|
"sneak.berlin/go/simplexcalc/internal/bot"
|
|
"sneak.berlin/go/simplexcalc/internal/config"
|
|
"sneak.berlin/go/simplexcalc/internal/logger"
|
|
)
|
|
|
|
func runCmd(version string) *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "run",
|
|
Short: "run the bot",
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
return run(cmd.Context(), version)
|
|
},
|
|
}
|
|
}
|
|
|
|
// run reads the configuration and runs the bot until SIGINT or SIGTERM,
|
|
// or until it fails. A failure is the process's exit status, so a
|
|
// supervisor restarts it.
|
|
func run(ctx context.Context, version string) error {
|
|
cfg, err := config.New()
|
|
if err != nil {
|
|
return fmt.Errorf("reading configuration: %w", err)
|
|
}
|
|
|
|
log := logger.New(os.Stdout, cfg.Debug)
|
|
log.Info("starting",
|
|
"appname", appname,
|
|
"version", version,
|
|
"arch", runtime.GOARCH,
|
|
"data_dir", cfg.DataDir,
|
|
)
|
|
|
|
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
err = bot.Run(ctx, log, cfg, bot.ChatPort)
|
|
if err != nil {
|
|
log.Error("stopped", "error", err)
|
|
|
|
return err
|
|
}
|
|
|
|
log.Info("stopped")
|
|
|
|
return nil
|
|
}
|