Skip to content

Commit

Permalink
feat: initial configuration via environment
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
  • Loading branch information
wolf31o2 committed Sep 22, 2024
1 parent af189e9 commit d76ca96
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 21 deletions.
83 changes: 83 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package config

import (
"fmt"

"github.com/blinklabs-io/node/topology"

"github.com/kelseyhightower/envconfig"

Check failure on line 22 in internal/config/config.go

View workflow job for this annotation

GitHub Actions / go-test (1.22.x, ubuntu-latest)

no required module provides package github.com/kelseyhightower/envconfig; to add it:

Check failure on line 22 in internal/config/config.go

View workflow job for this annotation

GitHub Actions / go-test (1.23.x, ubuntu-latest)

no required module provides package github.com/kelseyhightower/envconfig; to add it:
)

type Config struct {
BindAddr string `split_words:"true"`
Network string
Port uint
Topology string
}

var globalConfig = &Config{
BindAddr: "0.0.0.0",
Network: "preview",
Port: 3001,
Topology: "",
}

func LoadConfig() (*Config, error) {
err := envconfig.Process("cardano", globalConfig)
if err != nil {
return nil, fmt.Errorf("error processing environment: %+v", err)
}
_, err = LoadTopologyConfig()
if err != nil {
return nil, fmt.Errorf("error loading topology: %+v", err)
}
return globalConfig, nil
}

func GetConfig() *Config {
return globalConfig
}

var globalTopologyConfig = &topology.TopologyConfig{
PublicRoots: []topology.TopologyConfigP2PPublicRoot{
{
AccessPoints: []topology.TopologyConfigP2PAccessPoint{
{
Address: "preview-node.play.dev.cardano.org",
Port: 3001,
},
},
},
},
}

func LoadTopologyConfig() (*topology.TopologyConfig, error) {
if globalConfig.Topology == "" {
return globalTopologyConfig, nil
}
tc, err := topology.NewTopologyConfigFromFile(globalConfig.Topology)
if err != nil {
return nil, fmt.Errorf("failed to load topology file: %+v", err)
}
// update globalTopologyConfig
globalTopologyConfig = tc
return globalTopologyConfig, nil
}

func GetTopologyConfig() *topology.TopologyConfig{
return globalTopologyConfig
}
28 changes: 7 additions & 21 deletions internal/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@ import (
"os"

"github.com/blinklabs-io/node"
"github.com/blinklabs-io/node/topology"
"github.com/blinklabs-io/node/internal/config"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func Run(logger *slog.Logger) error {
// TODO: make this configurable
l, err := net.Listen("tcp", ":3001")
cfg := config.GetConfig()
logger.Info(fmt.Sprintf("loaded config: %+v", cfg))
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.Port))
if err != nil {
return err
}
logger.Info("listening for ouroboros node-to-node connections on :3001")
logger.Info(fmt.Sprintf("listening for ouroboros node-to-node connections on %s", fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.Port)))
// Metrics listener
http.Handle("/metrics", promhttp.Handler())
logger.Info("listening for prometheus metrics connections on :12798")
Expand All @@ -51,8 +52,7 @@ func Run(logger *slog.Logger) error {
node.WithLogger(logger),
// TODO: uncomment and make this configurable
//node.WithDataDir(".data"),
// TODO: make this configurable
node.WithNetwork("preview"),
node.WithNetwork(cfg.Network),
node.WithListeners(
node.ListenerConfig{
Listener: l,
Expand All @@ -62,21 +62,7 @@ func Run(logger *slog.Logger) error {
node.WithPrometheusRegistry(prometheus.DefaultRegisterer),
// TODO: make this configurable
//node.WithTracing(true),
// TODO: replace with parsing topology file
node.WithTopologyConfig(
&topology.TopologyConfig{
PublicRoots: []topology.TopologyConfigP2PPublicRoot{
{
AccessPoints: []topology.TopologyConfigP2PAccessPoint{
{
Address: "preview-node.play.dev.cardano.org",
Port: 3001,
},
},
},
},
},
),
node.WithTopologyConfig(config.GetTopologyConfig()),
),
)
if err != nil {
Expand Down

0 comments on commit d76ca96

Please sign in to comment.