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"
|
2017-05-11 13:39:34 +08:00
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
_ "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"
|
2017-06-02 16:08:14 +08:00
|
|
|
"github.com/kardianos/service"
|
2017-05-20 09:52:18 +08:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2021-03-23 15:09:17 +08:00
|
|
|
"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
|
|
|
|
2017-06-02 16:08:14 +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()
|
|
|
|
|
2017-06-02 16:08:14 +08:00
|
|
|
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())
|
2017-05-10 13:43:36 +08:00
|
|
|
|
2023-05-22 17:40:07 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Create service error => ", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-05-11 13:39:34 +08:00
|
|
|
|
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
|
|
|
}
|