kitty

package module
v0.0.0-...-592bad0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2020 License: MIT Imports: 8 Imported by: 0

README

kitty contain services kit.

Install

go get github.com/Kamva/kitty

Available Services:

  • Config: config service.
  • Logger: logger service
  • Translator: translator service.

How to use:

example:

// Assume we want to use viper as config service.
v := viper.New()

// tune your viper.

config := kittyconfig.NewViperDriver(v)

// Use config service in app.

today todo:

  • On worker and jobs implementations get queue prefix (in both jobs and worker, to distinct microservices queues).

Todo:

  • Collection presenter
  • Write Tests
  • Add more services like log,...
  • Add badges to readme.
  • CI

Documentation

Index

Constants

View Source
const (
	// ErrorKeyInternalError is the internal error key in Error
	// messages over all of packages. use this to have just one
	// internal_error translation key in your translation system.
	ErrKeyInternalError = "err_internal_error"
)
View Source
const (
	Version = "1.1.0"
)

Version is kitty current version.

Variables

View Source
var (
	ErrInvalidID = NewError(http.StatusBadRequest, "kt.ett.e.0", "err_invalid_id", errors.New("id value is invalid"))
)
View Source
var TranslateKeyEmptyMessage = "__empty_message__"

TranslateKeyEmptyMessage is special key that translators return empty string for that.

Functions

func PrintBanner

func PrintBanner(banner, product, version, website string)

PrintBanner print the app's banner

Types

type Config

type Config interface {
	// Unmarshal function load viperDriver into viperDriver instance. pass instance by reference.
	Unmarshal(configInstance interface{}) error

	// Get method return config value in any type.
	Get(key string) interface{}

	// GetString method return config value as string
	GetString(key string) string

	// GetInt64 return int64 config value.
	GetInt64(key string) int64

	// GetFloat64 return float64 config value.
	GetFloat64(key string) float64

	// GetBool return bool config value.
	GetBool(key string) bool

	// GetList return config value as string list.
	GetList(key string) []string
}

Config is viperDriver interface that should embed in each Config object.

type Context

type Context interface {
	context.Context

	// RequestID returns the request id.
	RequestID() string

	// Correlation returns the request correlation id.
	CorrelationID() string

	// User returns the user
	User() User

	// Logger returns the kitty logger customized for specific request.
	Logger() Logger

	// Translator returns the translator localized relative to the users request.
	Translator() Translator

	// ToMap method convert context to map to export and import.
	ToMap() Map
}

Context is the kitty context to use in services.

func CtxFromMap

func CtxFromMap(m Map, uf UserFinder, l Logger, t Translator) (Context, error)

CtxFromMap generate new context form the exported map by another context (by using ToMap function on the context).

func NewCtx

func NewCtx(requestID, correlationID string, locale string, user User, logger Logger, translator Translator) Context

NewCtx returns new kitty Context. locale syntax is just same as HTTP Accept-Language header.

type Error

type Error interface {
	error

	// SetError set the internal error.
	SetError(error) Error

	//Is function satisfy Is interface of errors package.
	Is(error) bool

	// HTTPStatus returns the http status code for the Error.
	HTTPStatus() int

	// HTTPStatus returns the http status code for the reply.
	SetHTTPStatus(status int) Error

	// Code return the Error identifier code
	Code() string

	// Key returns unique key for each Error to use as translation key,...
	Key() string

	// Params returns params of the Error to use in translation,...
	Params() Map

	// SetParams set the Error translation parameters to use in reply translation,...
	SetParams(params Map) Error

	// Data returns the extra data of the Error (e.g show this data to user).
	Data() Map

	// SetData set the Error data as extra data of the Error to show to the user.
	SetData(data Map) Error

	// ReportData returns the data that should use on reporting Error to somewhere (e.g log aggregator)
	ReportData() Map

	SetReportData(data Map) Error

	// ReportIfNeeded function report the Error to the log system if
	// http status code is in range 5XX.
	// return value specify that reported or no.
	ReportIfNeeded(Logger, Translator) bool
}

Error is reply to actions when occur error in microservices.

func NewError

func NewError(httpStatus int, code string, key string, err error) Error

NewError returns new instance the Error interface.

type HttpRespBody

type HttpRespBody struct {
	Code    string `json:"code" mapstructure:"code"`
	Message string `json:"message" mapstructure:"message"`
	Data    Map    `json:"data" mapstructure:"data"`
	// contains filtered or unexported fields
}

HttpRespBody is the http response body format

func NewBody

func NewBody(code string, msg string, data Map) HttpRespBody

NewBody return new instance of the HttpRespBody

func (HttpRespBody) Debug

func (b HttpRespBody) Debug(debug bool, debugData Map) HttpRespBody

Debug set debug flag and debug data.

func (HttpRespBody) MarshalJSON

func (b HttpRespBody) MarshalJSON() ([]byte, error)

MarshalJSON marshall the body to json value.

type ID

type ID interface {
	fmt.Stringer

	// Validate say that id value is valid or not.
	Validate(id interface{}) error

	// From convert provided value to its id.
	// From will returns error if provided value
	// can not convert to an native id.
	From(id interface{}) error

	// MustFrom Same as FromString but on occur error, it will panic.
	MustFrom(id interface{})

	// Val returns the native id value (e.g ObjectID in mongo, ...).
	Val() interface{}
}

ID is the id of entities across the kitty packages. This is because kitty does not want to be dependent to specific type of id. (e.g mongo ObjectID, mysql integer,...)

type Logger

type Logger interface {

	// Core function returns the logger core concrete struct.
	// this is because in sometimes we need to convert one logger
	// interface to another and need to logger code.
	Core() interface{}

	// With method set key,values and return new logger
	// contains this key values as log fields.
	WithFields(keyValues ...interface{}) Logger

	// Debug log debug message.
	Debug(i ...interface{})

	// Info log info message.
	Info(i ...interface{})

	// Warn log warn message.
	Warn(i ...interface{})

	// Error log error message
	Error(i ...interface{})

	// Fatal log fatal message.
	Fatal(i ...interface{})

	// Panic log message as fatal and the panic.
	Panic(i ...interface{})
}

type Map

type Map map[string]interface{}

type Reply

type Reply interface {
	// specifically this interface contains the error interface
	// also, to able pass as return value in some frameworks that
	// permit to return error and then set error handler.
	// e.g by this  way we can pass this Reply to the ErrorHandler
	// and error handler can check Reply type and return the proper
	// response.
	// in implementation of Reply we can return just empty string or
	// special value (like "__reply__") as error message.
	error

	//Is function satisfy Is interface of errors package.
	Is(error) bool

	// HTTPStatus returns the http status code for the reply.
	HTTPStatus() int

	// HTTPStatus returns the http status code for the reply.
	SetHTTPStatus(status int) Reply

	// Code return the reply identifier code
	Code() string

	// Key returns unique key for each reply to use as translation key,...
	Key() string

	// Params returns params of the reply to use in translation,...
	Params() Map

	// SetParams set the reply parameters to use in reply translation,...
	SetParams(params Map) Reply

	// Data returns the extra data of the reply (e.g show this data to user).
	Data() Map

	// SetData set the reply data as extra data of the reply to show to the user.
	SetData(data Map) Reply
}

Reply is reply to actions in microservices.

func NewReply

func NewReply(httpStatus int, code string, key string) Reply

NewReply returns new instance the Reply interface implemented by defaultReply.

type Secret

type Secret string

Use secret to string show as * in fmt package.

func (Secret) MarshalJSON

func (s Secret) MarshalJSON() ([]byte, error)

func (Secret) String

func (s Secret) String() string

type Translator

type Translator interface {
	// Localize function returns new localized translator function.
	Localize(langs ...string) Translator

	// Translate get key and params nad return translation.
	Translate(key string, keyParams ...interface{}) (string, error)

	// MustTranslate get key and params and translate,
	//otherwise panic relative error.
	MustTranslate(key string, keyParams ...interface{}) string

	// TranslateDefault translate with default message.
	TranslateDefault(key string, fallback string, keyParams ...interface{}) (string, error)

	// MustTranslateDefault translate with default message, on occur error,will panic it.
	MustTranslateDefault(key string, fallback string, keyParams ...interface{}) string
}

type User

type User interface {
	// Specify that user is guestUser or no.
	IsGuest() bool

	// Return users identifier (if guestUser return just empty string or something like this.)
	Identifier() ID

	// Return the user name.
	GetName() string

	// Username can be unique username,email,phone number or everything else that can use as username.
	GetUsername() string
}

func NewGuestUser

func NewGuestUser() User

type UserFinder

type UserFinder = func(id interface{}) (User, error)

Directories

Path Synopsis
db
Package pagination provides support for pagination requests and responses.
Package pagination provides support for pagination requests and responses.

Jump to

Keyboard shortcuts

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