Skip to content

Commit 3a79907

Browse files
authored
Merge pull request #10 from aurora-is-near/name_handler
Handle name change
2 parents 60b1c58 + 6f9cd21 commit 3a79907

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

internal/handlers/name.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}

internal/handlers/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Record struct {
1919
ID int `json:"id"`
2020
Name string `json:"name"`
2121
Coin string `json:"base_token_symbol"`
22-
ChainID int `json:"chain_id"`
22+
ChainID string `json:"chain_id"`
2323
LightLogoURL string `json:"network_logo"`
2424
DarkLogoURL string `json:"network_logo_dark"`
2525
FaviconURL string `json:"favicon"`

internal/subscription/subscription.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ func (p *PostgresChanges) HandleMessage() error {
149149
handlers := []handlers.Handler{
150150
handlers.NewCoinHandler(),
151151
handlers.NewImageHandler(),
152+
handlers.NewNameHandler(),
152153
}
153154

154155
containersToRestart := []docker.Container{}

0 commit comments

Comments
 (0)