Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle name change #10

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions internal/handlers/name.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion internal/handlers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
1 change: 1 addition & 0 deletions internal/subscription/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (p *PostgresChanges) HandleMessage() error {
handlers := []handlers.Handler{
handlers.NewCoinHandler(),
handlers.NewImageHandler(),
handlers.NewNameHandler(),
}

containersToRestart := []docker.Container{}
Expand Down