diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..1e27b2d --- /dev/null +++ b/config.yaml @@ -0,0 +1,8 @@ +# This is a example file for the system +ListenPort: 25005 +File: "member.csv" +DB: + Path: "scheduler.db" + Port: "" # not need if use SQLite + User: "" # not need if use SQLite + Password: "" # not need if use SQLite diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..d9061b8 --- /dev/null +++ b/config/config.go @@ -0,0 +1,46 @@ +package config + +import ( + "fmt" + "github.com/spf13/viper" + "os" +) + +func Load() { + + viper.SetConfigName("config") + viper.SetConfigType("yaml") + viper.AddConfigPath(".") + + if err := viper.ReadInConfig(); err != nil { + fmt.Printf("Error reading config file: %v\n", err) + os.Exit(1) + } + + ListenPort = viper.GetInt("ListenPort") + File = viper.GetString("File") + DB.Path = viper.GetString("DB.Path") + //DB.Port = viper.GetString("DB.Port") + + err := check() + if err != nil { + fmt.Println("check your config!") + os.Exit(1) + } + + debugprint() + +} + +func check() error { + + return nil + +} +func debugprint() { + + fmt.Printf("ListenPort=%v", ListenPort) + fmt.Printf("File=%v", File) + fmt.Printf("database path : %s", DB.Path) + +} diff --git a/config/model.go b/config/model.go new file mode 100644 index 0000000..d574342 --- /dev/null +++ b/config/model.go @@ -0,0 +1,16 @@ +package config + +var ( + ListenPort int + File string + DB database +) + +type database struct { + Path string + //Port string + //User string + //Password string + + // enable if you want use an instance other than SQLite +} diff --git a/main.go b/main.go index 06ab7d0..9ad39d1 100644 --- a/main.go +++ b/main.go @@ -1 +1,10 @@ package main + +import ( + "zsxyww.com/scheduler/config" +) + +func main() { + + config.Load() +}