Rename rest to code.

This commit is contained in:
zicla
2019-04-26 02:59:35 +08:00
parent 46256439da
commit ce84a9c9f0
67 changed files with 138 additions and 124 deletions

184
code/config/config.go Normal file
View File

@ -0,0 +1,184 @@
package config
import (
"github.com/json-iterator/go"
"io/ioutil"
"tank/code/logger"
"tank/code/tool"
"time"
"unsafe"
)
const (
//用户身份的cookie字段名
COOKIE_AUTH_KEY = "_ak"
//数据库表前缀 tank200表示当前应用版本是tank:2.0.x版数据库结构发生变化必然是中型升级
TABLE_PREFIX = "tank20_"
//当前版本
VERSION = "2.0.0"
)
/*
如果你需要在本地127.0.0.1创建默认的数据库和账号,使用以下语句。
create database tank;
grant all privileges on tank.* to tank identified by 'tank123';
flush privileges;
*/
var CONFIG = &Config{}
//依赖外部定义的变量。
type Config 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 {
logger.LOGGER.Error("ServerPort 未配置")
return false
} else {
//只要配置文件中有配置端口,就使用。
CONFIG.ServerPort = this.ServerPort
}
if this.MysqlUsername == "" {
logger.LOGGER.Error("MysqlUsername 未配置")
return false
}
if this.MysqlPassword == "" {
logger.LOGGER.Error("MysqlPassword 未配置")
return false
}
if this.MysqlHost == "" {
logger.LOGGER.Error("MysqlHost 未配置")
return false
}
if this.MysqlPort == 0 {
logger.LOGGER.Error("MysqlPort 未配置")
return false
}
if this.MysqlSchema == "" {
logger.LOGGER.Error("MysqlSchema 未配置")
return false
}
return true
}
//验证配置文件是否完好
func (this *Config) 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 *Config) ReadFromConfigFile() {
//读取配置文件
filePath := tool.GetConfPath() + "/tank.json"
content, err := ioutil.ReadFile(filePath)
if err != nil {
logger.LOGGER.Warn("无法找到配置文件:%s 即将进入安装过程!", filePath)
this.Installed = false
} else {
this.Item = &ConfigItem{}
logger.LOGGER.Warn("读取配置文件:%s", filePath)
err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(content, this.Item)
if err != nil {
logger.LOGGER.Error("配置文件格式错误! 即将进入安装过程!")
this.Installed = false
return
}
//验证项是否齐全
itemValidate := this.Item.validate()
if !itemValidate {
logger.LOGGER.Error("配置文件信息不齐全! 即将进入安装过程!")
this.Installed = false
return
}
//使用配置项中的文件路径
if this.Item.MatterPath == "" {
this.MatterPath = tool.GetHomePath() + "/matter"
} else {
this.MatterPath = this.Item.MatterPath
}
tool.MakeDirAll(CONFIG.MatterPath)
//使用配置项中的端口
if this.Item.ServerPort != 0 {
this.ServerPort = this.Item.ServerPort
}
this.MysqlUrl = tool.GetMysqlUrl(this.Item.MysqlPort, this.Item.MysqlHost, this.Item.MysqlSchema, this.Item.MysqlUsername, this.Item.MysqlPassword)
this.Installed = true
logger.LOGGER.Info("使用配置文件:%s", filePath)
logger.LOGGER.Info("上传文件存放路径:%s", this.MatterPath)
}
}
//系统如果安装好了就调用这个方法。
func (this *Config) InstallOk() {
this.ReadFromConfigFile()
}