Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use default toml if config file without ext #330

Merged
merged 2 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/constant/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ const (
EgoGovernorEnableConfig = "EGO_GOVERNOR_ENABLE_CONFIG"
// EgoLogEnableAddCaller when set to true, your log will show caller, default value is false
EgoLogEnableAddCaller = "EGO_LOG_ENABLE_ADD_CALLER"
// EgoDefaultConfigExt defines default config file extension, support ".toml",".yaml",".json",
// EgoDefaultConfigExt effective only the configuration file path without extension name
EgoDefaultConfigExt = "EGO_DEFAULT_CONFIG_EXT"
)
5 changes: 5 additions & 0 deletions core/econf/file/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package file

import (
"github.com/gotomicro/ego/core/constant"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -49,6 +50,10 @@ func (fp *fileDataSource) Parse(path string, watch bool) econf.ConfigType {

func extParser(configAddr string) econf.ConfigType {
ext := filepath.Ext(configAddr)
if ext == "" { // 如果配置文件没有扩展名,尝试从环境变量获取配置文件的扩展名
ext = os.Getenv(constant.EgoDefaultConfigExt)
}

switch ext {
case ".json":
return econf.ConfigTypeJSON
Expand Down