common_utils

package
v1.1.69 Latest Latest
Warning

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

Go to latest
Published: May 17, 2024 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TEST        = "test"
	DEVELOPMENT = "development"
	STAGING     = "staging"
	PRODUCTION  = "production"
)

Variables

This section is empty.

Functions

func BuildCacheKey added in v1.0.35

func BuildCacheKey(key string, identifier string, funcName string, args ...any) string

func BuildPrefixKey added in v1.0.35

func BuildPrefixKey(keys ...string) string

func CheckIfFileExists

func CheckIfFileExists(path string) bool

func ComparedPassword added in v1.0.10

func ComparedPassword(hashedPassword string, password string) error

func ConvertInterfaceE

func ConvertInterfaceE(from any, to any) error

func ConvertInterfaceP

func ConvertInterfaceP(from any, to any)

func CreateFile

func CreateFile(path string) (*os.File, error)

func CustomError

func CustomError(message string, statusCode int) error

func CustomErrorWithTrace

func CustomErrorWithTrace(err error, message string, statusCode int) error

func DeferCheck

func DeferCheck(function func() error)

func DeleteFile

func DeleteFile(path string) error

func DownloadFile

func DownloadFile(url, filePath string) error

func EnableCORS added in v1.0.6

func EnableCORS(r *chi.Mux)

func ExecSession added in v1.1.55

func ExecSession(ctx context.Context, client *mongo.Client, fn func(mongo.SessionContext) error) error

func ExecSessionWithRetry added in v1.1.55

func ExecSessionWithRetry(ctx context.Context, client *mongo.Client, fn func(mongo.SessionContext) error) error

func ExecTx added in v1.1.11

func ExecTx(ctx context.Context, pgxPool *pgxpool.Pool, fn func(tx pgx.Tx) error) error

func ExecTxWithRetry added in v1.1.11

func ExecTxWithRetry(ctx context.Context, pgxPool *pgxpool.Pool, fn func(tx pgx.Tx) error) error

func Fetch added in v1.1.66

func Fetch(url string, headers ...map[string]string) (*goquery.Document, error)

func FetchFile added in v1.1.68

func FetchFile(url string) (*os.File, error)

func GenerateJsonResponse added in v1.0.6

func GenerateJsonResponse(w http.ResponseWriter, data interface{}, statusCode int, message string)

func GetCurrentDir

func GetCurrentDir() string

func GetExt

func GetExt(pathOrFilename string) string

func HashedPassword added in v1.0.9

func HashedPassword(password string) (string, error)

func ImageUriToBase64 added in v1.1.64

func ImageUriToBase64(uri string) (string, error)

func LogAndPanicIfError

func LogAndPanicIfError(err error, message string)

func LogDebug

func LogDebug(message string, fields ...zap.Field)

func LogError

func LogError(message string, fields ...zap.Field)

func LogFatal

func LogFatal(message string, fields ...zap.Field)

func LogIfError

func LogIfError(err error)

func LogInfo

func LogInfo(message string, fields ...zap.Field)

func LogPanic

func LogPanic(message string, fields ...zap.Field)

func Marshal added in v1.1.32

func Marshal(v any) ([]byte, error)

func NewDecoder added in v1.1.32

func NewDecoder(r io.Reader) *jsoniter.Decoder

func NewEncoder added in v1.1.32

func NewEncoder(w io.Writer) *jsoniter.Encoder

func PanicAppError

func PanicAppError(message string, statusCode int)

func PanicIfAppError

func PanicIfAppError(err error, message string, statusCode int)

func PanicIfAppErrorWithTrace added in v1.0.64

func PanicIfAppErrorWithTrace(ctx context.Context, err error, message string, statusCode int)

func PanicIfError

func PanicIfError(err error)

func PanicValidationError

func PanicValidationError(errors []ValidationError, statusCode int)

func Unmarshal added in v1.1.32

func Unmarshal(data []byte, v any) error

func ValidateBodyPayload

func ValidateBodyPayload(body io.ReadCloser, output interface{})

func ValidateQueryParamInt added in v1.0.6

func ValidateQueryParamInt(r *http.Request, queryName string) int

func ValidateStruct

func ValidateStruct(data interface{})

func ValidateUrlParamUUID added in v1.0.6

func ValidateUrlParamUUID(r *http.Request, paramName string) uuid.UUID

Types

type AppError

type AppError struct {
	Message    string
	StatusCode int
}

func (*AppError) Error

func (ae *AppError) Error() string

type BaseConfig

type BaseConfig struct {
	ServiceName            string        `mapstructure:"SERVICE_NAME"`
	ServiceEnv             string        `mapstructure:"SERVICE_ENV"`
	ServiceHost            string        `mapstructure:"SERVICE_HOST"`
	ServiceHttpPort        string        `mapstructure:"SERVICE_HTTP_PORT"`
	ServiceGrpcPort        string        `mapstructure:"SERVICE_GRPC_PORT"`
	GcpProjectId           string        `mapstructure:"GCP_PROJECT_ID"`
	PubsubDlq              string        `mapstructure:"PUBSUB_DLQ_TOPIC"`
	RedisHost              string        `mapstructure:"REDIS_HOST"`
	RedisPort              string        `mapstructure:"REDIS_PORT"`
	RedisUser              string        `mapstructure:"REDIS_USER"`
	RedisPassword          string        `mapstructure:"REDIS_PASSWORD"`
	RedisCacheExpire       int           `mapstructure:"REDIS_DEFAULT_CACHE_EXPIRE"`
	MongoHost              string        `mapstructure:"MONGO_HOST"`
	MongoPort              string        `mapstructure:"MONGO_PORT"`
	MongoUser              string        `mapstructure:"MONGO_USER"`
	MongoPassword          string        `mapstructure:"MONGO_PASSWORD"`
	MongoDb                string        `mapstructure:"MONGO_DATABASE"`
	MongoReplicaSet        string        `mapstructure:"MONGO_REPLICA_SET,default=rs0"`
	PostgresHost           string        `mapstructure:"POSTGRES_HOST"`
	PostgresPort           string        `mapstructure:"POSTGRES_PORT"`
	PostgresUser           string        `mapstructure:"POSTGRES_USER"`
	PostgresPassword       string        `mapstructure:"POSTGRES_PASSWORD"`
	PostgresDb             string        `mapstructure:"POSTGRES_DATABASE"`
	ElasticsearchHost      []string      `mapstructure:"ELASTICSEARCH_HOST"`
	ElasticsearchUser      string        `mapstructure:"ELASTICSEARCH_USER"`
	ElasticsearchPassword  string        `mapstructure:"ELASTICSEARCH_PASSWORD"`
	ElasticsearchLogging   bool          `mapstructure:"ELASTICSEARCH_LOGGING,default=false"`
	JaegerEnable           bool          `mapstructure:"JAEGER_ENABLE"`
	JaegerHost             string        `mapstructure:"JAEGER_HOST"`
	JaegerPort             string        `mapstructure:"JAEGER_PORT"`
	JaegerLogSpans         bool          `mapstructure:"JAEGER_LOG_SPANS"`
	S3Endpoint             string        `mapstructure:"S3_ENDPOINT"`
	S3AccessKey            string        `mapstructure:"S3_ACCESS_KEY"`
	S3SecretKey            string        `mapstructure:"S3_SECRET_KEY"`
	S3Region               string        `mapstructure:"S3_REGION"`
	S3PublicBucket         string        `mapstructure:"S3_PUBLIC_BUCKET"`
	S3PrivateBucket        string        `mapstructure:"S3_PRIVATE_BUCKET"`
	S3PublicUrl            string        `mapstructure:"S3_PUBLIC_URL"`
	S3PreSignedExpire      time.Duration `mapstructure:"S3_PRESIGNED_EXPIRE"`
	JwtSecretKey           string        `mapstructure:"JWT_SECRET_KEY"`
	JwtAccessTokenExpire   time.Duration `mapstructure:"JWT_ACCESS_TOKEN_EXPIRE"`
	JwtRefreshTokenExpire  time.Duration `mapstructure:"JWT_REFRESH_TOKEN_EXPIRE"`
	KafkaBrokers           []string      `mapstructure:"KAFKA_BROKERS"`
	KafkaTopics            []string      `mapstructure:"KAFKA_TOPICS"`
	KafkaGroupID           string        `mapstructure:"KAFKA_GROUP_ID"`
	KafkaDlqTopic          string        `mapstructure:"KAFKA_DLQ_TOPIC"`
	KafkaDlqRetry          int           `mapstructure:"KAFKA_DLQ_RETRY,default=3"`
	KafkaAutoCommit        bool          `mapstructure:"KAFKA_AUTO_COMMIT,default=false"`
	KafkaAutoTopicCreation bool          `mapstructure:"KAFKA_AUTO_TOPIC_CREATION,default=true"`
	KafkaReplicationFactor int           `mapstructure:"KAFKA_REPLICATION_FACTOR,default=1"`
}

func CheckAndSetConfig

func CheckAndSetConfig(path string, configName string) *BaseConfig

func LoadBaseConfig

func LoadBaseConfig(path string, configName string) (*BaseConfig, error)

type Pagination added in v1.0.26

type Pagination[T any] struct {
	SearchField string `json:"searchField,omitempty"`
	SearchValue string `json:"searchValue,omitempty"`
	Limit       int    `json:"limit,omitempty"`
	Page        int    `json:"page,omitempty"`
	Sort        string `json:"sort,omitempty"`
	TotalRows   int    `json:"totalRows,omitempty"`
	TotalPages  int    `json:"totalPages,omitempty"`
	Rows        []T    `json:"rows,omitempty"`
}

func Paginate added in v1.0.26

func Paginate[T any](pagination *Pagination[T], rows []T) *Pagination[T]

func ValidatePagination added in v1.0.26

func ValidatePagination[T any](r *http.Request) *Pagination[T]

type ParamType added in v1.0.6

type ParamType interface {
	uuid.UUID | string
}

type Response added in v1.0.6

type Response[T any] struct {
	StatusCode int    `json:"status_code"`
	Status     string `json:"status"`
	Message    string `json:"message"`
	Data       T      `json:"data,omitempty"`
}

swagger:model Response

type ResponseMap added in v1.0.6

type ResponseMap struct {
	StatusCode int                    `json:"status_code"`
	Status     string                 `json:"status"`
	Message    string                 `json:"message"`
	Data       map[string]interface{} `json:"data,omitempty"`
}

FOR TESTING PURPOSE

type ResponseSlice added in v1.0.6

type ResponseSlice struct {
	StatusCode int                      `json:"status_code"`
	Status     string                   `json:"status"`
	Message    string                   `json:"message"`
	Data       []map[string]interface{} `json:"data,omitempty"`
}

FOR TESTING PURPOSE

type ValidationError

type ValidationError struct {
	Message string
}

func (*ValidationError) Error

func (ve *ValidationError) Error() string

type ValidationErrors

type ValidationErrors struct {
	Errors     []ValidationError
	StatusCode int
}

func (*ValidationErrors) Error

func (ve *ValidationErrors) Error() string

Jump to

Keyboard shortcuts

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