memos/bin/server/cmd/root.go

60 lines
1.5 KiB
Go
Raw Normal View History

2022-02-03 15:32:03 +08:00
package cmd
import (
"fmt"
"os"
"memos/server"
2022-05-22 09:29:34 +08:00
"memos/server/profile"
2022-02-03 15:32:03 +08:00
"memos/store"
2022-05-22 00:59:22 +08:00
DB "memos/store/db"
2022-02-03 15:32:03 +08:00
)
2022-05-20 22:48:36 +08:00
const (
greetingBanner = `
`
)
2022-02-03 15:32:03 +08:00
type Main struct {
2022-05-22 09:29:34 +08:00
profile *profile.Profile
2022-02-03 15:32:03 +08:00
}
func (m *Main) Run() error {
2022-05-22 00:59:22 +08:00
db := DB.NewDB(m.profile)
2022-02-03 15:32:03 +08:00
if err := db.Open(); err != nil {
return fmt.Errorf("cannot open db: %w", err)
}
2022-03-29 20:53:43 +08:00
s := server.NewServer(m.profile)
2022-02-03 15:32:03 +08:00
2022-05-22 00:59:22 +08:00
storeInstance := store.New(db.Db, m.profile)
2022-05-16 07:37:23 +08:00
s.Store = storeInstance
2022-02-03 15:32:03 +08:00
if err := s.Run(); err != nil {
return err
}
return nil
}
2022-03-29 20:53:43 +08:00
func Execute() {
2022-05-22 09:29:34 +08:00
profile := profile.GetProfile()
2022-03-29 20:53:43 +08:00
m := Main{
2022-05-17 21:21:13 +08:00
profile: profile,
2022-03-29 20:53:43 +08:00
}
2022-05-20 22:48:36 +08:00
println(greetingBanner)
fmt.Printf("Version %s has started at :%d\n", profile.Version, profile.Port)
if err := m.Run(); err != nil {
fmt.Printf("error: %+v\n", err)
2022-03-29 20:53:43 +08:00
os.Exit(1)
}
}