Skip to content

Commit

Permalink
Merge pull request #42 from hantmac/feature/embed-config
Browse files Browse the repository at this point in the history
Feature/embed config
  • Loading branch information
hantmac committed May 31, 2021
2 parents 260814d + 7025e44 commit 6d493cf
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 16 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/fuckdb/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var serverCmd = &cobra.Command{
}
logrus.Infoln("config init success")

tplFile := viper.GetString("customTmpl")
tplFile := viper.GetString("example.tpl")
if tplFile != "" {
tmpl, err := template.ParseFiles(tplFile)
if err != nil {
Expand Down
21 changes: 8 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package config

import (
"strings"

"bytes"
_ "embed"
"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

//go:embed config.yaml
var configBytes []byte

type Config struct {
Name string
}
Expand All @@ -20,20 +23,12 @@ func (c *Config) WatchConfig() {
}

func (c *Config) Init() error {
if c.Name != "" {
viper.SetConfigName(c.Name)
} else {
viper.AddConfigPath("./config")
viper.SetConfigName("config")
}
viper.SetConfigType("yaml")
viper.AutomaticEnv()
viper.SetEnvPrefix("monitor")
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
if err := viper.ReadInConfig(); err != nil {
err := viper.ReadConfig(bytes.NewBufferString(string(configBytes)))
if err != nil {
return err
}
viper.AutomaticEnv()

return nil
}
Expand Down
3 changes: 1 addition & 2 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
name: DbToGoStruct # API Server的名字
name: DbToGoStruct # API Server的名字
gormlog: true
server:
host: 127.0.0.1
port: 8000

customTmpl: example.tpl

genFilePath: ./

0 comments on commit 6d493cf

Please sign in to comment.