mirror of
https://github.com/usememos/memos.git
synced 2025-04-17 10:57:24 +02:00
198 lines
5.7 KiB
Go
198 lines
5.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/usememos/memos/server"
|
|
"github.com/usememos/memos/server/profile"
|
|
"github.com/usememos/memos/server/version"
|
|
"github.com/usememos/memos/store"
|
|
"github.com/usememos/memos/store/db"
|
|
)
|
|
|
|
const (
|
|
greetingBanner = `
|
|
███╗ ███╗███████╗███╗ ███╗ ██████╗ ███████╗
|
|
████╗ ████║██╔════╝████╗ ████║██╔═══██╗██╔════╝
|
|
██╔████╔██║█████╗ ██╔████╔██║██║ ██║███████╗
|
|
██║╚██╔╝██║██╔══╝ ██║╚██╔╝██║██║ ██║╚════██║
|
|
██║ ╚═╝ ██║███████╗██║ ╚═╝ ██║╚██████╔╝███████║
|
|
╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝
|
|
`
|
|
)
|
|
|
|
var (
|
|
rootCmd = &cobra.Command{
|
|
Use: "memos",
|
|
Short: `An open source, lightweight note-taking service. Easily capture and share your great thoughts.`,
|
|
Run: func(_ *cobra.Command, _ []string) {
|
|
instanceProfile := &profile.Profile{
|
|
Mode: viper.GetString("mode"),
|
|
Addr: viper.GetString("addr"),
|
|
Port: viper.GetInt("port"),
|
|
Data: viper.GetString("data"),
|
|
Driver: viper.GetString("driver"),
|
|
DSN: viper.GetString("dsn"),
|
|
Public: viper.GetBool("public"),
|
|
PasswordAuth: viper.GetBool("password-auth"),
|
|
Version: version.GetCurrentVersion(viper.GetString("mode")),
|
|
}
|
|
if err := instanceProfile.Validate(); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
dbDriver, err := db.NewDBDriver(instanceProfile)
|
|
if err != nil {
|
|
cancel()
|
|
slog.Error("failed to create db driver", "error", err)
|
|
return
|
|
}
|
|
if err := dbDriver.Migrate(ctx); err != nil {
|
|
cancel()
|
|
slog.Error("failed to migrate database", "error", err)
|
|
return
|
|
}
|
|
|
|
storeInstance := store.New(dbDriver, instanceProfile)
|
|
if err := storeInstance.MigrateManually(ctx); err != nil {
|
|
cancel()
|
|
slog.Error("failed to migrate manually", "error", err)
|
|
return
|
|
}
|
|
|
|
s, err := server.NewServer(ctx, instanceProfile, storeInstance)
|
|
if err != nil {
|
|
cancel()
|
|
slog.Error("failed to create server", "error", 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)
|
|
|
|
if err := s.Start(ctx); err != nil {
|
|
if err != http.ErrServerClosed {
|
|
slog.Error("failed to start server", "error", err)
|
|
cancel()
|
|
}
|
|
}
|
|
|
|
printGreetings(instanceProfile)
|
|
|
|
go func() {
|
|
<-c
|
|
s.Shutdown(ctx)
|
|
cancel()
|
|
}()
|
|
|
|
// Wait for CTRL-C.
|
|
<-ctx.Done()
|
|
},
|
|
}
|
|
)
|
|
|
|
func init() {
|
|
viper.SetDefault("mode", "demo")
|
|
viper.SetDefault("driver", "sqlite")
|
|
viper.SetDefault("addr", "")
|
|
viper.SetDefault("port", 8081)
|
|
viper.SetDefault("public", false)
|
|
viper.SetDefault("password-auth", true)
|
|
|
|
rootCmd.PersistentFlags().String("mode", "demo", `mode of server, can be "prod" or "dev" or "demo"`)
|
|
rootCmd.PersistentFlags().String("addr", "", "address of server")
|
|
rootCmd.PersistentFlags().Int("port", 8081, "port of server")
|
|
rootCmd.PersistentFlags().String("data", "", "data directory")
|
|
rootCmd.PersistentFlags().String("driver", "sqlite", "database driver")
|
|
rootCmd.PersistentFlags().String("dsn", "", "database source name(aka. DSN)")
|
|
rootCmd.PersistentFlags().Bool("public", false, "")
|
|
rootCmd.PersistentFlags().Bool("password-auth", true, "")
|
|
|
|
err := viper.BindPFlag("mode", rootCmd.PersistentFlags().Lookup("mode"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
err = viper.BindPFlag("addr", rootCmd.PersistentFlags().Lookup("addr"))
|
|
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)
|
|
}
|
|
err = viper.BindPFlag("driver", rootCmd.PersistentFlags().Lookup("driver"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
err = viper.BindPFlag("dsn", rootCmd.PersistentFlags().Lookup("dsn"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
err = viper.BindPFlag("public", rootCmd.PersistentFlags().Lookup("public"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
err = viper.BindPFlag("password-auth", rootCmd.PersistentFlags().Lookup("password-auth"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
viper.SetEnvPrefix("memos")
|
|
viper.AutomaticEnv()
|
|
if err := viper.BindEnv("password-auth", "MEMOS_PASSWORD_AUTH"); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func printGreetings(profile *profile.Profile) {
|
|
fmt.Printf(`---
|
|
Server profile
|
|
version: %s
|
|
data: %s
|
|
dsn: %s
|
|
addr: %s
|
|
port: %d
|
|
mode: %s
|
|
public: %t
|
|
password-auth: %t
|
|
driver: %s
|
|
---
|
|
`, profile.Version, profile.Data, profile.DSN, profile.Addr, profile.Port, profile.Mode, profile.Public, profile.PasswordAuth, profile.Driver)
|
|
|
|
print(greetingBanner)
|
|
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)
|
|
}
|
|
fmt.Printf(`---
|
|
See more in:
|
|
👉Website: %s
|
|
👉GitHub: %s
|
|
---
|
|
`, "https://usememos.com", "https://github.com/usememos/memos")
|
|
}
|
|
|
|
func main() {
|
|
if err := rootCmd.Execute(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|