services

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2024 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheMiss = errors.New("cache miss")

ErrCacheMiss indicates that the requested key does not exist in the cache

Functions

This section is empty.

Types

type AuthClient

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

AuthClient is the client that handles authentication requests

func NewAuthClient

func NewAuthClient(cfg *config.Config, orm *ent.Client) *AuthClient

NewAuthClient creates a new authentication client

func (*AuthClient) CheckPassword

func (c *AuthClient) CheckPassword(password, hash string) error

CheckPassword check if a given password matches a given hash

func (*AuthClient) DeletePasswordTokens

func (c *AuthClient) DeletePasswordTokens(ctx echo.Context, userID int) error

DeletePasswordTokens deletes all password tokens in the database for a belonging to a given user. This should be called after a successful password reset.

func (*AuthClient) GenerateEmailVerificationToken

func (c *AuthClient) GenerateEmailVerificationToken(email string) (string, error)

GenerateEmailVerificationToken generates an email verification token for a given email address using JWT which is set to expire based on the duration stored in configuration

func (*AuthClient) GeneratePasswordResetToken

func (c *AuthClient) GeneratePasswordResetToken(ctx echo.Context, userID int) (string, *ent.PasswordToken, error)

GeneratePasswordResetToken generates a password reset token for a given user. For security purposes, the token itself is not stored in the database but rather a hash of the token, exactly how passwords are handled. This method returns both the generated token as well as the token entity which only contains the hash.

func (*AuthClient) GetAuthenticatedUser

func (c *AuthClient) GetAuthenticatedUser(ctx echo.Context) (*ent.User, error)

GetAuthenticatedUser returns the authenticated user if the user is logged in

func (*AuthClient) GetAuthenticatedUserID

func (c *AuthClient) GetAuthenticatedUserID(ctx echo.Context) (int, error)

GetAuthenticatedUserID returns the authenticated user's ID, if the user is logged in

func (*AuthClient) GetValidPasswordToken

func (c *AuthClient) GetValidPasswordToken(ctx echo.Context, userID, tokenID int, token string) (*ent.PasswordToken, error)

GetValidPasswordToken returns a valid, non-expired password token entity for a given user, token ID and token. Since the actual token is not stored in the database for security purposes, if a matching password token entity is found a hash of the provided token is compared with the hash stored in the database in order to validate.

func (*AuthClient) HashPassword

func (c *AuthClient) HashPassword(password string) (string, error)

HashPassword returns a hash of a given password

func (*AuthClient) Login

func (c *AuthClient) Login(ctx echo.Context, userID int) error

Login logs in a user of a given ID

func (*AuthClient) Logout

func (c *AuthClient) Logout(ctx echo.Context) error

Logout logs the requesting user out

func (*AuthClient) RandomToken

func (c *AuthClient) RandomToken(length int) (string, error)

RandomToken generates a random token string of a given length

func (*AuthClient) ValidateEmailVerificationToken

func (c *AuthClient) ValidateEmailVerificationToken(token string) (string, error)

ValidateEmailVerificationToken validates an email verification token and returns the associated email address if the token is valid and has not expired

type CacheClient

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

CacheClient is the client that allows you to interact with the cache

func NewCacheClient

func NewCacheClient(store CacheStore) *CacheClient

NewCacheClient creates a new cache client

func (*CacheClient) Close

func (c *CacheClient) Close()

Close closes the connection to the cache

func (*CacheClient) Flush

func (c *CacheClient) Flush() *CacheFlushOp

Flush creates a cache flush operation

func (*CacheClient) Get

func (c *CacheClient) Get() *CacheGetOp

Get creates a cache get operation

func (*CacheClient) Set

func (c *CacheClient) Set() *CacheSetOp

Set creates a cache set operation

type CacheFlushOp added in v0.15.0

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

CacheFlushOp handles chaining a flush operation

func (*CacheFlushOp) Execute added in v0.15.0

func (c *CacheFlushOp) Execute(ctx context.Context) error

Execute flushes the data from the cache

func (*CacheFlushOp) Group added in v0.15.0

func (c *CacheFlushOp) Group(group string) *CacheFlushOp

Group sets the cache group

func (*CacheFlushOp) Key added in v0.15.0

func (c *CacheFlushOp) Key(key string) *CacheFlushOp

Key sets the cache key

func (*CacheFlushOp) Tags added in v0.15.0

func (c *CacheFlushOp) Tags(tags ...string) *CacheFlushOp

Tags sets the cache tags

type CacheGetOp added in v0.15.0

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

CacheGetOp handles chaining a get operation

func (*CacheGetOp) Fetch added in v0.15.0

func (c *CacheGetOp) Fetch(ctx context.Context) (any, error)

Fetch fetches the data from the cache

func (*CacheGetOp) Group added in v0.15.0

func (c *CacheGetOp) Group(group string) *CacheGetOp

Group sets the cache group

func (*CacheGetOp) Key added in v0.15.0

func (c *CacheGetOp) Key(key string) *CacheGetOp

Key sets the cache key

type CacheSetOp added in v0.15.0

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

CacheSetOp handles chaining a set operation

func (*CacheSetOp) Data added in v0.15.0

func (c *CacheSetOp) Data(data any) *CacheSetOp

Data sets the data to cache

func (*CacheSetOp) Expiration added in v0.15.0

func (c *CacheSetOp) Expiration(expiration time.Duration) *CacheSetOp

Expiration sets the expiration duration of the cached data

func (*CacheSetOp) Group added in v0.15.0

func (c *CacheSetOp) Group(group string) *CacheSetOp

Group sets the cache group

func (*CacheSetOp) Key added in v0.15.0

func (c *CacheSetOp) Key(key string) *CacheSetOp

Key sets the cache key

func (*CacheSetOp) Save added in v0.15.0

func (c *CacheSetOp) Save(ctx context.Context) error

Save saves the data in the cache

func (*CacheSetOp) Tags added in v0.15.0

func (c *CacheSetOp) Tags(tags ...string) *CacheSetOp

Tags sets the cache tags

type CacheStore added in v0.15.0

type CacheStore interface {
	// contains filtered or unexported methods
}

CacheStore provides an interface for cache storage

type CachedPage added in v0.13.0

type CachedPage struct {
	// URL stores the URL of the requested page
	URL string

	// HTML stores the complete HTML of the rendered Page
	HTML []byte

	// StatusCode stores the HTTP status code
	StatusCode int

	// Headers stores the HTTP headers
	Headers map[string]string
}

CachedPage is what is used to store a rendered Page in the cache

type Container

type Container struct {
	// Validator stores a validator
	Validator *Validator

	// Web stores the web framework
	Web *echo.Echo

	// Config stores the application configuration
	Config *config.Config

	// Cache contains the cache client
	Cache *CacheClient

	// Database stores the connection to the database
	Database *sql.DB

	// ORM stores a client to the ORM
	ORM *ent.Client

	// Mail stores an email sending client
	Mail *MailClient

	// Auth stores an authentication client
	Auth *AuthClient

	// TemplateRenderer stores a service to easily render and cache templates
	TemplateRenderer *TemplateRenderer

	// Tasks stores the task client
	Tasks *backlite.Client
}

Container contains all services used by the application and provides an easy way to handle dependency injection including within tests

func NewContainer

func NewContainer() *Container

NewContainer creates and initializes a new Container

func (*Container) Shutdown

func (c *Container) Shutdown() error

Shutdown shuts the Container down and disconnects all connections. If the task runner was started, cancel the context to shut it down prior to calling this.

type InvalidPasswordTokenError

type InvalidPasswordTokenError struct{}

InvalidPasswordTokenError is an error returned when an invalid token is provided

func (InvalidPasswordTokenError) Error

Error implements the error interface.

type MailClient

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

MailClient provides a client for sending email This is purposely not completed because there are many different methods and services for sending email, many of which are very different. Choose what works best for you and populate the methods below

func NewMailClient

func NewMailClient(cfg *config.Config, templates *TemplateRenderer) (*MailClient, error)

NewMailClient creates a new MailClient

func (*MailClient) Compose

func (m *MailClient) Compose() *mail

Compose creates a new email

type NotAuthenticatedError

type NotAuthenticatedError struct{}

NotAuthenticatedError is an error returned when a user is not authenticated

func (NotAuthenticatedError) Error

func (e NotAuthenticatedError) Error() string

Error implements the error interface.

type TemplateParsed

type TemplateParsed struct {
	// Template is the parsed template
	Template *template.Template
	// contains filtered or unexported fields
}

TemplateParsed is a wrapper around parsed templates which are stored in the TemplateRenderer cache

func (*TemplateParsed) Execute

func (t *TemplateParsed) Execute(data any) (*bytes.Buffer, error)

Execute executes a template with the given data and provides the output

type TemplateRenderer

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

TemplateRenderer provides a flexible and easy to use method of rendering simple templates or complex sets of templates while also providing caching and/or hot-reloading depending on your current environment

func NewTemplateRenderer

func NewTemplateRenderer(cfg *config.Config, cache *CacheClient, fm template.FuncMap) *TemplateRenderer

NewTemplateRenderer creates a new TemplateRenderer

func (*TemplateRenderer) GetCachedPage added in v0.13.0

func (t *TemplateRenderer) GetCachedPage(ctx echo.Context, url string) (*CachedPage, error)

GetCachedPage attempts to fetch a cached page for a given URL

func (*TemplateRenderer) Load

func (t *TemplateRenderer) Load(group, key string) (*TemplateParsed, error)

Load loads a template from the cache

func (*TemplateRenderer) Parse

func (t *TemplateRenderer) Parse() *templateBuilder

Parse creates a template build operation

func (*TemplateRenderer) RenderPage added in v0.13.0

func (t *TemplateRenderer) RenderPage(ctx echo.Context, page page.Page) error

RenderPage renders a Page as an HTTP response

type Validator

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

Validator provides validation mainly validating structs within the web context

func NewValidator

func NewValidator() *Validator

NewValidator creats a new Validator

func (*Validator) Validate

func (v *Validator) Validate(i any) error

Validate validates a struct

Jump to

Keyboard shortcuts

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