tank/code/support/tank_config.go
2019-04-28 01:25:31 +08:00

221 lines
5.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package support
import (
"github.com/eyebluecn/tank/code/core"
"github.com/eyebluecn/tank/code/tool/util"
"github.com/json-iterator/go"
"io/ioutil"
"os"
"time"
"unsafe"
)
/*
如果你需要在本地127.0.0.1创建默认的数据库和账号,使用以下语句。
create database tank;
grant all privileges on tank.* to tank identified by 'tank123';
flush privileges;
*/
//依赖外部定义的变量。
type TankConfig struct {
//默认监听端口号
ServerPort int
//网站是否已经完成安装
Installed bool
//上传的文件路径,要求不以/结尾。如果没有指定默认在根目录下的matter文件夹中。eg: /var/www/matter
MatterPath string
//数据库连接信息。
MysqlUrl string
//配置文件中的项
Item *ConfigItem
}
//和tank.json文件中的键值一一对应。
type ConfigItem struct {
//默认监听端口号
ServerPort int
//上传的文件路径,要求不以/结尾。如果没有指定默认在根目录下的matter文件夹中。eg: /var/www/matter
MatterPath string
//mysql相关配置。
//数据库端口
MysqlPort int
//数据库Host
MysqlHost string
//数据库名字
MysqlSchema string
//用户名
MysqlUsername string
//密码
MysqlPassword string
}
//验证配置文件的正确性。
func (this *ConfigItem) validate() bool {
if this.ServerPort == 0 {
core.LOGGER.Error("ServerPort 未配置")
return false
}
if this.MysqlUsername == "" {
core.LOGGER.Error("MysqlUsername 未配置")
return false
}
if this.MysqlPassword == "" {
core.LOGGER.Error("MysqlPassword 未配置")
return false
}
if this.MysqlHost == "" {
core.LOGGER.Error("MysqlHost 未配置")
return false
}
if this.MysqlPort == 0 {
core.LOGGER.Error("MysqlPort 未配置")
return false
}
if this.MysqlSchema == "" {
core.LOGGER.Error("MysqlSchema 未配置")
return false
}
return true
}
//验证配置文件是否完好
func (this *TankConfig) Init() {
//JSON初始化
jsoniter.RegisterTypeDecoderFunc("time.Time", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
//如果使用time.UTC那么时间会相差8小时
t, err := time.ParseInLocation("2006-01-02 15:04:05", iter.ReadString(), time.Local)
if err != nil {
iter.Error = err
return
}
*((*time.Time)(ptr)) = t
})
jsoniter.RegisterTypeEncoderFunc("time.Time", func(ptr unsafe.Pointer, stream *jsoniter.Stream) {
t := *((*time.Time)(ptr))
//如果使用time.UTC那么时间会相差8小时
stream.WriteString(t.Local().Format("2006-01-02 15:04:05"))
}, nil)
//默认从6010端口启动
this.ServerPort = 6010
this.ReadFromConfigFile()
}
//系统如果安装好了就调用这个方法。
func (this *TankConfig) ReadFromConfigFile() {
//读取配置文件
filePath := util.GetConfPath() + "/tank.json"
content, err := ioutil.ReadFile(filePath)
if err != nil {
core.LOGGER.Warn("无法找到配置文件:%s 即将进入安装过程!", filePath)
this.Installed = false
} else {
this.Item = &ConfigItem{}
core.LOGGER.Warn("读取配置文件:%s", filePath)
err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(content, this.Item)
if err != nil {
core.LOGGER.Error("配置文件格式错误! 即将进入安装过程!")
this.Installed = false
return
}
//验证项是否齐全
itemValidate := this.Item.validate()
if !itemValidate {
core.LOGGER.Error("配置文件信息不齐全! 即将进入安装过程!")
this.Installed = false
return
}
//使用配置项中的文件路径
if this.Item.MatterPath == "" {
this.MatterPath = util.GetHomePath() + "/matter"
} else {
this.MatterPath = this.Item.MatterPath
}
util.MakeDirAll(this.MatterPath)
//使用配置项中的端口
if this.Item.ServerPort != 0 {
this.ServerPort = this.Item.ServerPort
}
this.MysqlUrl = util.GetMysqlUrl(this.Item.MysqlPort, this.Item.MysqlHost, this.Item.MysqlSchema, this.Item.MysqlUsername, this.Item.MysqlPassword)
this.Installed = true
core.LOGGER.Info("使用配置文件:%s", filePath)
core.LOGGER.Info("上传文件存放路径:%s", this.MatterPath)
}
}
//是否已经安装
func (this *TankConfig) IsInstalled() bool {
return this.Installed
}
//启动端口
func (this *TankConfig) GetServerPort() int {
return this.ServerPort
}
//获取mysql链接
func (this *TankConfig) GetMysqlUrl() string {
return this.MysqlUrl
}
//文件存放路径
func (this *TankConfig) GetMatterPath() string {
return this.MatterPath
}
//完成安装过程,主要是要将配置写入到文件中
func (this *TankConfig) FinishInstall(mysqlPort int, mysqlHost string, mysqlSchema string, mysqlUsername string, mysqlPassword string) {
var configItem = &ConfigItem{
//默认监听端口号
ServerPort: core.CONFIG.GetServerPort(),
//上传的文件路径,要求不以/结尾。如果没有指定默认在根目录下的matter文件夹中。eg: /var/www/matter
MatterPath: core.CONFIG.GetMatterPath(),
//mysql相关配置。
//数据库端口
MysqlPort: mysqlPort,
//数据库Host
MysqlHost: mysqlHost,
//数据库名字
MysqlSchema: mysqlSchema,
//用户名
MysqlUsername: mysqlUsername,
//密码
MysqlPassword: mysqlPassword,
}
//用json的方式输出返回值。为了让格式更好看。
jsonStr, _ := jsoniter.ConfigCompatibleWithStandardLibrary.MarshalIndent(configItem, "", " ")
//写入到配置文件中不能使用os.O_APPEND 否则会追加)
filePath := util.GetConfPath() + "/tank.json"
f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0777)
util.PanicError(err)
_, err = f.Write(jsonStr)
util.PanicError(err)
err = f.Close()
util.PanicError(err)
this.ReadFromConfigFile()
}