-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from orvice/feat/config-with-viper
config with viper
- Loading branch information
Showing
7 changed files
with
136 additions
and
44 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,22 +1,39 @@ | ||
package config | ||
|
||
import "github.com/orvice/utils/env" | ||
import ( | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type Config struct { | ||
DNSMode string `mapstructure:"DNS_MODE"` | ||
Domain string `mapstructure:"DOMAIN"` | ||
TelegramChatID int64 `mapstructure:"TELEGRAM_CHATID"` | ||
TelegramToken string `mapstructure:"TELEGRAM_TOKEN"` | ||
} | ||
|
||
var ( | ||
DOMAIN string | ||
config Config | ||
) | ||
|
||
var ( | ||
DNSMode string | ||
func Init() (err error) { | ||
err = LoadConfig(".") | ||
if err != nil { | ||
return | ||
} | ||
return | ||
} | ||
|
||
TelegramToken string | ||
TelegramChatID int64 | ||
) | ||
func GetConfig() Config { | ||
return config | ||
} | ||
|
||
func GetConfigFromEnv() { | ||
DNSMode = env.Get("DNS_MODE", "cf") | ||
DOMAIN = env.Get("DOMAIN") | ||
func LoadConfig(path string) (err error) { | ||
viper.AddConfigPath(path) | ||
viper.SetConfigName("app") | ||
|
||
TelegramChatID = int64(env.GetInt("TELEGRAM_CHATID")) | ||
TelegramToken = env.Get("TELEGRAM_TOKEN") | ||
viper.SetConfigType("env") | ||
viper.AutomaticEnv() | ||
// viper.ReadInConfig() | ||
err = viper.Unmarshal(&config) | ||
return | ||
} |
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,19 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
os.Setenv("DOMAIN", "example.com") | ||
os.Setenv("TELEGRAM_CHATID", "1234567890") | ||
err := LoadConfig(".") | ||
assert.Nil(t, err) | ||
|
||
t.Log(GetConfig()) | ||
assert.Equal(t, "example.com", GetConfig().Domain) | ||
assert.Equal(t, int64(1234567890), GetConfig().TelegramChatID) | ||
} |
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