Files
EdgeNode/internal/utils/service.go

113 lines
2.1 KiB
Go
Raw Normal View History

2021-01-11 18:16:15 +08:00
package utils
import (
"log"
"os"
"path/filepath"
"runtime"
"sync"
2024-07-27 15:42:50 +08:00
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/files"
"github.com/iwind/TeaGo/logs"
2021-01-11 18:16:15 +08:00
)
2023-08-08 12:02:21 +08:00
// ServiceManager 服务管理器
2021-01-11 18:16:15 +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 12:02:21 +08:00
_ = logFile.Delete()
2021-01-11 18:16:15 +08:00
}
//logger
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)
})
}
2023-08-08 12:02:21 +08:00
// Log 记录普通日志
2021-01-11 18:16:15 +08:00
func (this *ServiceManager) Log(msg string) {
this.setup()
if this.logger == nil {
return
}
this.logger.Println("[info]" + msg)
}
2023-08-08 12:02:21 +08:00
// LogError 记录错误日志
2021-01-11 18:16:15 +08:00
func (this *ServiceManager) LogError(msg string) {
this.setup()
if this.logger == nil {
return
}
this.logger.Println("[error]" + msg)
}
2023-08-08 12:02:21 +08:00
// Close 关闭
2021-01-11 18:16:15 +08:00
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")
}
2023-08-08 12:02:21 +08:00
// PauseWindow 保持命令行窗口是打开的
2021-01-11 18:16:15 +08:00
func (this *ServiceManager) PauseWindow() {
if runtime.GOOS != "windows" {
return
}
b := make([]byte, 1)
_, _ = os.Stdin.Read(b)
}