From 55ca37ecb206bb7905fa0d7e8536718cc16f467d Mon Sep 17 00:00:00 2001 From: Stephen Wodecki Date: Mon, 20 Nov 2023 20:29:42 -0500 Subject: [PATCH] add dev mode and clean bigcache --- cmd/config.go | 10 ++++++++++ cmd/server.go | 34 +++++++++++++++++++++++----------- pkg/storage/bigcache.go | 12 +++++++----- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 0c744ca..d10e1c7 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -1,6 +1,9 @@ package cmd import ( + "strconv" + "time" + "github.com/steviebps/realm/utils" ) @@ -9,6 +12,13 @@ type RealmConfig struct { Server ServerConfig `json:"server,omitempty"` } +func NewDefaultServerConfig() RealmConfig { + return RealmConfig{ + Client: ClientConfig{}, + Server: ServerConfig{StorageType: "bigcache", StorageOptions: map[string]string{"life_window": strconv.FormatInt(int64(time.Hour*24), 10)}, Port: "8080", Inheritable: true}, + } +} + type ServerConfig struct { StorageType string `json:"storage"` StorageOptions map[string]string `json:"options"` diff --git a/cmd/server.go b/cmd/server.go index b88d65e..6d1c6d8 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -17,6 +17,12 @@ var serverCmd = &cobra.Command{ Use: "server", Short: "Starts realm server", Long: "Starts realm server", + PreRun: func(cmd *cobra.Command, args []string) { + devMode, _ := cmd.Flags().GetBool("dev") + if !devMode { + cmd.MarkFlagRequired("config") + } + }, Run: func(cmd *cobra.Command, args []string) { flags := cmd.Flags() debug, _ := flags.GetBool("debug") @@ -40,16 +46,24 @@ var serverCmd = &cobra.Command{ logger.Error(err.Error()) os.Exit(1) } - if configPath == "" { + + devMode, _ := flags.GetBool("dev") + if !devMode && configPath == "" { logger.Error("config must be specified") os.Exit(1) } + var realmConfig RealmConfig - realmConfig, err := parseConfig(configPath) - if err != nil { - logger.Error(err.Error()) - os.Exit(1) + if devMode { + realmConfig = NewDefaultServerConfig() + } else { + realmConfig, err = parseConfig(configPath) + if err != nil { + logger.Error(err.Error()) + os.Exit(1) + } } + serverConfig := realmConfig.Server portStr, err := flags.GetString("port") @@ -90,7 +104,9 @@ var serverCmd = &cobra.Command{ for k, v := range serverConfig.StorageOptions { options = append(options, k, v) } - logger.Debug("Storage options", options...) + if len(options) > 0 { + logger.Debug("Storage options", options...) + } stg, err := strgCreator(serverConfig.StorageOptions) if err != nil { @@ -106,11 +122,6 @@ var serverCmd = &cobra.Command{ } } - if err != nil { - logger.Error(err.Error()) - os.Exit(1) - } - handler, err := realmhttp.NewHandler(realmhttp.HandlerConfig{Storage: stg, Logger: logger, RequestTimeout: 5 * time.Second}) if err != nil { logger.Error(err.Error()) @@ -136,5 +147,6 @@ var serverCmd = &cobra.Command{ func init() { serverCmd.Flags().String("port", "", "port to run server on") + serverCmd.Flags().Bool("dev", false, "run server in dev mode") rootCmd.AddCommand(serverCmd) } diff --git a/pkg/storage/bigcache.go b/pkg/storage/bigcache.go index 869ac00..ebea443 100644 --- a/pkg/storage/bigcache.go +++ b/pkg/storage/bigcache.go @@ -22,6 +22,8 @@ var ( _ Storage = (*BigCacheStorage)(nil) ) +const bigCacheEntryKey string = "bc" + func NewBigCacheStorage(config map[string]string) (Storage, error) { // defaults var shards int = 64 @@ -83,7 +85,7 @@ func (f *BigCacheStorage) Get(ctx context.Context, logicalPath string) (*Storage return nil, err } - path, key := f.expandPath(logicalPath) + path, key := f.expandPath(logicalPath + bigCacheEntryKey) b, err := f.underlying.Get(filepath.Join(path, key)) if err != nil { if errors.Is(err, bigcache.ErrEntryNotFound) { @@ -108,7 +110,7 @@ func (f *BigCacheStorage) Put(ctx context.Context, e StorageEntry) error { if err := ValidatePath(e.Key); err != nil { return err } - path, key := f.expandPath(e.Key) + path, key := f.expandPath(e.Key + bigCacheEntryKey) select { case <-ctx.Done(): @@ -126,7 +128,7 @@ func (f *BigCacheStorage) Delete(ctx context.Context, logicalPath string) error if err := ValidatePath(logicalPath); err != nil { return err } - path, key := f.expandPath(logicalPath) + path, key := f.expandPath(logicalPath + bigCacheEntryKey) select { case <-ctx.Done(): @@ -155,7 +157,7 @@ func (f *BigCacheStorage) List(ctx context.Context, prefix string) ([]string, er } key := record.Key() if strings.HasPrefix(key, prefix) { - names = append(names, strings.TrimPrefix(key, prefix)) + names = append(names, filepath.Dir(strings.TrimPrefix(key, prefix))) } } @@ -175,5 +177,5 @@ func (f *BigCacheStorage) List(ctx context.Context, prefix string) ([]string, er func (f *BigCacheStorage) expandPath(k string) (string, string) { key := filepath.Base(k) path := filepath.Dir(k) - return path, key + return path, "_" + key }