goutil

package module
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: GPL-3.0 Imports: 41 Imported by: 0

README

go-util

This repository will be use for supporting process development service. All i need function or stack will be put in there. Go-util open for using for anyone.

Features

  • Load configuration
  • Context (get, set & parsing)
  • Database connection, including (Gorm & Sqlx)
  • Date formatting
  • Email sender
  • Open file
  • JWT create & parsing
  • Logging
  • Pagination (need test)
  • Random value
  • Reponse json (for gin-gonic framework)
  • Telegram connection
  • Validation rules

Documentation

Index

Constants

View Source
const (
	LoggerSilent = logLevel(logger.Silent)
	LoggerError  = logLevel(logger.Error)
	LoggerWarn   = logLevel(logger.Warn)
	LoggerInfo   = logLevel(logger.Info)
)
View Source
const (
	ContentTypeJSON              contentType = "application/json"
	ContentTypeFormURLEncoded    contentType = "application/x-www-form-urlencoded"
	ContentTypeTextHTML          contentType = "text/html"
	ContentTypeMultipartFormData contentType = "multipart/form-data"
)
View Source
const MainToken = "main-token"
View Source
const RefreshToken = "refresh-token"

Variables

This section is empty.

Functions

func Configuration

func Configuration(osFile *os.File, model interface{}) error

Configuration is a function for get info configuration

func CreateJWT

func CreateJWT(req RequestCreateJWT) (token string, err error)

CreateJWT is a function for generate token

func Gomail

func Gomail(host, password string, port int, from, to, subject, message string) error

Gomail is a function for send email

func LogMiddleware

func LogMiddleware(logger Logs) func(c *gin.Context)

LogMiddleware ..

func NewGorm

func NewGorm(db *sql.DB, driverName string, level logLevel) (*gorm.DB, error)

NewGorm is a override sql from native sql to gorm. driverName is a package of database your using, currently we have only mysql package. level is a info log of query, value is 1 - 4 (Silent, Error, Warn & Info)

func NewMySQL

func NewMySQL(user, password, url, schema string, parameters []string) (*sql.DB, error)

NewMySQL is a function for set up db mysql connection

func NewPostgreSQL added in v0.3.33

func NewPostgreSQL(user, password, host, schema string, sslMode null.String, port int) (*sql.DB, error)

NewPostgreSQL is a function for set up db postgresql connection

func NewSqlx

func NewSqlx(db *sql.DB, driverName string) *sqlx.DB

NewSqlx is a override sql from native sql to sqlx

func OpenFile

func OpenFile(path, filename string) (*os.File, error)

OpenFile is a function for open file with path & filename

func OpenFileNewest

func OpenFileNewest(oldest *os.File, path, filename string) (*os.File, error)

OpenFileNewest is a function for open file with path & filename and close the oldest os.File

func Pagination

func Pagination(limit, offset int) (limits, offsets int)

Pagination is a function for calculate of limit & offset

func ParseContext

func ParseContext(c *gin.Context) context.Context

ParseContext is a function for parsing gin.Context to context.Context

func ParseJWT

func ParseJWT(key string, signMethod jwt.SigningMethod, attributesJWT []string) func(c *gin.Context)

ParseJWT is a function for parse of token string

func RandIntRunes

func RandIntRunes(n int) string

RandIntRunes ..

func RandStringRunes

func RandStringRunes(n int) string

RandStringRunes is a function for random string

func ResponseError

func ResponseError(c *gin.Context, code int, msg error, obj interface{})

ResponseError is a function for send result error to client

func ResponseOK

func ResponseOK(c *gin.Context, code int, obj interface{})

ResponseOK is a function for send result to client

func SetContext

func SetContext(c *gin.Context)

SetContext is a function for set value on context

func TimeFormat

func TimeFormat(t time.Time, tf int) string

TimeFormat is a function for formating from type data time.Time to type data string you can pick one the layout, format time layout : 1. "2006-01-02 15:04:05" 2. "2006-01-02" 3. "02 January 2006"

func TimeParse

func TimeParse(s string, tl int) time.Time

TimeParse is a function for formating from type data string to type data time.Time you can pick one the layout, format time layout : 1. "2006-01-02T15:04:05.000Z" 2. "2006-01-02 15:04:05" 3. "2006-01-02" 4. "02-01-2006" 5. "02 Jan 2006"

Types

type BasicAuth added in v0.0.14

type BasicAuth struct {
	Username string
	Password string
}

type BasicAuthPayload added in v0.0.14

type BasicAuthPayload struct {
	Path     string
	Method   string
	Payload  BasicAuth
	Response interface{}
	Headers  map[string]string
}

type Context

type Context struct {
	RequestID string
	Method    string
	Endpoint  string
}

type Key

type Key string
const (
	KeyRequestID Key = "request_id"
	KeyMethod    Key = "method"
	KeyEndpoint  Key = "endpoint"
	KeyToken     Key = "token"
)

func (Key) String

func (k Key) String() string

type KeyContext added in v0.3.41

type KeyContext string

type Logs

type Logs interface {
	Config(osFile *os.File, createOutput bool)
	Info(ctx context.Context, msg string, zapFields ...zapcore.Field)
	Warning(ctx context.Context, err error)
	Error(ctx context.Context, err error)
	Undo()
	Sync()
}

Logs is a interface of method logging

func NewLog

func NewLog(osFile *os.File, telegram TeleService, createOutput bool) (Logs, error)

NewLog is a function for set up log information in this service

type RESTful added in v0.0.13

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

func NewRESTful added in v0.0.13

func NewRESTful(baseURL string, retry int) *RESTful

func (*RESTful) Request added in v0.0.13

func (r *RESTful) Request(req RequestPayload) (statusCode int, err error)

func (*RESTful) RequestBasicAuth added in v0.0.14

func (r *RESTful) RequestBasicAuth(req BasicAuthPayload) (statusCode int, err error)

type RegisterTranslation added in v0.3.36

type RegisterTranslation func(v *validator.Validate, trans *ut.Translator) error

type RegisterValidation added in v0.3.36

type RegisterValidation func(v *validator.Validate) error

type RequestCreateJWT added in v0.3.41

type RequestCreateJWT struct {
	SignMethod jwt.SigningMethod
	Key        string
	Data       jwt.MapClaims
}

type RequestPayload added in v0.0.12

type RequestPayload struct {
	Path        string
	QueryParam  map[string]string
	Method      string
	ContentType contentType
	Payload     interface{}
	PayloadFile interface{}
	Response    interface{}
	Headers     map[string]string
}

type Response

type Response struct {
	Code    int         `json:"code"`
	Message interface{} `json:"message"`
	Data    interface{} `json:"data"`
}

type TeleService

type TeleService interface {
	SendError(ctx context.Context, path string, line int, msg string) (err error)
}

TeleService is a interface all of function third-party Telegram

func NewTele

func NewTele(
	token string,
	chatID int64) (tele TeleService, err error)

NewTele is a function open connection

type Validation

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

func NewValidation

func NewValidation() (Validation, error)

func (*Validation) RegisterTranslation added in v0.3.37

func (vt *Validation) RegisterTranslation(translations ...RegisterTranslation) (err error)

func (*Validation) RegisterValidation added in v0.3.37

func (vt *Validation) RegisterValidation(validations ...RegisterValidation) (err error)

func (*Validation) ValidationStruct

func (vt *Validation) ValidationStruct(req interface{}) error

func (*Validation) ValidationVariable

func (vt *Validation) ValidationVariable(req interface{}, tag string, msgErr string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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