infra

package
v0.0.0-...-4c17be9 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Common tools and helper functions

Index

Constants

View Source
const NBRandomPassword = "A String Very Very Very Niubilty!!@##$!@#4"
View Source
const NBSecretPassword = "A String Very Very Very Strong!!@##$!@#$"

Keep this two config private, it should not expose to open source

Variables

View Source
var (
	DB *gorm.DB
)

Functions

func Bind

func Bind(c *gin.Context, obj interface{}) error

Changed the c.MustBindWith() -> c.ShouldBindWith(). I don't want to auto return 400 when error happened. origin function is here: https://github.com/gin-gonic/gin/blob/master/context.go

func GenToken

func GenToken(id uint) string

A Util function to generate jwt_token which can be used in the request header

func GetDB

func GetDB() *gorm.DB

Using this function to get a connection, you can create your connection pool here.

func RandString

func RandString(n int) string

A helper function to generate random string

func TestDBFree

func TestDBFree(test_db *Sqlite)

Delete the database after running testing cases.

Types

type CommonError

type CommonError struct {
	Errors map[string]interface{} `json:"errors"`
}

My own Error type that will help return my customized Error info

{"database": {"hello":"no such table", error: "not_exists"}}

func NewError

func NewError(key string, err error) CommonError

Warp the error info in a object

func NewValidatorError

func NewValidatorError(err error) CommonError

To handle the error returned by c.Bind in gin framework https://github.com/go-playground/validator/blob/v9/_examples/translations/main.go

type Database

type Database interface {
	OpenDB(uri string)
	CloseDB()
	Get() *gorm.DB // TODO: remove later because of violating DI principle
}

type MySQL

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

func (*MySQL) CloseDB

func (mysql *MySQL) CloseDB()

func (*MySQL) Get

func (mysql *MySQL) Get() *gorm.DB

func (*MySQL) OpenDB

func (mysql *MySQL) OpenDB(uri string)

type Sqlite

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

func TestDBInit

func TestDBInit() *Sqlite

This function will create a temporarily database for running testing cases

func (*Sqlite) CloseDB

func (s *Sqlite) CloseDB()

func (*Sqlite) Get

func (s *Sqlite) Get() *gorm.DB

func (*Sqlite) OpenDB

func (s *Sqlite) OpenDB(uri string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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