Add a new method for declare configs.
This commit is contained in:
parent
2a8fbb7b86
commit
dcf78ad6ef
@ -1,5 +1,5 @@
|
|||||||
# 使用1.8的golang作为母镜像
|
# 使用1.8的golang作为母镜像
|
||||||
FROM golang:1.8
|
FROM golang:1.9
|
||||||
|
|
||||||
# 维护者信息
|
# 维护者信息
|
||||||
MAINTAINER eyeblue "eyebluecn@126.com"
|
MAINTAINER eyeblue "eyebluecn@126.com"
|
||||||
|
@ -77,7 +77,7 @@
|
|||||||
//Mysql数据库名称
|
//Mysql数据库名称
|
||||||
"MysqlSchema": "tank",
|
"MysqlSchema": "tank",
|
||||||
//Mysql用户名,建议为蓝眼云盘创建一个用户,不建议使用root
|
//Mysql用户名,建议为蓝眼云盘创建一个用户,不建议使用root
|
||||||
"MysqlUserName": "tank",
|
"MysqlUsername": "tank",
|
||||||
//Mysql密码
|
//Mysql密码
|
||||||
"MysqlPassword": "tank123",
|
"MysqlPassword": "tank123",
|
||||||
//超级管理员用户名,只能是字母和数字
|
//超级管理员用户名,只能是字母和数字
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
"MysqlPort": 3306,
|
"MysqlPort": 3306,
|
||||||
"MysqlHost": "127.0.0.1",
|
"MysqlHost": "127.0.0.1",
|
||||||
"MysqlSchema": "tank",
|
"MysqlSchema": "tank",
|
||||||
"MysqlUserName": "tank",
|
"MysqlUsername": "tank",
|
||||||
"MysqlPassword": "tank123",
|
"MysqlPassword": "tank123",
|
||||||
"AdminUsername": "admin",
|
"AdminUsername": "admin",
|
||||||
"AdminEmail": "admin@tank.eyeblue.cn",
|
"AdminEmail": "admin@tank.eyeblue.cn",
|
||||||
|
@ -21,10 +21,14 @@ services:
|
|||||||
- "6010:6010"
|
- "6010:6010"
|
||||||
restart: always
|
restart: always
|
||||||
environment:
|
environment:
|
||||||
TANK_DB_HOST: db
|
TANK_MYSQL_PORT: 3306
|
||||||
TANK_DB_PORT: 3306
|
TANK_MYSQL_HOST: db
|
||||||
TANK_DB_USER: tank
|
TANK_MYSQL_SCHEMA: tank
|
||||||
TANK_DB_PASSWORD: tank123
|
TANK_MYSQL_USERNAME: tank
|
||||||
|
TANK_MYSQL_PASSWORD: tank123
|
||||||
|
TANK_ADMIN_USERNAME: lish
|
||||||
|
TANK_ADMIN_EMAIL: lish@tank.eyeblue.cn
|
||||||
|
TANK_ADMIN_PASSWORD: 123456
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
db_data:
|
db_data:
|
102
rest/config.go
102
rest/config.go
@ -8,6 +8,8 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -29,7 +31,7 @@ flush privileges;
|
|||||||
*/
|
*/
|
||||||
/*
|
/*
|
||||||
你也可以在运行时的参数中临时修改一些配置项:
|
你也可以在运行时的参数中临时修改一些配置项:
|
||||||
-MysqlHost=127.0.0.1 -MysqlPort=3306 -MysqlSchema=tank -MysqlUserName=tank -MysqlPassword=tank123
|
-MysqlHost=127.0.0.1 -MysqlPort=3306 -MysqlSchema=tank -MysqlUsername=tank -MysqlPassword=tank123
|
||||||
*/
|
*/
|
||||||
var (
|
var (
|
||||||
CONFIG = &Config{
|
CONFIG = &Config{
|
||||||
@ -47,11 +49,11 @@ var (
|
|||||||
//数据库名字
|
//数据库名字
|
||||||
MysqlSchema: "tank",
|
MysqlSchema: "tank",
|
||||||
//用户名
|
//用户名
|
||||||
MysqlUserName: "tank",
|
MysqlUsername: "tank",
|
||||||
//密码
|
//密码
|
||||||
MysqlPassword: "tank123",
|
MysqlPassword: "tank123",
|
||||||
//数据库连接信息。这一项是上面几项组合而得,不可直接配置。
|
//数据库连接信息。这一项是上面几项组合而得,不可直接配置。
|
||||||
MysqlUrl: "%MysqlUserName:%MysqlPassword@tcp(%MysqlHost:%MysqlPort)/%MysqlSchema?charset=utf8&parseTime=True&loc=Local",
|
MysqlUrl: "%MysqlUsername:%MysqlPassword@tcp(%MysqlHost:%MysqlPort)/%MysqlSchema?charset=utf8&parseTime=True&loc=Local",
|
||||||
//超级管理员用户名,只能包含英文和数字
|
//超级管理员用户名,只能包含英文和数字
|
||||||
AdminUsername: "admin",
|
AdminUsername: "admin",
|
||||||
//超级管理员邮箱
|
//超级管理员邮箱
|
||||||
@ -77,7 +79,7 @@ type Config struct {
|
|||||||
//数据库名字
|
//数据库名字
|
||||||
MysqlSchema string
|
MysqlSchema string
|
||||||
//用户名
|
//用户名
|
||||||
MysqlUserName string
|
MysqlUsername string
|
||||||
//密码
|
//密码
|
||||||
MysqlPassword string
|
MysqlPassword string
|
||||||
//数据库连接信息。
|
//数据库连接信息。
|
||||||
@ -98,8 +100,8 @@ func (this *Config) validate() {
|
|||||||
LogPanic("ServerPort 未配置")
|
LogPanic("ServerPort 未配置")
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.MysqlUserName == "" {
|
if this.MysqlUsername == "" {
|
||||||
LogPanic("MysqlUserName 未配置")
|
LogPanic("MysqlUsername 未配置")
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.MysqlPassword == "" {
|
if this.MysqlPassword == "" {
|
||||||
@ -118,7 +120,7 @@ func (this *Config) validate() {
|
|||||||
LogPanic("MysqlSchema 未配置")
|
LogPanic("MysqlSchema 未配置")
|
||||||
}
|
}
|
||||||
|
|
||||||
this.MysqlUrl = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", this.MysqlUserName, this.MysqlPassword, this.MysqlHost, this.MysqlPort, this.MysqlSchema)
|
this.MysqlUrl = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", this.MysqlUsername, this.MysqlPassword, this.MysqlHost, this.MysqlPort, this.MysqlSchema)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,11 +145,8 @@ func init() {
|
|||||||
}, nil)
|
}, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
//从conf/tank.json中获取变量。
|
//第一级. 从配置文件conf/tank.json中读取配置项
|
||||||
|
func LoadConfigFromFile() {
|
||||||
//从flag中或者conf/tank.json中装填变量
|
|
||||||
func PrepareConfigs() {
|
|
||||||
|
|
||||||
//读取配置文件
|
//读取配置文件
|
||||||
filePath := GetConfPath() + "/tank.json"
|
filePath := GetConfPath() + "/tank.json"
|
||||||
content, err := ioutil.ReadFile(filePath)
|
content, err := ioutil.ReadFile(filePath)
|
||||||
@ -161,6 +160,65 @@ func PrepareConfigs() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//第二级. 从环境变量中读取配置项
|
||||||
|
func LoadConfigFromEnvironment() {
|
||||||
|
|
||||||
|
tmpServerPort := os.Getenv("TANK_SERVER_PORT")
|
||||||
|
if tmpServerPort != "" {
|
||||||
|
i, e := strconv.Atoi(tmpServerPort)
|
||||||
|
if e == nil {
|
||||||
|
CONFIG.ServerPort = i
|
||||||
|
} else {
|
||||||
|
LogPanic(fmt.Sprintf("环境变量TANK_SERVER_PORT必须为整数!%v", tmpServerPort))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpMysqlPort := os.Getenv("TANK_MYSQL_PORT")
|
||||||
|
if tmpMysqlPort != "" {
|
||||||
|
i, e := strconv.Atoi(tmpMysqlPort)
|
||||||
|
if e == nil {
|
||||||
|
CONFIG.MysqlPort = i
|
||||||
|
} else {
|
||||||
|
LogPanic(fmt.Sprintf("环境变量TANK_MYSQL_PORT必须为整数!%v", tmpMysqlPort))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpMysqlHost := os.Getenv("TANK_MYSQL_HOST")
|
||||||
|
if tmpMysqlHost != "" {
|
||||||
|
CONFIG.MysqlHost = tmpMysqlHost
|
||||||
|
}
|
||||||
|
tmpMysqlSchema := os.Getenv("TANK_MYSQL_SCHEMA")
|
||||||
|
if tmpMysqlSchema != "" {
|
||||||
|
CONFIG.MysqlSchema = tmpMysqlSchema
|
||||||
|
}
|
||||||
|
tmpMysqlUsername := os.Getenv("TANK_MYSQL_USERNAME")
|
||||||
|
if tmpMysqlUsername != "" {
|
||||||
|
CONFIG.MysqlUsername = tmpMysqlUsername
|
||||||
|
}
|
||||||
|
tmpMysqlPassword := os.Getenv("TANK_MYSQL_PASSWORD")
|
||||||
|
if tmpMysqlPassword != "" {
|
||||||
|
CONFIG.MysqlPassword = tmpMysqlPassword
|
||||||
|
}
|
||||||
|
tmpAdminUsername := os.Getenv("TANK_ADMIN_USERNAME")
|
||||||
|
if tmpAdminUsername != "" {
|
||||||
|
CONFIG.AdminUsername = tmpAdminUsername
|
||||||
|
}
|
||||||
|
tmpAdminEmail := os.Getenv("TANK_ADMIN_EMAIL")
|
||||||
|
if tmpAdminEmail != "" {
|
||||||
|
CONFIG.AdminEmail = tmpAdminEmail
|
||||||
|
}
|
||||||
|
tmpAdminPassword := os.Getenv("TANK_ADMIN_PASSWORD")
|
||||||
|
if tmpAdminPassword != "" {
|
||||||
|
CONFIG.AdminPassword = tmpAdminPassword
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//第三级. 从程序参数中读取配置项
|
||||||
|
func LoadConfigFromArguments() {
|
||||||
|
|
||||||
//从运行时参数中读取,运行时参数具有更高优先级。
|
//从运行时参数中读取,运行时参数具有更高优先级。
|
||||||
//系统端口号
|
//系统端口号
|
||||||
ServerPortPtr := flag.Int("ServerPort", CONFIG.ServerPort, "server port")
|
ServerPortPtr := flag.Int("ServerPort", CONFIG.ServerPort, "server port")
|
||||||
@ -172,7 +230,7 @@ func PrepareConfigs() {
|
|||||||
MysqlPortPtr := flag.Int("MysqlPort", CONFIG.MysqlPort, "mysql port")
|
MysqlPortPtr := flag.Int("MysqlPort", CONFIG.MysqlPort, "mysql port")
|
||||||
MysqlHostPtr := flag.String("MysqlHost", CONFIG.MysqlHost, "mysql host")
|
MysqlHostPtr := flag.String("MysqlHost", CONFIG.MysqlHost, "mysql host")
|
||||||
MysqlSchemaPtr := flag.String("MysqlSchema", CONFIG.MysqlSchema, "mysql schema")
|
MysqlSchemaPtr := flag.String("MysqlSchema", CONFIG.MysqlSchema, "mysql schema")
|
||||||
MysqlUserNamePtr := flag.String("MysqlUserName", CONFIG.MysqlUserName, "mysql username")
|
MysqlUsernamePtr := flag.String("MysqlUsername", CONFIG.MysqlUsername, "mysql username")
|
||||||
MysqlPasswordPtr := flag.String("MysqlPassword", CONFIG.MysqlPassword, "mysql password")
|
MysqlPasswordPtr := flag.String("MysqlPassword", CONFIG.MysqlPassword, "mysql password")
|
||||||
|
|
||||||
//超级管理员信息
|
//超级管理员信息
|
||||||
@ -203,8 +261,8 @@ func PrepareConfigs() {
|
|||||||
CONFIG.MysqlSchema = *MysqlSchemaPtr
|
CONFIG.MysqlSchema = *MysqlSchemaPtr
|
||||||
}
|
}
|
||||||
|
|
||||||
if *MysqlUserNamePtr != CONFIG.MysqlUserName {
|
if *MysqlUsernamePtr != CONFIG.MysqlUsername {
|
||||||
CONFIG.MysqlUserName = *MysqlUserNamePtr
|
CONFIG.MysqlUsername = *MysqlUsernamePtr
|
||||||
}
|
}
|
||||||
|
|
||||||
if *MysqlPasswordPtr != CONFIG.MysqlPassword {
|
if *MysqlPasswordPtr != CONFIG.MysqlPassword {
|
||||||
@ -223,6 +281,20 @@ func PrepareConfigs() {
|
|||||||
CONFIG.AdminPassword = *AdminPasswordPtr
|
CONFIG.AdminPassword = *AdminPasswordPtr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//三种方式指定配置项,后面的策略会覆盖前面的策略。
|
||||||
|
func PrepareConfigs() {
|
||||||
|
|
||||||
|
//第一级. 从配置文件conf/tank.json中读取配置项
|
||||||
|
LoadConfigFromFile()
|
||||||
|
|
||||||
|
//第二级. 从环境变量中读取配置项
|
||||||
|
LoadConfigFromEnvironment()
|
||||||
|
|
||||||
|
//第三级. 从程序参数中读取配置项
|
||||||
|
LoadConfigFromArguments()
|
||||||
|
|
||||||
//验证配置项的正确性
|
//验证配置项的正确性
|
||||||
CONFIG.validate()
|
CONFIG.validate()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user