appbase

package
v0.0.0-...-2af46a5 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 22 Imported by: 9

Documentation

Index

Constants

View Source
const ContextDomain = "contextDomain"
View Source
const ContextLoggerName = "contextLogger"
View Source
const ContextMessageId = "contextMessageId"
View Source
const DefaultSeed = "dea42a58-acf4-45af-85bb-e77e94bd5025"

DefaultSeed is a default seed for hex encoded hashed tokens

View Source
const SIG_SHUTDOWN_FOR_TESTS = syscall.Signal(0x42)

Variables

View Source
var IsHexRegex = regexp.MustCompile(`^[a-fA-F0-9]+$`)

Functions

func HashTokenBase64 deprecated

func HashTokenBase64(token string, salt string, secret string) string

Deprecated: Use HashTokenHex. Not sure how we started to use base64 encoding for hash. But for compatibility with previous release we must keep it for a while

func HashTokenHex

func HashTokenHex(token string, salt string, secret string) string

func InitAppConfig

func InitAppConfig[C InstanceConfig](appConfig C, settings *AppSettings) error

Types

type AbstractRepository

type AbstractRepository[T any] struct {
	Service
	// contains filtered or unexported fields
}

func NewAbstractRepository

func NewAbstractRepository[T any](id string, emptyData RepositoryData[T], source RepositoryDataLoader, attempts int, refreshPeriodSec int, cacheDir string) *AbstractRepository[T]

func (*AbstractRepository[T]) ChangesChannel

func (r *AbstractRepository[T]) ChangesChannel() <-chan bool

func (*AbstractRepository[T]) Close

func (r *AbstractRepository[T]) Close() error

func (*AbstractRepository[T]) GetData

func (r *AbstractRepository[T]) GetData() *T

type App

type App[C any] struct {
	// contains filtered or unexported fields
}

func NewApp

func NewApp[C any](appContext Context[C], appSettings *AppSettings) *App[C]

func (*App[C]) Exit

func (a *App[C]) Exit(signal os.Signal)

func (*App[C]) Run

func (a *App[C]) Run()

type AppSettings

type AppSettings struct {
	Name, ConfigPath, ConfigName, ConfigType, EnvPrefix string
}

func (*AppSettings) EnvPrefixWithUnderscore

func (a *AppSettings) EnvPrefixWithUnderscore() string

type CacheTagHeader

type CacheTagHeader string
const (
	HTTPTagLastModified CacheTagHeader = "last-modified"
	HTTPTagETag         CacheTagHeader = "etag"
	HTTPTagNone         CacheTagHeader = ""
)

type Config

type Config struct {
	AppSetting *AppSettings
	// InstanceId ID of bulker instance. It is used for identifying Kafka consumers.
	// If is not set, instance id will be generated and persisted to disk (~/.{appName}/instance_id) and reused on next restart.
	// Default: random uuid
	InstanceId string `mapstructure:"INSTANCE_ID"`

	// HTTPPort port for bulker http server.
	HTTPPort int `mapstructure:"HTTP_PORT"`

	// AuthTokens A list of hashed auth tokens that authorizes user in HTTP interface separated by comma. Each must have format:
	// `${salt}.${hash}` where `${salt}` should be random string. Hash is `base64(sha512($token + $salt + TokenSecrets)`.
	// `$token` must consist only of letters, digits, underscore and dash
	AuthTokens string `mapstructure:"AUTH_TOKENS"`
	// RawAuthTokens plain non hashed tokens separated by comma. Each token must consist only of letters, digits, underscore and dash
	RawAuthTokens string `mapstructure:"RAW_AUTH_TOKENS"`

	// See AuthTokens
	TokenSecrets string `mapstructure:"TOKEN_SECRET"`

	// LogFormat log format. Can be `text` or `json`. Default: `text`
	LogFormat string `mapstructure:"LOG_FORMAT"`
}

func (*Config) PostInit

func (c *Config) PostInit(settings *AppSettings) error

type Context

type Context[C any] interface {
	InitContext(settings *AppSettings) error
	ShutdownSignal() error
	Cleanup() error
	Config() *C
	Server() *http.Server
}

type HTTPRepository

type HTTPRepository[T any] struct {
	*AbstractRepository[T]
	// contains filtered or unexported fields
}

func NewHTTPRepository

func NewHTTPRepository[T any](id, url, token string, tagHeader CacheTagHeader, emptyData RepositoryData[T], attempts int, refreshPeriodSec int, cacheDir string) *HTTPRepository[T]

type InstanceConfig

type InstanceConfig interface {
	PostInit(settings *AppSettings) error
}

type Repository

type Repository[T any] interface {
	io.Closer
	GetData() *T
	ChangesChannel() <-chan bool
}

type RepositoryData

type RepositoryData[D any] interface {
	Init(reader io.Reader, tag any) error
	GetData() *D
	Store(closer io.Writer) error
}

type RepositoryDataLoader

type RepositoryDataLoader func(tag any) (reader io.ReadCloser, newTag any, modified bool, err error)

RepositoryDataLoader loads data from external source. tag can be used for etag or last modified handling

type Router

type Router struct {
	Service
	// contains filtered or unexported fields
}

func NewRouterBase

func NewRouterBase(config Config, noAuthPaths []string) *Router

func (*Router) Engine

func (r *Router) Engine() *gin.Engine

Engine returns gin router

func (*Router) ResponseError

func (r *Router) ResponseError(c *gin.Context, code int, errorType string, maskError bool, err error, sendResponse bool) *RouterError

func (*Router) ShouldCompress

func (r *Router) ShouldCompress(req *http.Request) bool

type RouterError

type RouterError struct {
	Error       error
	PublicError error
	ErrorType   string
}

type Service

type Service struct {
	// ID is used as [ID] prefix in log and error messages
	ID string
}

Service base struct for typical service objects

func NewServiceBase

func NewServiceBase(id string) Service

func (*Service) Debugf

func (sb *Service) Debugf(format string, a ...any)

func (*Service) Errorf

func (sb *Service) Errorf(format string, a ...any)

func (*Service) Fatalf

func (sb *Service) Fatalf(format string, a ...any)

func (*Service) Infof

func (sb *Service) Infof(format string, a ...any)

func (*Service) NewError

func (sb *Service) NewError(format string, a ...any) error

func (*Service) SystemErrorf

func (sb *Service) SystemErrorf(format string, a ...any)

func (*Service) Warnf

func (sb *Service) Warnf(format string, a ...any)

Jump to

Keyboard shortcuts

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