gocron/modules/app/app.go

65 lines
1.5 KiB
Go
Raw Normal View History

2017-03-14 06:31:46 +00:00
package app
import (
2017-04-02 02:38:49 +00:00
"os"
2017-03-23 05:58:42 +00:00
2017-04-07 01:22:00 +00:00
"github.com/ouqiang/gocron/modules/logger"
2017-04-06 06:58:34 +00:00
"runtime"
2017-05-01 05:59:52 +00:00
"github.com/ouqiang/gocron/modules/utils"
"gopkg.in/ini.v1"
2017-03-14 06:31:46 +00:00
)
2017-04-02 02:19:52 +00:00
var (
2017-04-02 02:38:49 +00:00
AppDir string // 应用根目录
ConfDir string // 配置目录
LogDir string // 日志目录
DataDir string // 存放session等
2017-04-02 02:38:49 +00:00
AppConfig string // 应用配置文件
Installed bool // 应用是否安装过
Setting *ini.Section // 应用配置
2017-03-14 06:31:46 +00:00
)
2017-03-24 09:55:44 +00:00
func InitEnv() {
2017-04-16 18:01:41 +00:00
runtime.GOMAXPROCS(runtime.NumCPU())
2017-04-03 07:27:19 +00:00
logger.InitLogger()
2017-04-02 02:38:49 +00:00
wd, err := os.Getwd()
if err != nil {
2017-04-03 10:16:23 +00:00
logger.Fatal(err)
2017-04-02 02:38:49 +00:00
}
AppDir = wd
ConfDir = AppDir + "/conf"
LogDir = AppDir + "/log"
DataDir = AppDir + "/data"
2017-04-02 02:38:49 +00:00
AppConfig = ConfDir + "/app.ini"
checkDirExists(ConfDir, LogDir, DataDir)
2017-04-02 02:38:49 +00:00
Installed = IsInstalled()
2017-03-14 06:31:46 +00:00
}
// 判断应用是否安装过
func IsInstalled() bool {
2017-04-02 02:38:49 +00:00
_, err := os.Stat(ConfDir + "/install.lock")
if os.IsNotExist(err) {
return false
}
2017-03-14 06:31:46 +00:00
2017-04-02 02:38:49 +00:00
return true
2017-03-14 06:31:46 +00:00
}
// 创建安装锁文件
func CreateInstallLock() error {
2017-04-02 02:38:49 +00:00
_, err := os.Create(ConfDir + "/install.lock")
if err != nil {
logger.Error("创建安装锁文件conf/install.lock失败")
2017-04-02 02:38:49 +00:00
}
2017-03-14 06:31:46 +00:00
2017-04-02 02:38:49 +00:00
return err
2017-03-14 06:31:46 +00:00
}
// 检测目录是否存在
2017-04-02 02:19:52 +00:00
func checkDirExists(path ...string) {
2017-04-02 02:38:49 +00:00
for _, value := range path {
2017-05-01 05:59:52 +00:00
if !utils.FileExist(value) {
logger.Fatal(value + "目录不存在或无权限访问")
2017-04-02 02:38:49 +00:00
}
}
2017-05-01 05:59:52 +00:00
}