providers

package
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StreamContextTaggerInterceptor

func StreamContextTaggerInterceptor(tagger TagsProvider) func(srv any, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error

StreamContextTaggerInterceptor is a grpc stream interceptor that tags the inbound context

func StreamLoggingInterceptor

func StreamLoggingInterceptor(logger Logger) grpc.StreamServerInterceptor

StreamLoggingInterceptor adds a stream logging interceptor to the server

func StreamMetricsInterceptor

func StreamMetricsInterceptor(metrics Metrics) grpc.StreamServerInterceptor

StreamMetricsInterceptor returns a grpc stream server interceptor that records metrics

func UnaryContextTaggerInterceptor

func UnaryContextTaggerInterceptor(tagger TagsProvider) func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error)

UnaryContextTaggerInterceptor is a grpc unary interceptor that tags the inbound context

func UnaryLoggingInterceptor

func UnaryLoggingInterceptor(logger Logger) grpc.UnaryServerInterceptor

UnaryLoggingInterceptor adds a unary logging interceptor to the server

func UnaryMetricsInterceptor

func UnaryMetricsInterceptor(metrics Metrics) grpc.UnaryServerInterceptor

UnaryMetricsInterceptor returns a grpc unary server interceptor that collects metrics

func WithTags

func WithTags(ctx context.Context, tags Tags) context.Context

WithTags adds tags to the context

Types

type All

type All struct {
	// Logger is the registered logger provider
	Logger Logger
	// Database is the registered database provider
	Database Database
	// Cache is the registered cache provider
	Cache Cache
	// Stream is the registered stream provider
	Stream Stream
	// Metrics is the registered metrics provider
	Metrics Metrics
	// Tagger is the registered tagger provider
	Email Emailer
	// PaymentProcessor is the registered payment processor provider
	PaymentProcessor PaymentProcessor
	// Storage is the registered storage provider
	Storage Storage
}

All is a struct that contains all providers

type Cache

type Cache interface {
	// Get returns a value from the cache by key
	Get(ctx context.Context, key string) (string, error)
	// Set sets a value in the cache by key
	Set(ctx context.Context, key string, value string, ttl time.Duration) error
	// Delete deletes a value from the cache by key
	Delete(ctx context.Context, key string) error
	// Lock locks a key in the cache for a given ttl. It returns true if the lock was acquired, false otherwise.
	Lock(ctx context.Context, key string, ttl time.Duration) (bool, error)
	// Unlock unlocks a key in the cache
	Unlock(ctx context.Context, key string, ttl time.Duration) error
	// Once runs a function once for a given key. It returns true if the function was run, false otherwise.
	Once(ctx context.Context, key string, ttl time.Duration, fn func(ctx context.Context) error) (bool, error)
}

Cache is an interface for caching data

type Card added in v0.9.1

type Card struct {
	ID        string    `json:"id"`
	Brand     string    `json:"brand"`
	Last4     string    `json:"last4"`
	ExpM      int64     `json:"exp_month"`
	ExpY      int64     `json:"exp_year"`
	IsDefault bool      `json:"is_default"`
	CreatedAt time.Time `json:"created_at"`
}

Card is a struct for a customer's card

type Charge added in v0.9.1

type Charge struct {
	ID          string    `json:"id"`
	CustomerID  string    `json:"customer_id"`
	CardID      string    `json:"card_id"`
	Amount      int64     `json:"amount"`
	Description string    `json:"description"`
	Status      string    `json:"status"`
	CreatedAt   time.Time `json:"created_at"`
}

Charge is a struct for charging a customer

type Customer added in v0.9.1

type Customer struct {
	ID            string            `json:"id"`
	Name          string            `json:"name"`
	Phone         string            `json:"phone"`
	Email         string            `json:"email"`
	Description   string            `json:"description"`
	DefaultCardID string            `json:"default_card_id"`
	Metadata      map[string]string `json:"metadata"`
	CreatedAt     time.Time         `json:"created_at"`
	UpdatedAt     time.Time         `json:"updated_at"`
}

Customer is a struct for creating a customer

type Database

type Database interface {
	// Migrate runs the database migrations
	Migrate(ctx context.Context) error
	// Close closes the database connection
	Close() error
}

Database is an interface that represents a database client. It is used to run migrations and close the connection. Type casting is required to use the client.

type Email added in v0.9.1

type Email struct {
	From        string   `json:"from"`
	To          []string `json:"to"`
	Subject     string   `json:"subject"`
	Body        string   `json:"body"`
	ContentType string   `json:"content_type"`
}

Email is a struct for sending emails

type Emailer added in v0.9.1

type Emailer interface {
	// SendEmail sends an email
	SendEmail(ctx context.Context, email *Email) error
}

Emailer is an interface that represents an email client

type Logger

type Logger interface {
	// Info logs an info message
	Info(ctx context.Context, msg string, tags ...map[string]any)
	// Error logs an error message
	Error(ctx context.Context, msg string, tags ...map[string]any)
	// Warn logs a warning message
	Warn(ctx context.Context, msg string, tags ...map[string]any)
	// Debug logs a debug message
	Debug(ctx context.Context, msg string, tags ...map[string]any)
}

Logger is an interface for logging

type MessageHandler

type MessageHandler func(ctx context.Context, message map[string]any) bool

MessageHandler is a function that handles a message from a stream

type Metrics

type Metrics interface {
	// Inc increments the value in a gauge
	Inc(name string, labels ...string)
	// Dec decrements the value in a gauge
	Dec(name string, labels ...string)
	// Observe records the value in a histogram
	Observe(name string, value float64, labels ...string)
	// Set sets the value in a gauge
	Set(name string, value float64, labels ...string)
	// RegisterHistogram registers a new histogram with the given name and labels
	RegisterHistogram(name string, labels ...string)
	// RegisterGauge registers a new gauge with the given name and labels
	RegisterGauge(name string, labels ...string)
}

Metrics is an interface for collecting metrics

type PaymentProcessor added in v0.9.1

type PaymentProcessor interface {
	// CreateCustomer creates a customer
	CreateCustomer(ctx context.Context, charge *Customer) (*Customer, error)
	// UpdateCustomer updates a customer
	UpdateCustomer(ctx context.Context, charge *Customer) (*Customer, error)
	// GetCustomer gets a customer
	GetCustomer(ctx context.Context, id string) (*Customer, error)
	// DeleteCustomer deletes a customer
	DeleteCustomer(ctx context.Context, id string) error
	// ListCustomers lists customers
	ListCustomers(ctx context.Context, after string, limit int64) ([]*Customer, error)
	// CreateCard creates a card for a customer
	CreateCard(ctx context.Context, customerID string, token string) (*Card, error)
	// GetCard gets a card for a customer
	GetCard(ctx context.Context, customerID string, cardID string) (*Card, error)
	// DeleteCard deletes a card for a customer
	DeleteCard(ctx context.Context, customerID string, cardID string) error
	// ListCards lists cards for a customer
	ListCards(ctx context.Context, customerID string) ([]*Card, error)
	// CreateCharge creates a charge
	CreateCharge(ctx context.Context, charge *Charge) (*Charge, error)
	// GetCharge gets a charge
	GetCharge(ctx context.Context, id string) (*Charge, error)
	// ListCharges lists charges
	ListCharges(ctx context.Context, customerID, after string, limit int64) ([]*Charge, error)
}

PaymentProcessor is a function that returns a PaymentProcessor implementation

type Storage added in v0.9.4

type Storage interface {
	// WriteFile writes a file to the storage provider
	WriteFile(ctx context.Context, path string, r io.ReadSeeker) error
	// ReadFile reads a file from the storage provider
	ReadFile(ctx context.Context, path string, w io.Writer) error
	// DeleteFile deletes a file
	DeleteFile(ctx context.Context, path string) error
}

Storage is an interface for a file storage provider

type Stream

type Stream interface {
	// Publish publishes a message to the stream
	Publish(ctx context.Context, topic string, message map[string]any) error
	// Subscribe subscribes to a topic
	Subscribe(ctx context.Context, topic string, consumer string, handler MessageHandler) error
	// AsyncSubscribe subscribes to a topic and handles messages asynchronously
	AsyncSubscribe(ctx context.Context, topic string, consumer string, handler MessageHandler) error
}

Stream is an interface for a stream

type Tags

type Tags interface {
	// LogTags returns the tags for logging
	LogTags() map[string]any
	// WithMetadata adds metadata to the tags
	WithMetadata(md metadata.MD) Tags
	// WithMethod adds the grpc method to the tags
	WithMethod(method string) Tags
	// WithContextID adds the contextID to the tags
	WithContextID(contextID string) Tags
	// WithError adds the error to the tags
	WithError(err error) Tags
	// GetMetadata returns the metadata from the tags
	GetMetadata() (metadata.MD, bool)
	// GetMethod returns the grpc method from the tags
	GetMethod() (string, bool)
	// GetContextID returns the contextID from the tags
	GetContextID() (string, bool)
	// GetError returns the error from the tags
	GetError() (error, bool)
	// Set sets the value for the given key
	Set(key string, value any) Tags
	// Get returns the value for the given key
	Get(key string) (any, bool)
}

Tags is an interface for tagging contexts

func GetTags

func GetTags(ctx context.Context) (Tags, bool)

GetTags returns the tags from the context

type TagsProvider added in v0.11.0

type TagsProvider func() Tags

TagsProvider is a function that returns a new Tags instance

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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