2022-02-03 15:32:03 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"memos/server"
|
|
|
|
"memos/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Main struct {
|
|
|
|
profile *Profile
|
|
|
|
|
|
|
|
server *server.Server
|
|
|
|
|
|
|
|
db *store.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func Execute() {
|
|
|
|
m := Main{}
|
2022-02-05 13:04:06 +08:00
|
|
|
profile := GetProfile()
|
2022-02-03 15:32:03 +08:00
|
|
|
m.profile = &profile
|
|
|
|
|
2022-02-05 13:04:06 +08:00
|
|
|
err := m.Run()
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%+v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Main) Run() error {
|
2022-02-06 10:37:09 +08:00
|
|
|
db := store.NewDB(m.profile.dsn, m.profile.mode)
|
2022-02-03 15:32:03 +08:00
|
|
|
|
|
|
|
if err := db.Open(); err != nil {
|
|
|
|
return fmt.Errorf("cannot open db: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.db = db
|
|
|
|
|
2022-03-29 07:30:29 +08:00
|
|
|
s := server.NewServer(m.profile.port, m.profile.mode)
|
2022-02-03 15:32:03 +08:00
|
|
|
|
|
|
|
s.ShortcutService = store.NewShortcutService(db)
|
|
|
|
s.MemoService = store.NewMemoService(db)
|
|
|
|
s.UserService = store.NewUserService(db)
|
2022-02-04 18:54:24 +08:00
|
|
|
s.ShortcutService = store.NewShortcutService(db)
|
|
|
|
s.ResourceService = store.NewResourceService(db)
|
2022-02-03 15:32:03 +08:00
|
|
|
|
|
|
|
m.server = s
|
|
|
|
|
|
|
|
if err := s.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|