蓝眼网盘定制版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
4.5 KiB

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