util

package
v0.0.0-...-827ba16 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2024 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Open      string = "open"
	Cancelled string = "cancelled"
	Accepted  string = "accepted"
	Committed string = "committed"
	Closed    string = "closed"
)
View Source
const IMAGE_NAME string = "test_db"
View Source
const RESOURCE_NAME string = "test_db"
View Source
const SQL_FILE string = "saturday.sql"

Variables

View Source
var EventTopic = os.Getenv("EVENT_TOPIC")
View Source
var LogTopic = os.Getenv("LOG_TOPIC")
View Source
var Logger = getLogger()
View Source
var NSQProducer *nsq.Producer

Functions

func BindAll

func BindAll(c *gin.Context, target interface{}) error

func CheckError

func CheckError(c *gin.Context, err error) bool

check error type if error is not nil, return true and handle accordingly, else return false

func CodeToSession

func CodeToSession(code string) (string, error)

func CreateBasicAuth

func CreateBasicAuth(username, password string) string

func CreateToken

func CreateToken(payload Payload) (string, error)

func FormatDate

func FormatDate(date time.Time) string

func GenToken

func GenToken(auth string, id ...string) string

this is used for testing "INVALID" to gen invalid token "EXPIRED" to gen expired token "NONE" return empty token

func GetCsvMap

func GetCsvMap(filePath string) ([]map[string]string, error)

ReadCsvFileWithHeader reads a csv file and returns a map of header and data

func GetDate

func GetDate() string

func GetErrorMessage

func GetErrorMessage(err validator.FieldError) string

func GetIdentity

func GetIdentity(c *gin.Context) model.Identity

func GetNSQProducer

func GetNSQProducer() *nsq.Producer

func GetPaginationQuery

func GetPaginationQuery(c *gin.Context) (offset uint64, limit uint64, err error)

func GetTokenString

func GetTokenString(token string) (string, error)

func InitDialer

func InitDialer()

func InitValidator

func InitValidator()

func MakeEventActionHandler

func MakeEventActionHandler(action Action, event *model.Event, identity model.Identity) *eventActionHandler

func ParseTokenWithJWKS

func ParseTokenWithJWKS(jwksURL string, token string) (*jwt.Token, *jwt.RegisteredClaims, error)

func Prefixer

func Prefixer(prefix string, columns []string) []string

return columns in the format of "prefix[0].column as prefix.column"

func ReadCsvFile

func ReadCsvFile(filePath string) ([][]string, error)

ReadCsvFile reads a csv file and returns a slice of records

func RollbackOnErr

func RollbackOnErr(err error, conn *sqlx.Tx)

func SendMail

func SendMail(message *gomail.Message) error

func SetColumnPrefix

func SetColumnPrefix(prefix string, column string) string

func Upload

func Upload(name string, reader io.Reader) (string, error)

Types

type Action

type Action string
const (
	Create      Action = "create"
	Accept      Action = "accept"
	Cancel      Action = "cancel"
	Drop        Action = "drop"
	Commit      Action = "commit"
	AlterCommit Action = "alterCommit"
	Reject      Action = "reject"
	Close       Action = "close"
	Update      Action = "update"
)

type Claims

type Claims struct {
	jwt.StandardClaims
	Payload
}

func ParseToken

func ParseToken(token string) (*jwt.Token, *Claims, error)

parse a jwt token, which should begin with "`Bearer `"

type ContextLogger

type ContextLogger struct {
	*logrus.Logger
	Context *gin.Context
}

type MockDB

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

MockDB is a mock database for testing. Use MakeMockDB to create a MockDB, and call MockDB.Start() to init resource. Under the hood, it use a docker container that runs mysql as data source. So you need to keep docker running for the test to work. You should call SetSchema() before each test to reset database schema.

func MakeMockDB

func MakeMockDB(assetsPath string) *MockDB

assetsPath should be the relative path to the assets folder, MockDB needs to read the dockerfile located in the assets folder.

func (*MockDB) Close

func (m *MockDB) Close()

func (*MockDB) CloseDb

func (m *MockDB) CloseDb()

func (*MockDB) SetSchema

func (m *MockDB) SetSchema() error

func (*MockDB) Start

func (m *MockDB) Start() (*sqlx.DB, error)

type NSQHookForError

type NSQHookForError struct {
	Producer *nsq.Producer
}

func (*NSQHookForError) Fire

func (hook *NSQHookForError) Fire(entry *logrus.Entry) error

Fire 根据 logrus.Entry 发送消息到 NSQ

func (*NSQHookForError) Levels

func (hook *NSQHookForError) Levels() []logrus.Level

Levels 返回日志级别,这里返回 ErrorLevel,FatalLevel,PanicLevel

type Payload

type Payload struct {
	Who    string
	Member model.Member
	Role   string
}

type ResponseBody

type ResponseBody struct {
	Message string        `json:"message"`
	Errors  []detailError `json:"errors,omitempty"`
}

type ServiceError

type ServiceError struct {
	HttpStatus int
	Body       ResponseBody
	// contains filtered or unexported fields
}

func IsServiceError

func IsServiceError(err error) (ServiceError, bool)

func MakeInternalServerError

func MakeInternalServerError() ServiceError

func MakeServiceError

func MakeServiceError(HttpStatus int) ServiceError

func MakeValidationError

func MakeValidationError(resource string, err error) ServiceError

parse error from validator return a *ServiceError with status code http.StatusUnprocessableEntity

func (ServiceError) AddDetailError

func (serviceError ServiceError) AddDetailError(resource string, field string, error string) ServiceError

func (ServiceError) Build

func (error ServiceError) Build() (int, interface{})

func (ServiceError) SetMessage

func (serviceError ServiceError) SetMessage(message string) ServiceError

func (ServiceError) SetStatus

func (serviceError ServiceError) SetStatus(status int) ServiceError

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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