mirror of
https://github.com/ZSCNetSupportDept/scheduler.git
synced 2025-10-28 12:35:03 +08:00
添加 config load
This commit is contained in:
8
config.yaml
Normal file
8
config.yaml
Normal file
@@ -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
|
||||
46
config/config.go
Normal file
46
config/config.go
Normal file
@@ -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)
|
||||
|
||||
}
|
||||
16
config/model.go
Normal file
16
config/model.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user