-
Notifications
You must be signed in to change notification settings - Fork 50
/
manager.go
70 lines (60 loc) · 1.71 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package proxy
import (
"log"
"math/rand"
"os"
"path/filepath"
"time"
)
var randR *rand.Rand
func init() {
randR = rand.New(rand.NewSource(time.Now().UnixNano()))
}
// APIServerManager server manager
type APIServerManager struct {
ps *portServer
ConfPath string
LogFile *os.File
mainConf *mainConf
}
// NewAPIServerManager init manager
func NewAPIServerManager(confPath string) *APIServerManager {
manager := &APIServerManager{}
manager.mainConf = loadMainConf(confPath)
manager.ConfPath, _ = filepath.Abs(confPath)
manager.ps = newPortServer(manager)
return manager
}
// Start start run manager
func (manager *APIServerManager) Start() {
logPath := filepath.Dir(filepath.Dir(manager.ConfPath)) + "/log/api-front.log"
manager.setupLog(logPath)
defer manager.LogFile.Close()
manager.ps.start()
log.Println("all server shutdown")
}
func (manager *APIServerManager) rootConfDir() string {
return filepath.Dir(manager.ConfPath) + string(filepath.Separator)
}
func (manager *APIServerManager) setupLog(logPath string) {
logPathDay := logPath + "." + time.Now().Format("20060102")
DirCheck(logPathDay)
var err error
manager.LogFile, err = os.OpenFile(logPathDay, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
if err != nil {
log.Fatalln("create log file failed [", logPathDay, "]", err)
}
log.SetOutput(manager.LogFile)
SetInterval(func() {
logPathDay := logPath + "." + time.Now().Format("20060102")
if !FileExists(logPathDay) {
manager.LogFile.Close()
DirCheck(logPathDay)
manager.LogFile, err = os.OpenFile(logPathDay, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
if err != nil {
log.Println("create log file failed [", logPathDay, "]", err)
}
log.SetOutput(manager.LogFile)
}
}, 30)
}