-
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(config): add config module (#21)
- Loading branch information
Showing
15 changed files
with
160 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
PACTUS_NODES=bootstrap1.pactus.org:50051,bootstrap2.pactus.org:50051,bootstrap3.pactus.org:50051,bootstrap4.pactus.org:50051,151.115.110.114:50051,188.121.116.247:50051", | ||
PACTUS_BRIDGE_ADDRESS= | ||
# Enviroment | ||
ENVIROMENT=(dev | prod) | ||
|
||
Fee= | ||
DB_PATH= | ||
# Pactus | ||
|
||
TELEGRAM_BOT_TOKEN= | ||
TELEGRAM_CHAT_ID= | ||
# Wrapto Lock Address | ||
PACTUS_WALLET_ADDRESS= | ||
PACTUS_WALLET_PASSWORD= | ||
PACTUS_WALLET_PATH= | ||
PACTUS_RPC= | ||
|
||
# Polygon | ||
POLYGON_PRIVATE_KEY= | ||
POLYGON_CONTRACT_ADDRESS= | ||
POLYGON_RPC= | ||
POLYGON_PRIVATE_KEY= | ||
|
||
WALLET_PATH= | ||
WALLET_ADDRESS= | ||
WALLET_PASSWORD= | ||
# Database | ||
DATABASE_PATH= |
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,71 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/joho/godotenv" | ||
) | ||
|
||
type Config struct { | ||
Environment string | ||
Pactus PactusConfig | ||
Polygon PolygonConfig | ||
Database DatabaseConfig | ||
} | ||
|
||
type PactusConfig struct { | ||
WalletPath string | ||
WalletPass string | ||
LockAddr string | ||
RPCNode string | ||
} | ||
|
||
type PolygonConfig struct { | ||
PrivateKey string | ||
ContractAddr string | ||
RPCNode string | ||
} | ||
|
||
type DatabaseConfig struct { | ||
Path string | ||
} | ||
|
||
func LoadConfig() (*Config, error) { | ||
if err := godotenv.Load(); err != nil { | ||
return nil, err | ||
} | ||
|
||
cfg := &Config{ | ||
Environment: os.Getenv("NETWORK"), | ||
Pactus: PactusConfig{ | ||
WalletPath: os.Getenv("PACTUS_WALLET_PATH"), | ||
WalletPass: os.Getenv("PACTUS_WALLET_PASSWORD"), | ||
LockAddr: os.Getenv("PACTUS_WALLET_ADDRESS"), | ||
RPCNode: os.Getenv("PACTUS_RPC"), | ||
}, | ||
Polygon: PolygonConfig{ | ||
PrivateKey: os.Getenv("POLYGON_PRIVATE_KEY"), | ||
ContractAddr: os.Getenv("POLYGON_CONTRACT_ADDRESS"), | ||
RPCNode: os.Getenv("POLYGON_RPC"), | ||
}, | ||
Database: DatabaseConfig{ | ||
os.Getenv("DATABASE_PATH"), | ||
}, | ||
} | ||
|
||
if err := cfg.basicCheck(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return cfg, nil | ||
} | ||
|
||
func (c *Config) basicCheck() error { | ||
if !(c.Environment == "dev") || !(c.Environment == "prod") { | ||
return InvalidEnvironmentError{ | ||
Environment: c.Environment, | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,11 @@ | ||
package config | ||
|
||
import "fmt" | ||
|
||
type InvalidEnvironmentError struct { | ||
Environment string | ||
} | ||
|
||
func (e InvalidEnvironmentError) Error() string { | ||
return fmt.Sprintf("environment can be `dev` or `prod` not: %s", e.Environment) | ||
} |
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
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
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
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