123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package cmd
- import (
- "context"
- "fmt"
- "net/http"
- "os"
- "os/signal"
- "syscall"
- "time"
- "github.com/spf13/cobra"
- "github.com/spf13/viper"
- "github.com/usememos/memos/server"
- _profile "github.com/usememos/memos/server/profile"
- "github.com/usememos/memos/setup"
- "github.com/usememos/memos/store"
- "github.com/usememos/memos/store/db"
- )
- const (
- greetingBanner = `
- ███╗ ███╗███████╗███╗ ███╗ ██████╗ ███████╗
- ████╗ ████║██╔════╝████╗ ████║██╔═══██╗██╔════╝
- ██╔████╔██║█████╗ ██╔████╔██║██║ ██║███████╗
- ██║╚██╔╝██║██╔══╝ ██║╚██╔╝██║██║ ██║╚════██║
- ██║ ╚═╝ ██║███████╗██║ ╚═╝ ██║╚██████╔╝███████║
- ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝
- `
- )
- var (
- profile *_profile.Profile
- mode string
- port int
- data string
- rootCmd = &cobra.Command{
- Use: "memos",
- Short: `An open-source, self-hosted memo hub with knowledge management and social networking.`,
- Run: func(_cmd *cobra.Command, _args []string) {
- ctx, cancel := context.WithCancel(context.Background())
- s, err := server.NewServer(ctx, profile)
- if err != nil {
- cancel()
- fmt.Printf("failed to create server, error: %+v\n", err)
- return
- }
- c := make(chan os.Signal, 1)
- // Trigger graceful shutdown on SIGINT or SIGTERM.
- // The default signal sent by the `kill` command is SIGTERM,
- // which is taken as the graceful shutdown signal for many systems, eg., Kubernetes, Gunicorn.
- signal.Notify(c, os.Interrupt, syscall.SIGTERM)
- go func() {
- sig := <-c
- fmt.Printf("%s received.\n", sig.String())
- s.Shutdown(ctx)
- cancel()
- }()
- println(greetingBanner)
- fmt.Printf("Version %s has started at :%d\n", profile.Version, profile.Port)
- if err := s.Start(ctx); err != nil {
- if err != http.ErrServerClosed {
- fmt.Printf("failed to start server, error: %+v\n", err)
- cancel()
- }
- }
- // Wait for CTRL-C.
- <-ctx.Done()
- },
- }
- setupCmd = &cobra.Command{
- Use: "setup",
- Short: "Make initial setup for memos",
- Run: func(cmd *cobra.Command, _ []string) {
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- hostUsername, err := cmd.Flags().GetString(setupCmdFlagHostUsername)
- if err != nil {
- fmt.Printf("failed to get owner username, error: %+v\n", err)
- return
- }
- hostPassword, err := cmd.Flags().GetString(setupCmdFlagHostPassword)
- if err != nil {
- fmt.Printf("failed to get owner password, error: %+v\n", err)
- return
- }
- db := db.NewDB(profile)
- if err := db.Open(ctx); err != nil {
- fmt.Printf("failed to open db, error: %+v\n", err)
- return
- }
- store := store.New(db.DBInstance, profile)
- if err := setup.Execute(ctx, store, hostUsername, hostPassword); err != nil {
- fmt.Printf("failed to setup, error: %+v\n", err)
- return
- }
- },
- }
- )
- func Execute() error {
- return rootCmd.Execute()
- }
- func init() {
- cobra.OnInitialize(initConfig)
- rootCmd.PersistentFlags().StringVarP(&mode, "mode", "m", "demo", `mode of server, can be "prod" or "dev" or "demo"`)
- rootCmd.PersistentFlags().IntVarP(&port, "port", "p", 8081, "port of server")
- rootCmd.PersistentFlags().StringVarP(&data, "data", "d", "", "data directory")
- err := viper.BindPFlag("mode", rootCmd.PersistentFlags().Lookup("mode"))
- if err != nil {
- panic(err)
- }
- err = viper.BindPFlag("port", rootCmd.PersistentFlags().Lookup("port"))
- if err != nil {
- panic(err)
- }
- err = viper.BindPFlag("data", rootCmd.PersistentFlags().Lookup("data"))
- if err != nil {
- panic(err)
- }
- viper.SetDefault("mode", "demo")
- viper.SetDefault("port", 8081)
- viper.SetEnvPrefix("memos")
- setupCmd.Flags().String(setupCmdFlagHostUsername, "", "Owner username")
- setupCmd.Flags().String(setupCmdFlagHostPassword, "", "Owner password")
- rootCmd.AddCommand(setupCmd)
- }
- func initConfig() {
- viper.AutomaticEnv()
- var err error
- profile, err = _profile.GetProfile()
- if err != nil {
- fmt.Printf("failed to get profile, error: %+v\n", err)
- return
- }
- println("---")
- println("Server profile")
- println("dsn:", profile.DSN)
- println("port:", profile.Port)
- println("mode:", profile.Mode)
- println("version:", profile.Version)
- println("---")
- }
- const (
- setupCmdFlagHostUsername = "host-username"
- setupCmdFlagHostPassword = "host-password"
- )
|