gocron/modules/app/app.go

117 lines
2.4 KiB
Go
Raw Normal View History

2017-03-14 06:31:46 +00:00
package app
import (
2017-09-16 09:58:33 +00:00
"os"
"github.com/ouqiang/gocron/modules/logger"
"github.com/ouqiang/gocron/modules/setting"
"github.com/ouqiang/gocron/modules/utils"
"io/ioutil"
"strconv"
"strings"
2017-03-14 06:31:46 +00:00
)
2017-04-02 02:19:52 +00:00
var (
2017-09-16 09:58:33 +00:00
AppDir string // 应用根目录
ConfDir string // 配置目录
LogDir string // 日志目录
DataDir string // 存放session等
AppConfig string // 应用配置文件
Installed bool // 应用是否安装过
Setting *setting.Setting // 应用配置
VersionId int // 版本号
VersionFile string // 版本号文件
2017-03-14 06:31:46 +00:00
)
func InitEnv(versionString string) {
2017-09-16 09:58:33 +00:00
logger.InitLogger()
wd, err := os.Getwd()
if err != nil {
logger.Fatal(err)
}
AppDir = wd
ConfDir = AppDir + "/conf"
LogDir = AppDir + "/log"
DataDir = AppDir + "/data"
AppConfig = ConfDir + "/app.ini"
VersionFile = ConfDir + "/.version"
checkDirExists(ConfDir, LogDir, DataDir)
Installed = IsInstalled()
VersionId = ToNumberVersion(versionString)
2017-03-14 06:31:46 +00:00
}
// 判断应用是否已安装
2017-03-14 06:31:46 +00:00
func IsInstalled() bool {
2017-09-16 09:58:33 +00:00
_, err := os.Stat(ConfDir + "/install.lock")
if os.IsNotExist(err) {
return false
}
2017-03-14 06:31:46 +00:00
2017-09-16 09:58:33 +00:00
return true
2017-03-14 06:31:46 +00:00
}
// 创建安装锁文件
func CreateInstallLock() error {
2017-09-16 09:58:33 +00:00
_, err := os.Create(ConfDir + "/install.lock")
if err != nil {
logger.Error("创建安装锁文件conf/install.lock失败")
}
2017-03-14 06:31:46 +00:00
2017-09-16 09:58:33 +00:00
return err
2017-03-14 06:31:46 +00:00
}
// 更新应用版本号文件
2017-09-16 09:58:33 +00:00
func UpdateVersionFile() {
err := ioutil.WriteFile(VersionFile,
[]byte(strconv.Itoa(VersionId)),
0644,
)
if err != nil {
logger.Fatal(err)
}
}
// 获取应用当前版本号, 从版本号文件中读取
func GetCurrentVersionId() int {
2017-09-16 09:58:33 +00:00
if !utils.FileExist(VersionFile) {
return 0
}
2017-09-16 09:58:33 +00:00
bytes, err := ioutil.ReadFile(VersionFile)
if err != nil {
logger.Fatal(err)
}
2017-09-16 09:58:33 +00:00
versionId, err := strconv.Atoi(strings.TrimSpace(string(bytes)))
if err != nil {
logger.Fatal(err)
}
2017-09-16 09:58:33 +00:00
return versionId
}
// 把字符串版本号a.b.c转换为整数版本号abc
2017-09-16 09:58:33 +00:00
func ToNumberVersion(versionString string) int {
v := strings.Replace(versionString, ".", "", -1)
if len(v) < 3 {
v += "0"
}
versionId, err := strconv.Atoi(v)
if err != nil {
logger.Fatal(err)
}
return versionId
}
2017-03-14 06:31:46 +00:00
// 检测目录是否存在
2017-04-02 02:19:52 +00:00
func checkDirExists(path ...string) {
2017-09-16 09:58:33 +00:00
for _, value := range path {
if !utils.FileExist(value) {
logger.Fatal(value + "目录不存在或无权限访问")
}
}
}