store

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RedisStatusOkay              = "OK"
	DefaultRedisConnectionString = "redis://localhost:6379"
	DefaultTTL                   = 0
)

Const

View Source
const (
	ErrTokenTypeNotString   = "token of type string required"
	DefaultUnredactedLength = 4
	// DefaultRedactedLength ensures infomation about the actual length of the encoded token isn't exposed to an unauthenticated client
	DefaultRedactedLength = 10
	DefaultRedactedToken  = "*"
)

Variables

View Source
var (
	ErrWithOperation    = "operation completed with error: %s\n"
	OperationSuccessful = "operation successful"
)

Functions

func InterfaceIsString

func InterfaceIsString(a any) (bool, string)

InterfaceIsString checks if an interface underlying type is a string

func IsValidFile

func IsValidFile(loc string, log *zerolog.Logger) error

IsValidFile checks if a given file address is valid

func Redact

func Redact(s string) string

Redact censors sensitive token before printing them in logs or as a response.

Types

type File

type File struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewFile

func NewFile(loc string, logger *vlog.Logger) *File

NewFile creates a new filestore at loc

func (*File) Close

func (f *File) Close(ctx context.Context) error

Close closes an open file

func (*File) Connect

func (f *File) Connect(ctx context.Context) (bool, error)

Connect attempts to open a filestream to the file at location loc

func (*File) Delete

func (f *File) Delete(ctx context.Context, id string) (bool, error)

Delete removes a token from the file store

func (*File) Flush

func (f *File) Flush(ctx context.Context) (bool, error)

Flush cleans al the data from a file store

func (*File) Loop

func (f *File) Loop()

func (*File) Patch

func (f *File) Patch(ctx context.Context, id string, token any) (bool, error)

Patch only updates a token in the file store, identified by id

func (*File) Retrieve

func (f *File) Retrieve(ctx context.Context, id string) (string, error)

Retrieve retrieves a token from the store identified by id

func (*File) RetrieveAll

func (f *File) RetrieveAll(ctx context.Context) (map[string]string, error)

RetrieveAll retrieves all the tokens from the store

func (*File) Store

func (f *File) Store(ctx context.Context, id string, token any) error

Store persists a new key-value entry in the file store

func (*File) Write

func (f *File) Write(m map[string]string) error

Write persists the map to disk using godotenv.Write

type FileChannels

type FileChannels struct {
	// contains filtered or unexported fields
}

type Gob

type Gob struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewGob

func NewGob(ctx context.Context, loc string, logger *vlog.Logger, trunc bool) (*Gob, error)

func (*Gob) Close

func (g *Gob) Close(ctx context.Context) error

Close closes the redis connection

func (*Gob) Connect

func (g *Gob) Connect(ctx context.Context) (bool, error)

func (*Gob) Delete

func (g *Gob) Delete(ctx context.Context, id string) (bool, error)

func (*Gob) Flush

func (g *Gob) Flush(ctx context.Context) (bool, error)

Flush empties the internal sync.Map and the persistent gob store // TODO: why? What is the use case for this? TODO: Flush should rather persist the current state of the in-memory map into disk, and then empty the in-memory map. It isn't idiomatic for flush to clear the persistent store too.

func (*Gob) MapDump

func (g *Gob) MapDump(ctx context.Context) error

MapDump persists the current in-memory data to the persistent store

func (*Gob) MapRefresh

func (g *Gob) MapRefresh(ctx context.Context) error

MapRefresh refreshes the sync.Map in-memory store with the latest updates from the persistent store

func (*Gob) Patch

func (g *Gob) Patch(ctx context.Context, id string, token any) (bool, error)

func (*Gob) Retrieve

func (g *Gob) Retrieve(ctx context.Context, id string) (string, error)

func (*Gob) RetrieveAll

func (g *Gob) RetrieveAll(ctx context.Context) (map[string]string, error)

RetrieveAll

func (*Gob) Store

func (g *Gob) Store(ctx context.Context, id string, token any) error

type Map

type Map struct {
	// contains filtered or unexported fields
}

func NewSyncMap

func NewSyncMap(ctx context.Context, logger *vlog.Logger) *Map

func (*Map) Close

func (m *Map) Close(ctx context.Context) error

func (*Map) Connect

func (m *Map) Connect(ctx context.Context) (bool, error)

func (*Map) Delete

func (m *Map) Delete(ctx context.Context, id string) (bool, error)

func (*Map) Flush

func (m *Map) Flush(ctx context.Context) (bool, error)

func (*Map) IsExist

func (m *Map) IsExist(key string) bool

IsExist checks whether a key exists in a sync map

func (*Map) Map

func (m *Map) Map() *sync.Map

Map returns the pointer to the inner syncMap structure for external extendability within the store package

func (*Map) Patch

func (m *Map) Patch(ctx context.Context, id string, token any) (bool, error)

func (*Map) Retrieve

func (m *Map) Retrieve(ctx context.Context, id string) (string, error)

func (*Map) RetrieveAll

func (m *Map) RetrieveAll(ctx context.Context) (map[string]string, error)

func (*Map) Store

func (m *Map) Store(ctx context.Context, id string, token any) error

type Options

type Options func(*Redis) error

Options implements funcitonal options for passing in Redis configuration options

func WithConnectionString

func WithConnectionString(s string) Options

WithConnectionString switches the mode of connection to redis to using

type Redis

type Redis struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Redis holds the config options and the state of the redis connection through the lifetime of the connection.

func NewRedis

func NewRedis(connectionString string, logger *vlog.Logger) (*Redis, error)

NewRedis creates a new instance of Redis, for managing CRUD operations to the configured redis client

func (*Redis) Client

func (r *Redis) Client() *redis.Client

Client returns the Redis client to the caller

func (*Redis) Close

func (r *Redis) Close(ctx context.Context) error

Close closes the redis connection

func (*Redis) Connect

func (r *Redis) Connect(ctx context.Context) (bool, error)

func (*Redis) Delete

func (r *Redis) Delete(ctx context.Context, id string) (bool, error)

Delete deletes a key/value pair identified by key

func (*Redis) Flush

func (r *Redis) Flush(ctx context.Context) (bool, error)

Flush clears the content of the current redis DB

func (*Redis) IsExists

func (r *Redis) IsExists(ctx context.Context, key string) bool

IsExists checks if a key already exists in redis

func (*Redis) Patch

func (r *Redis) Patch(ctx context.Context, id string, token any) (bool, error)

Patch replaces the value of a key in the redis DB

func (*Redis) Ping

func (r *Redis) Ping(ctx context.Context) (bool, error)

Ping sends a ping message to the redis server to check the connection health

func (*Redis) Retrieve

func (r *Redis) Retrieve(ctx context.Context, id string) (string, error)

Retrieve retrieves a key/value pair from the database.

func (*Redis) RetrieveAll

func (r *Redis) RetrieveAll(ctx context.Context) (map[string]string, error)

RetrieveAll retrieves all the key/value pairs currently stored in the database.

func (*Redis) Store

func (r *Redis) Store(ctx context.Context, id string, token any) (err error)

Store stores a key/value pair in the database.

type Store

type Store interface {
	Connect(ctx context.Context) (bool, error)
	// Store persists the key value pair to the store. It ensures that it doesn't already exist; if it already does, it aborts.
	Store(ctx context.Context, id string, token any) error
	Retrieve(ctx context.Context, id string) (string, error)
	RetrieveAll(ctx context.Context) (map[string]string, error)
	Delete(ctx context.Context, id string) (bool, error)
	Patch(ctx context.Context, id string, token any) (bool, error)
	Flush(ctx context.Context) (bool, error)
	Close(ctx context.Context) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL