|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "blockscout-vc/internal/docker" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/spf13/viper" |
| 8 | +) |
| 9 | + |
| 10 | +// MaxCoinLength defines the maximum allowed length for a coin symbol |
| 11 | +const MaxNameLength = 30 |
| 12 | + |
| 13 | +type NameHandler struct { |
| 14 | + BaseHandler |
| 15 | +} |
| 16 | + |
| 17 | +func NewNameHandler() *NameHandler { |
| 18 | + return &NameHandler{ |
| 19 | + BaseHandler: NewBaseHandler(), |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// Handle processes coin-related changes and updates service configurations |
| 24 | +func (h *NameHandler) Handle(record *Record) HandlerResult { |
| 25 | + result := HandlerResult{} |
| 26 | + |
| 27 | + if err := h.validateName(record.Name); err != nil { |
| 28 | + result.Error = fmt.Errorf("invalid name: %w", err) |
| 29 | + return result |
| 30 | + } |
| 31 | + |
| 32 | + compose, err := h.docker.ReadComposeFile() |
| 33 | + if err != nil { |
| 34 | + result.Error = fmt.Errorf("failed to read compose file: %w", err) |
| 35 | + return result |
| 36 | + } |
| 37 | + |
| 38 | + frontendServiceName := viper.GetString("frontendServiceName") |
| 39 | + frontendContainerName := viper.GetString("frontendContainerName") |
| 40 | + |
| 41 | + updates := map[string]map[string]interface{}{ |
| 42 | + frontendServiceName: {}, |
| 43 | + } |
| 44 | + updates[frontendServiceName]["NEXT_PUBLIC_NETWORK_NAME"] = record.Name |
| 45 | + updates[frontendServiceName]["NEXT_PUBLIC_NETWORK_SHORT_NAME"] = record.Name |
| 46 | + |
| 47 | + // Apply updates to services |
| 48 | + for service, env := range updates { |
| 49 | + var updated bool |
| 50 | + compose, updated, err = h.docker.UpdateServiceEnv(compose, service, env) |
| 51 | + if err != nil { |
| 52 | + result.Error = fmt.Errorf("failed to update %s service environment: %w", service, err) |
| 53 | + return result |
| 54 | + } |
| 55 | + if updated { |
| 56 | + fmt.Printf("Updated %s service environment: %+v\n", service, env) |
| 57 | + fmt.Printf("Frontend container name: %s\n", frontendContainerName) |
| 58 | + fmt.Printf("Frontend service name: %s\n", frontendServiceName) |
| 59 | + result.ContainersToRestart = append(result.ContainersToRestart, docker.Container{ |
| 60 | + Name: frontendContainerName, |
| 61 | + ServiceName: frontendServiceName, |
| 62 | + }) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + err = h.docker.WriteComposeFile(compose) |
| 67 | + if err != nil { |
| 68 | + result.Error = fmt.Errorf("failed to write compose file: %w", err) |
| 69 | + return result |
| 70 | + } |
| 71 | + |
| 72 | + return result |
| 73 | +} |
| 74 | + |
| 75 | +// validateCoin checks if the coin symbol meets the required criteria |
| 76 | +func (h *NameHandler) validateName(name string) error { |
| 77 | + if name == "" { |
| 78 | + return fmt.Errorf("name cannot be empty") |
| 79 | + } |
| 80 | + if len(name) == 0 { |
| 81 | + return fmt.Errorf("name cannot be empty") |
| 82 | + } |
| 83 | + if len(name) > MaxCoinLength { |
| 84 | + return fmt.Errorf("name length cannot exceed %d characters", MaxCoinLength) |
| 85 | + } |
| 86 | + return nil |
| 87 | +} |
0 commit comments