feat:简化main函数

This commit is contained in:
Mmx
2021-07-11 13:05:10 +08:00
parent f3f122a1c5
commit cd15385aec
4 changed files with 44 additions and 31 deletions

13
controllers/daemon.go Normal file
View File

@@ -0,0 +1,13 @@
package controllers
import (
"flag"
)
func init() {
goDaemon := flag.Bool("daemon", false, "")
flag.Parse()
if *goDaemon {
Guardian()
}
}

View File

@@ -3,6 +3,8 @@ package controllers
import ( import (
"Mmx/global" "Mmx/global"
"Mmx/util" "Mmx/util"
"os"
"os/exec"
"time" "time"
) )
@@ -19,3 +21,19 @@ func Guardian() {
}() }()
} }
} }
func EnterGuardian() {
if global.Config.Settings.Guardian != 0 {
global.Status.Daemon = true
util.Log.Println("[Guardian mode]")
if global.Config.Settings.Daemon {
if err := exec.Command(os.Args[0], "-daemon").Start(); err != nil {
util.ErrHandler(err)
return
}
util.Log.Println("[Daemon mode entered]")
return
}
Guardian()
}
}

33
main.go
View File

@@ -3,44 +3,15 @@ package main
import ( import (
"Mmx/Util" "Mmx/Util"
"Mmx/controllers" "Mmx/controllers"
"Mmx/global"
"flag"
"os"
"os/exec"
"runtime"
) )
func main() { func main() {
goDaemon := flag.Bool("daemon", false, "") defer util.Log.CatchRecover()
flag.Parse()
if *goDaemon {
controllers.Guardian()
}
defer func() {
if e := recover(); e != nil {
util.Log.Println(e)
var buf [4096]byte
util.Log.Println(string(buf[:runtime.Stack(buf[:], false)]))
os.Exit(1)
}
}()
if err := controllers.Login(true); err != nil { if err := controllers.Login(true); err != nil {
util.ErrHandler(err) util.ErrHandler(err)
return return
} }
if global.Config.Settings.Guardian != 0 { controllers.EnterGuardian()
global.Status.Daemon = true
util.Log.Println("[Guardian mode]")
if global.Config.Settings.Daemon {
if err := exec.Command(os.Args[0], "-daemon").Start(); err != nil {
util.ErrHandler(err)
return
}
util.Log.Println("[Daemon mode entered]")
return
}
controllers.Guardian()
}
} }

View File

@@ -4,7 +4,9 @@ import (
"Mmx/Global" "Mmx/Global"
"fmt" "fmt"
"log" "log"
"os"
"reflect" "reflect"
"runtime"
"time" "time"
) )
@@ -57,3 +59,12 @@ func (c *loG) Fatalln(a ...interface{}) {
c.WriteLog("LoginError-"+c.timeStamp+".log", a...) c.WriteLog("LoginError-"+c.timeStamp+".log", a...)
log.Fatalln(a...) log.Fatalln(a...)
} }
func (c *loG) CatchRecover() {
if e := recover(); e != nil {
c.Println(e)
var buf [4096]byte
c.Println(string(buf[:runtime.Stack(buf[:], false)]))
os.Exit(1)
}
}