feat: enhance Safew watcher initialization and token management
- Added functionality to list and start Safew tokens in the main server function, ensuring proper initialization of the Safew watcher. - Introduced `ensureSafewWatcher` method in the ChannelHandler to manage Safew tokens during channel creation and updates. - Implemented `StartTokens` method in the Watcher to handle multiple tokens efficiently. - Enhanced error handling and logging for Safew watcher operations to improve observability.
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"aiaa-notification-service/internal/model"
|
||||
)
|
||||
@@ -94,3 +95,38 @@ func (s *Store) DeleteChannel(ctx context.Context, id int) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) ListSafewTokens(ctx context.Context) ([]string, error) {
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT config FROM notification_channel WHERE type = 'safew'`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list safew tokens: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
seen := map[string]struct{}{}
|
||||
var tokens []string
|
||||
for rows.Next() {
|
||||
var configBytes []byte
|
||||
if err := rows.Scan(&configBytes); err != nil {
|
||||
return nil, fmt.Errorf("scan safew config: %w", err)
|
||||
}
|
||||
var cfg struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
if err := json.Unmarshal(configBytes, &cfg); err != nil {
|
||||
continue
|
||||
}
|
||||
tok := strings.TrimSpace(cfg.Token)
|
||||
if tok == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[tok]; ok {
|
||||
continue
|
||||
}
|
||||
seen[tok] = struct{}{}
|
||||
tokens = append(tokens, tok)
|
||||
}
|
||||
if tokens == nil {
|
||||
tokens = []string{}
|
||||
}
|
||||
return tokens, rows.Err()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user