1
0

main.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. _ "github.com/beego/beego/v2/server/web/session/memcache"
  11. _ "github.com/beego/beego/v2/server/web/session/mysql"
  12. _ "github.com/beego/beego/v2/server/web/session/redis"
  13. "github.com/kardianos/service"
  14. _ "github.com/mattn/go-sqlite3"
  15. "github.com/mindoc-org/mindoc/commands"
  16. "github.com/mindoc-org/mindoc/commands/daemon"
  17. _ "github.com/mindoc-org/mindoc/routers"
  18. )
  19. func isViaDaemonUnix() bool {
  20. parentPid := os.Getppid()
  21. cmdLineBytes, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", parentPid))
  22. if err != nil {
  23. return false
  24. }
  25. cmdLine := string(cmdLineBytes)
  26. executable := strings.Split(cmdLine, " ")[0]
  27. fmt.Printf("Parent executable: %s\n", executable)
  28. filename := filepath.Base(executable)
  29. return strings.Contains(filename, "mindoc-daemon")
  30. }
  31. func main() {
  32. if len(os.Args) >= 3 && os.Args[1] == "service" {
  33. if os.Args[2] == "install" {
  34. daemon.Install()
  35. } else if os.Args[2] == "remove" {
  36. daemon.Uninstall()
  37. } else if os.Args[2] == "restart" {
  38. daemon.Restart()
  39. }
  40. }
  41. commands.RegisterCommand()
  42. d := daemon.NewDaemon()
  43. if runtime.GOOS != "windows" && !isViaDaemonUnix() {
  44. s, err := service.New(d, d.Config())
  45. if err != nil {
  46. fmt.Println("Create service error => ", err)
  47. os.Exit(1)
  48. }
  49. if err := s.Run(); err != nil {
  50. log.Fatal("启动程序失败 ->", err)
  51. }
  52. } else {
  53. d.Run()
  54. }
  55. }