2023-02-22 19:07:16 +03:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
2023-09-17 17:55:13 +03:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2023-08-20 21:09:41 +03:00
|
|
|
"github.com/usememos/memos/common/log"
|
2023-02-22 19:07:16 +03:00
|
|
|
"github.com/usememos/memos/server"
|
|
|
|
_profile "github.com/usememos/memos/server/profile"
|
2023-03-27 16:22:49 +03:00
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
"github.com/usememos/memos/store/db"
|
2023-02-22 19:07:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
greetingBanner = `
|
|
|
|
███╗ ███╗███████╗███╗ ███╗ ██████╗ ███████╗
|
|
|
|
████╗ ████║██╔════╝████╗ ████║██╔═══██╗██╔════╝
|
|
|
|
██╔████╔██║█████╗ ██╔████╔██║██║ ██║███████╗
|
|
|
|
██║╚██╔╝██║██╔══╝ ██║╚██╔╝██║██║ ██║╚════██║
|
|
|
|
██║ ╚═╝ ██║███████╗██║ ╚═╝ ██║╚██████╔╝███████║
|
|
|
|
╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝
|
|
|
|
`
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
profile *_profile.Profile
|
|
|
|
mode string
|
2023-08-24 04:59:23 +03:00
|
|
|
addr string
|
2023-02-22 19:07:16 +03:00
|
|
|
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())
|
2023-06-17 16:25:46 +03:00
|
|
|
db := db.NewDB(profile)
|
2023-08-26 02:33:45 +03:00
|
|
|
if err := db.Open(); err != nil {
|
2023-06-17 16:25:46 +03:00
|
|
|
cancel()
|
2023-08-20 21:09:41 +03:00
|
|
|
log.Error("failed to open db", zap.Error(err))
|
2023-06-17 16:25:46 +03:00
|
|
|
return
|
|
|
|
}
|
2023-08-26 02:33:45 +03:00
|
|
|
if err := db.Migrate(ctx); err != nil {
|
|
|
|
cancel()
|
|
|
|
log.Error("failed to migrate db", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
2023-06-17 16:25:46 +03:00
|
|
|
|
|
|
|
store := store.New(db.DBInstance, profile)
|
|
|
|
s, err := server.NewServer(ctx, profile, store)
|
2023-02-22 19:07:16 +03:00
|
|
|
if err != nil {
|
|
|
|
cancel()
|
2023-08-20 21:09:41 +03:00
|
|
|
log.Error("failed to create server", zap.Error(err))
|
2023-02-22 19:07:16 +03:00
|
|
|
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
|
2023-08-20 21:09:41 +03:00
|
|
|
log.Info(fmt.Sprintf("%s received.\n", sig.String()))
|
2023-02-22 19:07:16 +03:00
|
|
|
s.Shutdown(ctx)
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
2023-08-20 21:09:41 +03:00
|
|
|
printGreetings()
|
|
|
|
|
2023-02-22 19:07:16 +03:00
|
|
|
if err := s.Start(ctx); err != nil {
|
|
|
|
if err != http.ErrServerClosed {
|
2023-08-20 21:09:41 +03:00
|
|
|
log.Error("failed to start server", zap.Error(err))
|
2023-02-22 19:07:16 +03:00
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for CTRL-C.
|
|
|
|
<-ctx.Done()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func Execute() error {
|
2023-08-20 21:09:41 +03:00
|
|
|
defer log.Sync()
|
2023-02-22 19:07:16 +03:00
|
|
|
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"`)
|
2023-08-24 04:59:23 +03:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&addr, "addr", "a", "", "address of server")
|
2023-02-22 19:07:16 +03:00
|
|
|
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)
|
|
|
|
}
|
2023-08-24 04:59:23 +03:00
|
|
|
err = viper.BindPFlag("addr", rootCmd.PersistentFlags().Lookup("addr"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-02-22 19:07:16 +03:00
|
|
|
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")
|
2023-08-24 04:59:23 +03:00
|
|
|
viper.SetDefault("addr", "")
|
2023-02-22 19:07:16 +03:00
|
|
|
viper.SetDefault("port", 8081)
|
|
|
|
viper.SetEnvPrefix("memos")
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2023-08-24 04:59:23 +03:00
|
|
|
println("addr:", profile.Addr)
|
2023-02-22 19:07:16 +03:00
|
|
|
println("port:", profile.Port)
|
|
|
|
println("mode:", profile.Mode)
|
|
|
|
println("version:", profile.Version)
|
|
|
|
println("---")
|
|
|
|
}
|
2023-08-20 21:09:41 +03:00
|
|
|
|
|
|
|
func printGreetings() {
|
2023-09-17 17:55:13 +03:00
|
|
|
print(greetingBanner)
|
2023-08-24 04:59:23 +03:00
|
|
|
if len(profile.Addr) == 0 {
|
|
|
|
fmt.Printf("Version %s has been started on port %d\n", profile.Version, profile.Port)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Version %s has been started on address '%s' and port %d\n", profile.Version, profile.Addr, profile.Port)
|
|
|
|
}
|
2023-09-17 17:55:13 +03:00
|
|
|
println("---")
|
|
|
|
println("See more in:")
|
2023-08-20 21:09:41 +03:00
|
|
|
fmt.Printf("👉Website: %s\n", "https://usememos.com")
|
|
|
|
fmt.Printf("👉GitHub: %s\n", "https://github.com/usememos/memos")
|
2023-09-17 17:55:13 +03:00
|
|
|
println("---")
|
2023-08-20 21:09:41 +03:00
|
|
|
}
|