storage

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("closed")

Functions

func IsValidUUID

func IsValidUUID(id string) bool

IsValidUUID checks if passed string is valid UUID v4.

func NewUUID

func NewUUID() string

NewUUID generates new UUID v4.

Types

type InMemory

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

func NewInMemory

func NewInMemory(sessionTTL time.Duration, maxRequests uint16, cleanup ...time.Duration) *InMemory

NewInMemory creates inmemory storage.

func (*InMemory) Close

func (s *InMemory) Close() error

Close current storage with data invalidation.

func (*InMemory) CreateRequest

func (s *InMemory) CreateRequest(sessionUUID, clientAddr, method, uri string, content []byte, headers map[string]string) (string, error)

CreateRequest creates new request in storage using passed data and updates expiration time for session and all stored requests for the session.

func (*InMemory) CreateSession

func (s *InMemory) CreateSession(content []byte, code uint16, contentType string, delay time.Duration, sessionUUID ...string) (string, error)

CreateSession creates new session in storage using passed data.

func (*InMemory) DeleteRequest

func (s *InMemory) DeleteRequest(sessionUUID, requestUUID string) (bool, error)

DeleteRequest deletes stored request with passed session and request UUIDs.

func (*InMemory) DeleteRequests

func (s *InMemory) DeleteRequests(uuid string) (bool, error)

DeleteRequests deletes stored requests for session with passed UUID.

func (*InMemory) DeleteSession

func (s *InMemory) DeleteSession(uuid string) (bool, error)

DeleteSession deletes session with passed UUID.

func (*InMemory) GetAllRequests

func (s *InMemory) GetAllRequests(sessionUUID string) ([]Request, error)

GetAllRequests returns all request as a slice of structures.

func (*InMemory) GetRequest

func (s *InMemory) GetRequest(sessionUUID, requestUUID string) (Request, error)

GetRequest returns request data.

func (*InMemory) GetSession

func (s *InMemory) GetSession(uuid string) (Session, error)

GetSession returns session data.

type Redis

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

Redis is redis storage implementation.

func NewRedis

func NewRedis(ctx context.Context, rdb *redis.Client, sessionTTL time.Duration, maxRequests uint16) *Redis

NewRedis creates new redis storage instance.

func (*Redis) CreateRequest

func (s *Redis) CreateRequest(sessionUUID, clientAddr, method, uri string, content []byte, headers map[string]string) (string, error)

CreateRequest creates new request in storage using passed data and updates expiration time for session and all stored requests for the session.

func (*Redis) CreateSession

func (s *Redis) CreateSession(content []byte, code uint16, contentType string, delay time.Duration, sessionUUID ...string) (string, error)

CreateSession creates new session in storage using passed data.

func (*Redis) DeleteRequest

func (s *Redis) DeleteRequest(sessionUUID, requestUUID string) (bool, error)

DeleteRequest deletes stored request with passed session and request UUIDs.

func (*Redis) DeleteRequests

func (s *Redis) DeleteRequests(sessionUUID string) (bool, error)

DeleteRequests deletes stored requests for session with passed UUID.

func (*Redis) DeleteSession

func (s *Redis) DeleteSession(uuid string) (bool, error)

DeleteSession deletes session with passed UUID.

func (*Redis) GetAllRequests

func (s *Redis) GetAllRequests(sessionUUID string) ([]Request, error)

GetAllRequests returns all request as a slice of structures.

func (*Redis) GetRequest

func (s *Redis) GetRequest(sessionUUID, requestUUID string) (Request, error)

GetRequest returns request data.

func (*Redis) GetSession

func (s *Redis) GetSession(uuid string) (Session, error)

GetSession returns session data.

type Request

type Request interface {
	UUID() string               // UUID returns unique request identifier.
	ClientAddr() string         // ClientAddr returns client hostname or IP address (who sent this request).
	Method() string             // Method returns HTTP method name (eg.: 'GET', 'POST').
	Content() []byte            // Content returns request body (payload).
	Headers() map[string]string // Headers returns HTTP request headers.
	URI() string                // URI returns Uniform Resource Identifier.
	CreatedAt() time.Time       // CreatedAt returns creation time (accuracy to seconds).
}

Request describes recorded request and additional meta-data.

type Session

type Session interface {
	UUID() string         // UUID returns unique session identifier.
	Content() []byte      // Content returns session server response content.
	Code() uint16         // Code returns default server response code.
	ContentType() string  // ContentType returns response content type.
	Delay() time.Duration // Delay returns delay before response sending.
	CreatedAt() time.Time // CreatedAt returns creation time (accuracy to seconds).
}

Session describes session settings (like response data and any additional information).

type Storage

type Storage interface {
	// GetSession returns session data.
	// If session was not found - `nil, nil` will be returned.
	GetSession(uuid string) (Session, error)

	// CreateSession creates new session in storage using passed data.
	// Session UUID without error will be returned on success.
	CreateSession(content []byte, code uint16, contentType string, delay time.Duration, id ...string) (string, error)

	// DeleteSession deletes session with passed UUID.
	DeleteSession(uuid string) (bool, error)

	// DeleteRequests deletes stored requests for session with passed UUID.
	DeleteRequests(uuid string) (bool, error)

	// CreateRequest creates new request in storage using passed data and updates expiration time for session and all
	// stored requests for the session.
	// Session with passed UUID must exist.
	// Request UUID without error will be returned on success.
	CreateRequest(sessionUUID, clientAddr, method, uri string, content []byte, headers map[string]string) (string, error)

	// GetRequest returns request data.
	// If request was not found - `nil, nil` will be returned.
	GetRequest(sessionUUID, requestUUID string) (Request, error)

	// GetAllRequests returns all request as a slice of structures.
	// If requests was not found - `nil, nil` will be returned.
	GetAllRequests(sessionUUID string) ([]Request, error)

	// DeleteRequest deletes stored request with passed session and request UUIDs.
	DeleteRequest(sessionUUID, requestUUID string) (bool, error)
}

Storage is a Session's and Request's storage.

Jump to

Keyboard shortcuts

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