Finish all the i18n things of code.

This commit is contained in:
zicla
2019-05-06 02:18:08 +08:00
parent e37b248c8c
commit 54c5905a58
38 changed files with 504 additions and 720 deletions

View File

@ -10,76 +10,68 @@ import (
"unsafe"
)
/*
如果你需要在本地127.0.0.1创建默认的数据库和账号,使用以下语句。
create database tank;
grant all privileges on tank.* to tank identified by 'tank123';
flush privileges;
*/
//依赖外部定义的变量。
type TankConfig struct {
//默认监听端口号
//server port
serverPort int
//网站是否已经完成安装
//whether installed
installed bool
//上传的文件路径,要求不以/结尾。如果没有指定默认在根目录下的matter文件夹中。eg: /var/www/matter
//file storage location. eg./var/www/matter
matterPath string
//数据库连接信息。
//mysql url.
mysqlUrl string
//配置文件中的项
//configs in tank.json
item *ConfigItem
}
//tank.json文件中的键值一一对应。
//tank.json config items.
type ConfigItem struct {
//默认监听端口号
//server port
ServerPort int
//上传的文件路径,要求不以/结尾。如果没有指定默认在根目录下的matter文件夹中。eg: /var/www/matter
//file storage location. eg./var/www/matter
MatterPath string
//mysql相关配置。
//数据库端口
//mysql configurations.
//mysql port
MysqlPort int
//数据库Host
//mysql host
MysqlHost string
//数据库名字
//mysql schema
MysqlSchema string
//用户名
//mysql username
MysqlUsername string
//密码
//mysql password
MysqlPassword string
}
//验证配置文件的正确性。
//validate whether the config file is ok
func (this *ConfigItem) validate() bool {
if this.ServerPort == 0 {
core.LOGGER.Error("ServerPort 未配置")
core.LOGGER.Error("ServerPort is not configured")
return false
}
if this.MysqlUsername == "" {
core.LOGGER.Error("MysqlUsername 未配置")
core.LOGGER.Error("MysqlUsername is not configured")
return false
}
if this.MysqlPassword == "" {
core.LOGGER.Error("MysqlPassword 未配置")
core.LOGGER.Error("MysqlPassword is not configured")
return false
}
if this.MysqlHost == "" {
core.LOGGER.Error("MysqlHost 未配置")
core.LOGGER.Error("MysqlHost is not configured")
return false
}
if this.MysqlPort == 0 {
core.LOGGER.Error("MysqlPort 未配置")
core.LOGGER.Error("MysqlPort is not configured")
return false
}
if this.MysqlSchema == "" {
core.LOGGER.Error("MysqlSchema 未配置")
core.LOGGER.Error("MysqlSchema is not configured")
return false
}
@ -87,12 +79,11 @@ func (this *ConfigItem) validate() bool {
}
//验证配置文件是否完好
func (this *TankConfig) Init() {
//JSON初始化
//JSON init.
jsoniter.RegisterTypeDecoderFunc("time.Time", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
//如果使用time.UTC那么时间会相差8小时
//if use time.UTC there will be 8 hours gap.
t, err := time.ParseInLocation("2006-01-02 15:04:05", iter.ReadString(), time.Local)
if err != nil {
iter.Error = err
@ -103,45 +94,44 @@ func (this *TankConfig) Init() {
jsoniter.RegisterTypeEncoderFunc("time.Time", func(ptr unsafe.Pointer, stream *jsoniter.Stream) {
t := *((*time.Time)(ptr))
//如果使用time.UTC那么时间会相差8小时
//if use time.UTC there will be 8 hours gap.
stream.WriteString(t.Local().Format("2006-01-02 15:04:05"))
}, nil)
//默认从core.DEFAULT_SERVER_PORT端口启动
//default server port.
this.serverPort = core.DEFAULT_SERVER_PORT
this.ReadFromConfigFile()
}
//系统如果安装好了就调用这个方法。
func (this *TankConfig) ReadFromConfigFile() {
//读取配置文件
//read from tank.json
filePath := util.GetConfPath() + "/tank.json"
content, err := ioutil.ReadFile(filePath)
if err != nil {
core.LOGGER.Warn("无法找到配置文件:%s 即将进入安装过程!", filePath)
core.LOGGER.Warn("cannot find config file %s, installation will start!", filePath)
this.installed = false
} else {
this.item = &ConfigItem{}
core.LOGGER.Warn("读取配置文件:%s", filePath)
core.LOGGER.Warn("read config file %s", filePath)
err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(content, this.item)
if err != nil {
core.LOGGER.Error("配置文件格式错误! 即将进入安装过程!")
core.LOGGER.Error("config file error, installation will start!")
this.installed = false
return
}
//验证项是否齐全
//check the integrity
itemValidate := this.item.validate()
if !itemValidate {
core.LOGGER.Error("配置文件信息不齐全! 即将进入安装过程!")
core.LOGGER.Error("config file not integrity, installation will start!")
this.installed = false
return
}
//使用配置项中的文件路径
//use default file location.
if this.item.MatterPath == "" {
this.matterPath = util.GetHomePath() + "/matter"
} else {
@ -149,7 +139,7 @@ func (this *TankConfig) ReadFromConfigFile() {
}
util.MakeDirAll(this.matterPath)
//使用配置项中的端口
//use default server port
if this.item.ServerPort != 0 {
this.serverPort = this.item.ServerPort
}
@ -157,57 +147,50 @@ func (this *TankConfig) ReadFromConfigFile() {
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)
core.LOGGER.Info("use config file: %s", filePath)
core.LOGGER.Info("file storage location: %s", this.matterPath)
}
}
//是否已经安装
//whether installed.
func (this *TankConfig) Installed() bool {
return this.installed
}
//启动端口
//server port
func (this *TankConfig) ServerPort() int {
return this.serverPort
}
//获取mysql链接
//mysql url
func (this *TankConfig) MysqlUrl() string {
return this.mysqlUrl
}
//文件存放路径
//matter path
func (this *TankConfig) MatterPath() string {
return this.matterPath
}
//完成安装过程,主要是要将配置写入到文件中
//Finish the installation. Write config to tank.json
func (this *TankConfig) FinishInstall(mysqlPort int, mysqlHost string, mysqlSchema string, mysqlUsername string, mysqlPassword string) {
var configItem = &ConfigItem{
//默认监听端口号
//server port
ServerPort: core.CONFIG.ServerPort(),
//上传的文件路径,要求不以/结尾。如果没有指定默认在根目录下的matter文件夹中。eg: /var/www/matter
MatterPath: core.CONFIG.MatterPath(),
//mysql相关配置。
//数据库端口
MysqlPort: mysqlPort,
//数据库Host
MysqlHost: mysqlHost,
//数据库名字
MysqlSchema: mysqlSchema,
//用户名
//file storage location. eg./var/www/matter
MatterPath: core.CONFIG.MatterPath(),
MysqlPort: mysqlPort,
MysqlHost: mysqlHost,
MysqlSchema: mysqlSchema,
MysqlUsername: mysqlUsername,
//密码
MysqlPassword: mysqlPassword,
}
//用json的方式输出返回值。为了让格式更好看。
//pretty json.
jsonStr, _ := jsoniter.ConfigCompatibleWithStandardLibrary.MarshalIndent(configItem, "", " ")
//写入到配置文件中(不能使用os.O_APPEND 否则会追加)
//Write to tank.json (cannot use os.O_APPEND or append)
filePath := util.GetConfPath() + "/tank.json"
f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0777)
core.PanicError(err)