添加 config load

This commit is contained in:
2024-10-28 11:54:56 +08:00
parent 1da5d144c9
commit 3b0a0001df
4 changed files with 79 additions and 0 deletions

46
config/config.go Normal file
View 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
View 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
}