diff --git a/internal/handlers/name.go b/internal/handlers/name.go new file mode 100644 index 0000000..cd3435f --- /dev/null +++ b/internal/handlers/name.go @@ -0,0 +1,87 @@ +package handlers + +import ( + "blockscout-vc/internal/docker" + "fmt" + + "github.com/spf13/viper" +) + +// MaxCoinLength defines the maximum allowed length for a coin symbol +const MaxNameLength = 30 + +type NameHandler struct { + BaseHandler +} + +func NewNameHandler() *NameHandler { + return &NameHandler{ + BaseHandler: NewBaseHandler(), + } +} + +// Handle processes coin-related changes and updates service configurations +func (h *NameHandler) Handle(record *Record) HandlerResult { + result := HandlerResult{} + + if err := h.validateName(record.Name); err != nil { + result.Error = fmt.Errorf("invalid name: %w", err) + return result + } + + compose, err := h.docker.ReadComposeFile() + if err != nil { + result.Error = fmt.Errorf("failed to read compose file: %w", err) + return result + } + + frontendServiceName := viper.GetString("frontendServiceName") + frontendContainerName := viper.GetString("frontendContainerName") + + updates := map[string]map[string]interface{}{ + frontendServiceName: {}, + } + updates[frontendServiceName]["NEXT_PUBLIC_NETWORK_NAME"] = record.Name + updates[frontendServiceName]["NEXT_PUBLIC_NETWORK_SHORT_NAME"] = record.Name + + // Apply updates to services + for service, env := range updates { + var updated bool + compose, updated, err = h.docker.UpdateServiceEnv(compose, service, env) + if err != nil { + result.Error = fmt.Errorf("failed to update %s service environment: %w", service, err) + return result + } + if updated { + fmt.Printf("Updated %s service environment: %+v\n", service, env) + fmt.Printf("Frontend container name: %s\n", frontendContainerName) + fmt.Printf("Frontend service name: %s\n", frontendServiceName) + result.ContainersToRestart = append(result.ContainersToRestart, docker.Container{ + Name: frontendContainerName, + ServiceName: frontendServiceName, + }) + } + } + + err = h.docker.WriteComposeFile(compose) + if err != nil { + result.Error = fmt.Errorf("failed to write compose file: %w", err) + return result + } + + return result +} + +// validateCoin checks if the coin symbol meets the required criteria +func (h *NameHandler) validateName(name string) error { + if name == "" { + return fmt.Errorf("name cannot be empty") + } + if len(name) == 0 { + return fmt.Errorf("name cannot be empty") + } + if len(name) > MaxCoinLength { + return fmt.Errorf("name length cannot exceed %d characters", MaxCoinLength) + } + return nil +} diff --git a/internal/handlers/types.go b/internal/handlers/types.go index 528a6fb..49b1688 100644 --- a/internal/handlers/types.go +++ b/internal/handlers/types.go @@ -19,7 +19,7 @@ type Record struct { ID int `json:"id"` Name string `json:"name"` Coin string `json:"base_token_symbol"` - ChainID int `json:"chain_id"` + ChainID string `json:"chain_id"` LightLogoURL string `json:"network_logo"` DarkLogoURL string `json:"network_logo_dark"` FaviconURL string `json:"favicon"` diff --git a/internal/subscription/subscription.go b/internal/subscription/subscription.go index 051fa4a..933d8bf 100644 --- a/internal/subscription/subscription.go +++ b/internal/subscription/subscription.go @@ -149,6 +149,7 @@ func (p *PostgresChanges) HandleMessage() error { handlers := []handlers.Handler{ handlers.NewCoinHandler(), handlers.NewImageHandler(), + handlers.NewNameHandler(), } containersToRestart := []docker.Container{}