-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial configuration via environment (#136)
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
- Loading branch information
Showing
4 changed files
with
93 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters