增加从环境变量获取配置&修复修改密码失败的bug&增加退出登录&修复新增用户无法登录的bug
This commit is contained in:
@ -1,16 +1,16 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
DB string
|
||||
Server *Server
|
||||
Mysql *Mysql
|
||||
Sqlite *Sqlite
|
||||
}
|
||||
|
||||
type Mysql struct {
|
||||
@ -21,36 +21,31 @@ type Mysql struct {
|
||||
Database string
|
||||
}
|
||||
|
||||
type Sqlite struct {
|
||||
File string
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
Addr string
|
||||
}
|
||||
|
||||
func SetupConfig() *Config {
|
||||
func SetupConfig() (*Config, error) {
|
||||
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigType("yml")
|
||||
viper.AddConfigPath("/etc/next-terminal/")
|
||||
viper.AddConfigPath("$HOME/.next-terminal")
|
||||
viper.AddConfigPath(".")
|
||||
viper.AutomaticEnv()
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
|
||||
//pflag.String("mysql.hostname", "127.0.0.1", "mysql hostname")
|
||||
//pflag.Int("mysql.port", 3306, "mysql port")
|
||||
//pflag.String("mysql.username", "mysql", "mysql username")
|
||||
//pflag.String("mysql.password", "mysql", "mysql password")
|
||||
//pflag.String("mysql.database", "next_terminal", "mysql database")
|
||||
//pflag.String("server.addr", "0.0.0.0:8088", "server listen addr")
|
||||
|
||||
pflag.Parse()
|
||||
_ = viper.BindPFlags(pflag.CommandLine)
|
||||
|
||||
err := viper.ReadInConfig()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var config = &Config{
|
||||
DB: viper.GetString("db"),
|
||||
Mysql: &Mysql{
|
||||
Hostname: viper.GetString("mysql.hostname"),
|
||||
Port: viper.GetInt("mysql.port"),
|
||||
@ -58,10 +53,13 @@ func SetupConfig() *Config {
|
||||
Password: viper.GetString("mysql.password"),
|
||||
Database: viper.GetString("mysql.database"),
|
||||
},
|
||||
Sqlite: &Sqlite{
|
||||
File: viper.GetString("sqlite.file"),
|
||||
},
|
||||
Server: &Server{
|
||||
Addr: viper.GetString("server.addr"),
|
||||
},
|
||||
}
|
||||
|
||||
return config
|
||||
return config, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user