Files
EdgeAdmin/internal/utils/service.go

113 lines
2.1 KiB
Go
Raw Normal View History

2021-01-12 11:49:37 +08:00
package utils
import (
"log"
"os"
"path/filepath"
"runtime"
"sync"
2024-07-27 15:42:58 +08:00
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/files"
"github.com/iwind/TeaGo/logs"
2021-01-12 11:49:37 +08:00
)
// 服务管理器
type ServiceManager struct {
Name string
Description string
fp *os.File
logger *log.Logger
onceLocker sync.Once
}
// 获取对象
func NewServiceManager(name, description string) *ServiceManager {
manager := &ServiceManager{
Name: name,
Description: description,
}
// root
manager.resetRoot()
return manager
}
// 设置服务
func (this *ServiceManager) setup() {
this.onceLocker.Do(func() {
logFile := files.NewFile(Tea.Root + "/logs/service.log")
if logFile.Exists() {
2023-08-08 14:17:16 +08:00
_ = logFile.Delete()
2021-01-12 11:49:37 +08:00
}
2024-07-27 15:42:58 +08:00
// logger
2021-01-12 11:49:37 +08:00
fp, err := os.OpenFile(Tea.Root+"/logs/service.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
logs.Error(err)
return
}
this.fp = fp
this.logger = log.New(fp, "", log.LstdFlags)
})
}
// 记录普通日志
func (this *ServiceManager) Log(msg string) {
this.setup()
if this.logger == nil {
return
}
this.logger.Println("[info]" + msg)
}
// 记录错误日志
func (this *ServiceManager) LogError(msg string) {
this.setup()
if this.logger == nil {
return
}
this.logger.Println("[error]" + msg)
}
// 关闭
func (this *ServiceManager) Close() error {
if this.fp != nil {
return this.fp.Close()
}
return nil
}
// 重置Root
func (this *ServiceManager) resetRoot() {
if !Tea.IsTesting() {
exePath, err := os.Executable()
if err != nil {
exePath = os.Args[0]
}
link, err := filepath.EvalSymlinks(exePath)
if err == nil {
exePath = link
}
fullPath, err := filepath.Abs(exePath)
if err == nil {
Tea.UpdateRoot(filepath.Dir(filepath.Dir(fullPath)))
}
}
Tea.SetPublicDir(Tea.Root + Tea.DS + "web" + Tea.DS + "public")
Tea.SetViewsDir(Tea.Root + Tea.DS + "web" + Tea.DS + "views")
Tea.SetTmpDir(Tea.Root + Tea.DS + "web" + Tea.DS + "tmp")
}
// 保持命令行窗口是打开的
func (this *ServiceManager) PauseWindow() {
if runtime.GOOS != "windows" {
return
}
b := make([]byte, 1)
_, _ = os.Stdin.Read(b)
}