Skip to content

Commit

Permalink
feat: aggregate node config on a single struct
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoura committed Mar 7, 2024
1 parent 32fd18b commit 2c11f31
Show file tree
Hide file tree
Showing 14 changed files with 973 additions and 367 deletions.
28 changes: 20 additions & 8 deletions cmd/cartesi-rollups-node/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,43 @@ import (
"github.com/cartesi/rollups-node/internal/config"
)

func newHttpServiceHandler() http.Handler {
func newHttpServiceHandler(nodeConfig config.NodeConfig) http.Handler {
handler := http.NewServeMux()
handler.Handle("/healthz", http.HandlerFunc(healthcheckHandler))

graphqlProxy, err := newReverseProxy(getPort(portOffsetGraphQLServer))
graphqlProxy, err := newReverseProxy(
nodeConfig.CartesiHttpAddress(),
getPort(nodeConfig.CartesiHttpPort(), portOffsetGraphQLServer),
)
if err != nil {
config.ErrorLogger.Fatal(err)
}
handler.Handle("/graphql", graphqlProxy)

dispatcherProxy, err := newReverseProxy(getPort(portOffsetDispatcher))
dispatcherProxy, err := newReverseProxy(
nodeConfig.CartesiHttpAddress(),
getPort(nodeConfig.CartesiHttpPort(), portOffsetDispatcher),
)
if err != nil {
config.ErrorLogger.Fatal(err)
}
handler.Handle("/metrics", dispatcherProxy)

inspectProxy, err := newReverseProxy(getPort(portOffsetInspectServer))
inspectProxy, err := newReverseProxy(
nodeConfig.CartesiHttpAddress(),
getPort(nodeConfig.CartesiHttpPort(), portOffsetInspectServer),
)
if err != nil {
config.ErrorLogger.Fatal(err)
}
handler.Handle("/inspect", inspectProxy)
handler.Handle("/inspect/", inspectProxy)

if config.GetCartesiFeatureHostMode() {
hostProxy, err := newReverseProxy(getPort(portOffsetHostRunnerRollups))
if nodeConfig.CartesiFeatureHostMode() {
hostProxy, err := newReverseProxy(
nodeConfig.CartesiHttpAddress(),
getPort(nodeConfig.CartesiHttpPort(), portOffsetHostRunnerRollups),
)
if err != nil {
config.ErrorLogger.Fatal(err)
}
Expand All @@ -50,10 +62,10 @@ func healthcheckHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}

func newReverseProxy(port int) (*httputil.ReverseProxy, error) {
func newReverseProxy(httpAddress string, port int) (*httputil.ReverseProxy, error) {
urlStr := fmt.Sprintf(
"http://%v:%v/",
config.GetCartesiHttpAddress(),
httpAddress,
port,
)

Expand Down
34 changes: 20 additions & 14 deletions cmd/cartesi-rollups-node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,41 @@ func main() {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

sunodoValidatorEnabled := config.GetCartesiExperimentalSunodoValidatorEnabled()
nodeConfig := config.NewNodeConfigFromEnv()

nodeConfig.Validate()

config.InitLog(nodeConfig)

sunodoValidatorEnabled := nodeConfig.CartesiExperimentalSunodoValidatorEnabled()
if !sunodoValidatorEnabled {
// add Redis first
s = append(s, newRedis())
s = append(s, newRedis(nodeConfig))
}

// add services without dependencies
s = append(s, newGraphQLServer())
s = append(s, newIndexer())
s = append(s, newStateServer())
s = append(s, newGraphQLServer(nodeConfig))
s = append(s, newIndexer(nodeConfig))
s = append(s, newStateServer(nodeConfig))

// start either the server manager or host runner
if config.GetCartesiFeatureHostMode() {
s = append(s, newHostRunner())
if nodeConfig.CartesiFeatureHostMode() {
s = append(s, newHostRunner(nodeConfig))
} else {
s = append(s, newServerManager())
s = append(s, newServerManager(nodeConfig))
}

// enable claimer if reader mode and sunodo validator mode are disabled
if !config.GetCartesiFeatureDisableClaimer() && !sunodoValidatorEnabled {
s = append(s, newAuthorityClaimer())
if !nodeConfig.CartesiFeatureDisableClaimer() && !sunodoValidatorEnabled {
s = append(s, newAuthorityClaimer(nodeConfig))
}

// add services with dependencies
s = append(s, newAdvanceRunner()) // Depends on the server-manager/host-runner
s = append(s, newDispatcher()) // Depends on the state server
s = append(s, newInspectServer()) // Depends on the server-manager/host-runner
s = append(s, newAdvanceRunner(nodeConfig)) // Depends on the server-manager/host-runner
s = append(s, newDispatcher(nodeConfig)) // Depends on the state server
s = append(s, newInspectServer(nodeConfig)) // Depends on the server-manager/host-runner

s = append(s, newHttpService())
s = append(s, newHttpService(nodeConfig))

ready := make(chan struct{}, 1)
// logs startup time
Expand Down
Loading

0 comments on commit 2c11f31

Please sign in to comment.