memos.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "os/signal"
  8. "syscall"
  9. "time"
  10. "github.com/spf13/cobra"
  11. "github.com/spf13/viper"
  12. "github.com/usememos/memos/server"
  13. _profile "github.com/usememos/memos/server/profile"
  14. "github.com/usememos/memos/setup"
  15. "github.com/usememos/memos/store"
  16. "github.com/usememos/memos/store/db"
  17. )
  18. const (
  19. greetingBanner = `
  20. ███╗ ███╗███████╗███╗ ███╗ ██████╗ ███████╗
  21. ████╗ ████║██╔════╝████╗ ████║██╔═══██╗██╔════╝
  22. ██╔████╔██║█████╗ ██╔████╔██║██║ ██║███████╗
  23. ██║╚██╔╝██║██╔══╝ ██║╚██╔╝██║██║ ██║╚════██║
  24. ██║ ╚═╝ ██║███████╗██║ ╚═╝ ██║╚██████╔╝███████║
  25. ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝
  26. `
  27. )
  28. var (
  29. profile *_profile.Profile
  30. mode string
  31. port int
  32. data string
  33. rootCmd = &cobra.Command{
  34. Use: "memos",
  35. Short: `An open-source, self-hosted memo hub with knowledge management and social networking.`,
  36. Run: func(_cmd *cobra.Command, _args []string) {
  37. ctx, cancel := context.WithCancel(context.Background())
  38. s, err := server.NewServer(ctx, profile)
  39. if err != nil {
  40. cancel()
  41. fmt.Printf("failed to create server, error: %+v\n", err)
  42. return
  43. }
  44. c := make(chan os.Signal, 1)
  45. // Trigger graceful shutdown on SIGINT or SIGTERM.
  46. // The default signal sent by the `kill` command is SIGTERM,
  47. // which is taken as the graceful shutdown signal for many systems, eg., Kubernetes, Gunicorn.
  48. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  49. go func() {
  50. sig := <-c
  51. fmt.Printf("%s received.\n", sig.String())
  52. s.Shutdown(ctx)
  53. cancel()
  54. }()
  55. println(greetingBanner)
  56. fmt.Printf("Version %s has started at :%d\n", profile.Version, profile.Port)
  57. if err := s.Start(ctx); err != nil {
  58. if err != http.ErrServerClosed {
  59. fmt.Printf("failed to start server, error: %+v\n", err)
  60. cancel()
  61. }
  62. }
  63. // Wait for CTRL-C.
  64. <-ctx.Done()
  65. },
  66. }
  67. setupCmd = &cobra.Command{
  68. Use: "setup",
  69. Short: "Make initial setup for memos",
  70. Run: func(cmd *cobra.Command, _ []string) {
  71. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  72. defer cancel()
  73. hostUsername, err := cmd.Flags().GetString(setupCmdFlagHostUsername)
  74. if err != nil {
  75. fmt.Printf("failed to get owner username, error: %+v\n", err)
  76. return
  77. }
  78. hostPassword, err := cmd.Flags().GetString(setupCmdFlagHostPassword)
  79. if err != nil {
  80. fmt.Printf("failed to get owner password, error: %+v\n", err)
  81. return
  82. }
  83. db := db.NewDB(profile)
  84. if err := db.Open(ctx); err != nil {
  85. fmt.Printf("failed to open db, error: %+v\n", err)
  86. return
  87. }
  88. store := store.New(db.DBInstance, profile)
  89. if err := setup.Execute(ctx, store, hostUsername, hostPassword); err != nil {
  90. fmt.Printf("failed to setup, error: %+v\n", err)
  91. return
  92. }
  93. },
  94. }
  95. )
  96. func Execute() error {
  97. return rootCmd.Execute()
  98. }
  99. func init() {
  100. cobra.OnInitialize(initConfig)
  101. rootCmd.PersistentFlags().StringVarP(&mode, "mode", "m", "demo", `mode of server, can be "prod" or "dev" or "demo"`)
  102. rootCmd.PersistentFlags().IntVarP(&port, "port", "p", 8081, "port of server")
  103. rootCmd.PersistentFlags().StringVarP(&data, "data", "d", "", "data directory")
  104. err := viper.BindPFlag("mode", rootCmd.PersistentFlags().Lookup("mode"))
  105. if err != nil {
  106. panic(err)
  107. }
  108. err = viper.BindPFlag("port", rootCmd.PersistentFlags().Lookup("port"))
  109. if err != nil {
  110. panic(err)
  111. }
  112. err = viper.BindPFlag("data", rootCmd.PersistentFlags().Lookup("data"))
  113. if err != nil {
  114. panic(err)
  115. }
  116. viper.SetDefault("mode", "demo")
  117. viper.SetDefault("port", 8081)
  118. viper.SetEnvPrefix("memos")
  119. setupCmd.Flags().String(setupCmdFlagHostUsername, "", "Owner username")
  120. setupCmd.Flags().String(setupCmdFlagHostPassword, "", "Owner password")
  121. rootCmd.AddCommand(setupCmd)
  122. }
  123. func initConfig() {
  124. viper.AutomaticEnv()
  125. var err error
  126. profile, err = _profile.GetProfile()
  127. if err != nil {
  128. fmt.Printf("failed to get profile, error: %+v\n", err)
  129. return
  130. }
  131. println("---")
  132. println("Server profile")
  133. println("dsn:", profile.DSN)
  134. println("port:", profile.Port)
  135. println("mode:", profile.Mode)
  136. println("version:", profile.Version)
  137. println("---")
  138. }
  139. const (
  140. setupCmdFlagHostUsername = "host-username"
  141. setupCmdFlagHostPassword = "host-password"
  142. )