Skip to content

Commit

Permalink
feat: initial configuration via environment (#136)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
  • Loading branch information
wolf31o2 authored Sep 23, 2024
1 parent 2e4f535 commit f6abc86
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 21 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/blinklabs-io/ouroboros-mock v0.3.3
github.com/dgraph-io/badger/v4 v4.3.0
github.com/glebarez/sqlite v1.11.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/prometheus/client_golang v1.20.4
github.com/spf13/cobra v1.8.1
go.opentelemetry.io/otel v1.30.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
Expand Down
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"
)

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 f6abc86

Please sign in to comment.