From 7564c40c8d846c4c30b8fb1357d23b924e16bd6c Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 27 Jul 2024 23:08:22 +0800 Subject: [PATCH] chore: fix env init --- bin/memos/main.go | 67 +++++++++++++--------------- scripts/.air.toml | 2 +- server/router/api/v1/memo_service.go | 7 +-- 3 files changed, 32 insertions(+), 44 deletions(-) diff --git a/bin/memos/main.go b/bin/memos/main.go index 62a302199f125..4df196a7496be 100644 --- a/bin/memos/main.go +++ b/bin/memos/main.go @@ -31,12 +31,25 @@ const ( ) var ( - instanceProfile *profile.Profile - 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 { @@ -77,7 +90,7 @@ var ( } } - printGreetings() + printGreetings(instanceProfile) go func() { <-c @@ -91,11 +104,14 @@ var ( } ) -func Execute() error { - return rootCmd.Execute() -} - 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") @@ -143,29 +159,9 @@ func init() { if err := viper.BindEnv("password-auth", "MEMOS_PASSWORD_AUTH"); err != nil { panic(err) } +} - viper.SetDefault("mode", "demo") - viper.SetDefault("driver", "sqlite") - viper.SetDefault("addr", "") - viper.SetDefault("port", 8081) - viper.SetDefault("public", false) - viper.SetDefault("password-auth", true) - - 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) - } - +func printGreetings(profile *profile.Profile) { fmt.Printf(`--- Server profile version: %s @@ -178,15 +174,13 @@ public: %t password-auth: %t driver: %s --- -`, instanceProfile.Version, instanceProfile.Data, instanceProfile.DSN, instanceProfile.Addr, instanceProfile.Port, instanceProfile.Mode, instanceProfile.Public, instanceProfile.PasswordAuth, instanceProfile.Driver) -} +`, profile.Version, profile.Data, profile.DSN, profile.Addr, profile.Port, profile.Mode, profile.Public, profile.PasswordAuth, profile.Driver) -func printGreetings() { print(greetingBanner) - if len(instanceProfile.Addr) == 0 { - fmt.Printf("Version %s has been started on port %d\n", instanceProfile.Version, instanceProfile.Port) + 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", instanceProfile.Version, instanceProfile.Addr, instanceProfile.Port) + 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: @@ -197,8 +191,7 @@ See more in: } func main() { - err := Execute() - if err != nil { + if err := rootCmd.Execute(); err != nil { panic(err) } } diff --git a/scripts/.air.toml b/scripts/.air.toml index 110e099aee1f3..761b408bb63c7 100644 --- a/scripts/.air.toml +++ b/scripts/.air.toml @@ -2,7 +2,7 @@ root = "." tmp_dir = ".air" [build] -bin = "./.air/memos --mode dev" +bin = "./.air/memos --mode dev --public true" cmd = "go build -o ./.air/memos ./bin/memos/main.go" delay = 1000 exclude_dir = [".air", "web", "build"] diff --git a/server/router/api/v1/memo_service.go b/server/router/api/v1/memo_service.go index 5a7a5c57adc9b..2a638447bf5ca 100644 --- a/server/router/api/v1/memo_service.go +++ b/server/router/api/v1/memo_service.go @@ -772,11 +772,6 @@ func (s *APIV1Service) convertMemoFromStore(ctx context.Context, memo *store.Mem displayTs = memo.UpdatedTs } - creator, err := s.Store.GetUser(ctx, &store.FindUser{ID: &memo.CreatorID}) - if err != nil { - return nil, errors.Wrap(err, "failed to get creator") - } - name := fmt.Sprintf("%s%d", MemoNamePrefix, memo.ID) listMemoRelationsResponse, err := s.ListMemoRelations(ctx, &v1pb.ListMemoRelationsRequest{Name: name}) if err != nil { @@ -807,7 +802,7 @@ func (s *APIV1Service) convertMemoFromStore(ctx context.Context, memo *store.Mem Name: name, Uid: memo.UID, RowStatus: convertRowStatusFromStore(memo.RowStatus), - Creator: fmt.Sprintf("%s%d", UserNamePrefix, creator.ID), + Creator: fmt.Sprintf("%s%d", UserNamePrefix, memo.CreatorID), CreateTime: timestamppb.New(time.Unix(memo.CreatedTs, 0)), UpdateTime: timestamppb.New(time.Unix(memo.UpdatedTs, 0)), DisplayTime: timestamppb.New(time.Unix(displayTs, 0)),