Skip to content

Commit

Permalink
chore: fix env init
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Jul 27, 2024
1 parent 8fd5365 commit 7564c40
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 44 deletions.
67 changes: 30 additions & 37 deletions bin/memos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -77,7 +90,7 @@ var (
}
}

printGreetings()
printGreetings(instanceProfile)

go func() {
<-c
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -197,8 +191,7 @@ See more in:
}

func main() {
err := Execute()
if err != nil {
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}
2 changes: 1 addition & 1 deletion scripts/.air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
7 changes: 1 addition & 6 deletions server/router/api/v1/memo_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)),
Expand Down

0 comments on commit 7564c40

Please sign in to comment.