github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/service.go (about)

     1  package utils
     2  
     3  import (
     4  	"github.com/iwind/TeaGo/Tea"
     5  	"github.com/iwind/TeaGo/files"
     6  	"github.com/iwind/TeaGo/logs"
     7  	"log"
     8  	"os"
     9  	"path/filepath"
    10  	"runtime"
    11  	"sync"
    12  )
    13  
    14  // ServiceManager 服务管理器
    15  type ServiceManager struct {
    16  	Name        string
    17  	Description string
    18  
    19  	fp         *os.File
    20  	logger     *log.Logger
    21  	onceLocker sync.Once
    22  }
    23  
    24  // 获取对象
    25  func NewServiceManager(name, description string) *ServiceManager {
    26  	manager := &ServiceManager{
    27  		Name:        name,
    28  		Description: description,
    29  	}
    30  
    31  	// root
    32  	manager.resetRoot()
    33  
    34  	return manager
    35  }
    36  
    37  // 设置服务
    38  func (this *ServiceManager) setup() {
    39  	this.onceLocker.Do(func() {
    40  		logFile := files.NewFile(Tea.Root + "/logs/service.log")
    41  		if logFile.Exists() {
    42  			_ = logFile.Delete()
    43  		}
    44  
    45  		//logger
    46  		fp, err := os.OpenFile(Tea.Root+"/logs/service.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
    47  		if err != nil {
    48  			logs.Error(err)
    49  			return
    50  		}
    51  		this.fp = fp
    52  		this.logger = log.New(fp, "", log.LstdFlags)
    53  	})
    54  }
    55  
    56  // Log 记录普通日志
    57  func (this *ServiceManager) Log(msg string) {
    58  	this.setup()
    59  	if this.logger == nil {
    60  		return
    61  	}
    62  	this.logger.Println("[info]" + msg)
    63  }
    64  
    65  // LogError 记录错误日志
    66  func (this *ServiceManager) LogError(msg string) {
    67  	this.setup()
    68  	if this.logger == nil {
    69  		return
    70  	}
    71  	this.logger.Println("[error]" + msg)
    72  }
    73  
    74  // Close 关闭
    75  func (this *ServiceManager) Close() error {
    76  	if this.fp != nil {
    77  		return this.fp.Close()
    78  	}
    79  	return nil
    80  }
    81  
    82  // 重置Root
    83  func (this *ServiceManager) resetRoot() {
    84  	if !Tea.IsTesting() {
    85  		exePath, err := os.Executable()
    86  		if err != nil {
    87  			exePath = os.Args[0]
    88  		}
    89  		link, err := filepath.EvalSymlinks(exePath)
    90  		if err == nil {
    91  			exePath = link
    92  		}
    93  		fullPath, err := filepath.Abs(exePath)
    94  		if err == nil {
    95  			Tea.UpdateRoot(filepath.Dir(filepath.Dir(fullPath)))
    96  		}
    97  	}
    98  	Tea.SetPublicDir(Tea.Root + Tea.DS + "web" + Tea.DS + "public")
    99  	Tea.SetViewsDir(Tea.Root + Tea.DS + "web" + Tea.DS + "views")
   100  	Tea.SetTmpDir(Tea.Root + Tea.DS + "web" + Tea.DS + "tmp")
   101  }
   102  
   103  // PauseWindow 保持命令行窗口是打开的
   104  func (this *ServiceManager) PauseWindow() {
   105  	if runtime.GOOS != "windows" {
   106  		return
   107  	}
   108  
   109  	b := make([]byte, 1)
   110  	_, _ = os.Stdin.Read(b)
   111  }