mindoc/main.go

68 lines
1.4 KiB
Go
Raw Normal View History

2017-04-19 18:19:27 +08:00
package main
import (
2017-05-09 18:17:08 +08:00
"fmt"
2023-05-22 17:40:07 +08:00
"io/ioutil"
2019-05-22 15:11:41 +08:00
"log"
2017-05-09 18:17:08 +08:00
"os"
2023-05-22 17:40:07 +08:00
"path/filepath"
"runtime"
"strings"
_ "github.com/beego/beego/v2/server/web/session/memcache"
_ "github.com/beego/beego/v2/server/web/session/mysql"
_ "github.com/beego/beego/v2/server/web/session/redis"
"github.com/kardianos/service"
_ "github.com/mattn/go-sqlite3"
"github.com/mindoc-org/mindoc/commands"
"github.com/mindoc-org/mindoc/commands/daemon"
_ "github.com/mindoc-org/mindoc/routers"
2017-04-19 18:19:27 +08:00
)
2023-05-22 17:40:07 +08:00
func isViaDaemonUnix() bool {
parentPid := os.Getppid()
cmdLineBytes, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", parentPid))
if err != nil {
return false
}
cmdLine := string(cmdLineBytes)
executable := strings.Split(cmdLine, " ")[0]
fmt.Printf("Parent executable: %s\n", executable)
filename := filepath.Base(executable)
return strings.Contains(filename, "mindoc-daemon")
}
2017-04-19 18:19:27 +08:00
func main() {
2017-05-01 17:43:33 +08:00
if len(os.Args) >= 3 && os.Args[1] == "service" {
if os.Args[2] == "install" {
daemon.Install()
} else if os.Args[2] == "remove" {
daemon.Uninstall()
} else if os.Args[2] == "restart" {
daemon.Restart()
}
}
2017-04-21 18:20:35 +08:00
commands.RegisterCommand()
d := daemon.NewDaemon()
2017-05-01 19:12:52 +08:00
2023-05-22 17:40:07 +08:00
if runtime.GOOS != "windows" && !isViaDaemonUnix() {
s, err := service.New(d, d.Config())
2023-05-22 17:40:07 +08:00
if err != nil {
fmt.Println("Create service error => ", err)
os.Exit(1)
}
2023-05-22 17:40:07 +08:00
if err := s.Run(); err != nil {
log.Fatal("启动程序失败 ->", err)
}
} else {
d.Run()
2019-05-22 15:11:41 +08:00
}
2023-05-22 17:40:07 +08:00
2017-04-19 18:19:27 +08:00
}