Files
BountyBoard/cmd/app/main.go
T
etalon 4e01b64bbd feat: ticketing sync workers with idempotent import and orphan flagging (phase 6)
- task domain model + exhaustive §4.4 status machine tests (single source of truth)
- per-customer polling workers reconciled every 30s from the customers
  collection; panic-safe runs; manual sync-now trigger; status provider
  for the admin panel
- §5.3 upsert keyed on (system, key, customerId): refreshes content while
  status=imported, records upstream_changed timeline + notification after
- attachments cached into GridFS via connector-authorized downloads
- orphaned tickets flagged with timeline + consultant notification, never
  deleted; idempotent across polls
- consultant identity from users.extra.ticketingIdentities per system
- notifications store (insert/list/unread-count/mark-read)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 19:03:13 +02:00

94 lines
2.1 KiB
Go

// Command app is the Bounty Board main service.
package main
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"time"
"bountyboard/internal/auth"
"bountyboard/internal/config"
"bountyboard/internal/crypto"
"bountyboard/internal/domain"
"bountyboard/internal/files"
httpx "bountyboard/internal/http"
"bountyboard/internal/metrics"
"bountyboard/internal/store"
syncpkg "bountyboard/internal/sync"
)
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, "fatal:", err)
os.Exit(1)
}
}
func run() error {
if err := config.LoadDotEnv(".env"); err != nil {
return fmt.Errorf("load .env: %w", err)
}
cfg, err := config.Load()
if err != nil {
return err
}
log := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
slog.SetDefault(log)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
st, err := store.Connect(ctx, cfg, log)
if err != nil {
return err
}
defer func() {
closeCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := st.Close(closeCtx); err != nil {
log.Error("close mongo", "err", err)
}
}()
if err := auth.EnsureBootstrapAdmin(ctx, st, cfg, log); err != nil {
return err
}
reg := metrics.NewRegistry()
fileStore := files.NewStore(st.DB, cfg.MaxUploadMB)
srv := httpx.New(cfg, log, reg, st, fileStore)
srv.AddReadinessCheck(httpx.ReadinessCheck{
Name: "mongo",
Required: true,
Probe: st.Ping,
})
syncMgr := syncpkg.NewManager(st, fileStore, log, reg,
func(c *domain.Customer) (syncpkg.Credentials, error) {
if c.Ticketing.CredentialsEnc == "" {
return syncpkg.Credentials{}, nil
}
plain, err := crypto.Open(cfg.CredentialsEncKey, c.Ticketing.CredentialsEnc)
if err != nil {
return nil, err
}
var creds syncpkg.Credentials
if err := json.Unmarshal(plain, &creds); err != nil {
return nil, err
}
return creds, nil
},
srv.Publish)
srv.SetSyncStatusProvider(syncMgr)
srv.SetSyncTrigger(syncMgr.SyncNow)
go syncMgr.Run(ctx)
return srv.Run(ctx)
}