Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 6d493cf

Browse files
authored
Merge pull request #42 from hantmac/feature/embed-config
Feature/embed config
2 parents 260814d + 7025e44 commit 6d493cf

File tree

4 files changed

+10
-16
lines changed

4 files changed

+10
-16
lines changed
File renamed without changes.

cmd/fuckdb/cmd/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var serverCmd = &cobra.Command{
3535
}
3636
logrus.Infoln("config init success")
3737

38-
tplFile := viper.GetString("customTmpl")
38+
tplFile := viper.GetString("example.tpl")
3939
if tplFile != "" {
4040
tmpl, err := template.ParseFiles(tplFile)
4141
if err != nil {

config/config.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package config
22

33
import (
4-
"strings"
5-
4+
"bytes"
5+
_ "embed"
66
"github.com/fsnotify/fsnotify"
77
"github.com/sirupsen/logrus"
88
"github.com/spf13/viper"
99
)
1010

11+
//go:embed config.yaml
12+
var configBytes []byte
13+
1114
type Config struct {
1215
Name string
1316
}
@@ -20,20 +23,12 @@ func (c *Config) WatchConfig() {
2023
}
2124

2225
func (c *Config) Init() error {
23-
if c.Name != "" {
24-
viper.SetConfigName(c.Name)
25-
} else {
26-
viper.AddConfigPath("./config")
27-
viper.SetConfigName("config")
28-
}
2926
viper.SetConfigType("yaml")
30-
viper.AutomaticEnv()
31-
viper.SetEnvPrefix("monitor")
32-
replacer := strings.NewReplacer(".", "_")
33-
viper.SetEnvKeyReplacer(replacer)
34-
if err := viper.ReadInConfig(); err != nil {
27+
err := viper.ReadConfig(bytes.NewBufferString(string(configBytes)))
28+
if err != nil {
3529
return err
3630
}
31+
viper.AutomaticEnv()
3732

3833
return nil
3934
}

config/config.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
name: DbToGoStruct # API Server的名字
1+
name: DbToGoStruct # API Server的名字
22
gormlog: true
33
server:
44
host: 127.0.0.1
55
port: 8000
66

7-
customTmpl: example.tpl
87

98
genFilePath: ./

0 commit comments

Comments
 (0)