Parse config settings
This commit is contained in:
parent
d919cd87e7
commit
98faf11088
|
@ -0,0 +1,38 @@
|
|||
package settings
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
)
|
||||
|
||||
// MongoDB contains configuration settings for a MongoDB database.
|
||||
type MongoDB struct {
|
||||
MongodbUri string `split_words:"true" required:"true"`
|
||||
MongodbDatabase string `split_words:"true" required:"true"`
|
||||
}
|
||||
|
||||
// Logger contains configuration settings for a logger.
|
||||
type Logger struct {
|
||||
LogLevel string `split_words:"true" default:"INFO"`
|
||||
}
|
||||
|
||||
// LoadFromEnv loads the configuration settings from environment variables.
|
||||
//
|
||||
// If there is a .env file in the current directory, it will be used to
|
||||
// populate the environment variables.
|
||||
func LoadFromEnv[T any]() (*T, error) {
|
||||
|
||||
// Load .env file (if it exists)
|
||||
_ = godotenv.Load()
|
||||
|
||||
// Load environment variables into a struct
|
||||
var settings T
|
||||
err := envconfig.Process("", &settings)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read config from environment: %w", err)
|
||||
}
|
||||
|
||||
return &settings, nil
|
||||
}
|
|
@ -1,5 +1,32 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/wormhole-foundation/wormhole-explorer/common/logger"
|
||||
"github.com/wormhole-foundation/wormhole-explorer/common/settings"
|
||||
"github.com/wormhole-foundation/wormhole-explorer/core-contract-watcher/config"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// Load config
|
||||
cfg, err := settings.LoadFromEnv[config.ServiceSettings]()
|
||||
if err != nil {
|
||||
log.Fatal("Error loading config: ", err)
|
||||
}
|
||||
|
||||
// Build rootLogger
|
||||
rootLogger := logger.New("wormhole-explorer-core-contract-watcher", logger.WithLevel(cfg.LogLevel))
|
||||
|
||||
// Create top-level context
|
||||
_, rootCtxCancel := context.WithCancel(context.Background())
|
||||
|
||||
rootLogger.Info("starting service...")
|
||||
|
||||
// Graceful shutdown
|
||||
rootLogger.Info("cancelling root context...")
|
||||
rootCtxCancel()
|
||||
rootLogger.Info("terminated")
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"github.com/wormhole-foundation/wormhole-explorer/common/settings"
|
||||
)
|
||||
|
||||
// ServiceSettings models the configuration settings for the core-contract-watcher service.
|
||||
type ServiceSettings struct {
|
||||
settings.Logger
|
||||
settings.MongoDB
|
||||
}
|
Loading…
Reference in New Issue