Skip to content

Commit

Permalink
add dev mode and clean bigcache
Browse files Browse the repository at this point in the history
  • Loading branch information
steviebps committed Nov 21, 2023
1 parent 168ca8f commit 55ca37e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
10 changes: 10 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"strconv"
"time"

"github.com/steviebps/realm/utils"
)

Expand All @@ -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"`
Expand Down
34 changes: 23 additions & 11 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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 {
Expand All @@ -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())
Expand All @@ -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)
}
12 changes: 7 additions & 5 deletions pkg/storage/bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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():
Expand All @@ -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():
Expand Down Expand Up @@ -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)))
}
}

Expand All @@ -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
}

0 comments on commit 55ca37e

Please sign in to comment.