chassis

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2020 License: Apache-2.0 Imports: 26 Imported by: 0

README

Chassis

Build Status

pgxs go webservice chassis

Features:

  • gorm
  • go-restufl
  • YAML config files
  • Validate
  • Migrate
  • API errors

Documentation

Index

Constants

View Source
const (
	//EnvPgTestDataFile import test data sql file
	EnvPgTestDataFile = "PG_TEST_DATA_FILE"
	//AppRunEnvLocal app run env "local"
	AppRunEnvLocal = "local"
	//AppRunEnvTest app run env "test"
	AppRunEnvTest = "test"
	//AppRunEnvProd app run env "prod"
	AppRunEnvProd = "prod"
)
View Source
const (
	//SortDirectionDESC 降序
	SortDirectionDESC = "DESC"
	//SortDirectionASC 升序
	SortDirectionASC = "ASC"
)
View Source
const (
	//PageIndexKey pagination page index default 0
	PageIndexKey = "page_index"
	//DefaultPageIndexValue pagination page index default 0
	DefaultPageIndexValue = 0
	//PageSizeKey pagination page size default 10
	PageSizeKey = "page_size"
	//DefaultPageSizeValue pagination page size default 10
	DefaultPageSizeValue = 10
)
View Source
const KeyOpenAPITags = restfulspec.KeyOpenAPITags

KeyOpenAPITags is a Metadata key for a restful Route

Variables

View Source
var ErrIDInvalid = NewAPIError(100001, "非法ID", "ID为数值且不能为0")

ErrIDInvalid REST resource ID invalid

View Source
var (
	ErrNoDatabaseConfiguration = errors.New("there isn't any database setting in the configuration file")
)
View Source
var ErrPageParamsInvalid = NewAPIError(100002, "分页参数有误", "page_index,page_size为数值类型,page_size 大于5小于100 被5整除")

ErrPageParamsInvalid Pagination params invalid

Functions

func AddMetaDataTags added in v0.3.7

func AddMetaDataTags(ws *restful.WebService, tags []string)

AddMetaDataTags add metadata tags to Webservice all routes

func AddMetaDataTagsAndWriteSample added in v0.3.8

func AddMetaDataTagsAndWriteSample(ws *restful.WebService, tags []string, entityType interface{})

AddMetaDataTagsAndWriteSample AddMetaDataTags() and WriteSample

func AddWriteSample added in v0.3.8

func AddWriteSample(ws *restful.WebService, entityType interface{})

AddWriteSample setting a webservice all routes to write sample with ResponseEntity

func CloseAllDB added in v0.7.1

func CloseAllDB() error

Close close all db connection

func Copy added in v0.2.0

func Copy(toValue interface{}, fromValu interface{})

Copy copy anyting. just proxy to copier(jinzhu/copier).

func DB

func DB() (*gorm.DB, error)

DB get the default(first) *Db connection

func DBs added in v0.6.0

func DBs() ([]*gorm.DB, error)

DBs get all database connections

func EnvIsProd added in v0.4.3

func EnvIsProd() bool

EnvIsProd check env is production ? local and test will drop database & import test data

func ExecSQLFile added in v0.7.5

func ExecSQLFile(fileName string) error

ExecSQLFile danger!!! just for importing test data.

func Migrate added in v0.4.3

func Migrate(assetNames []string, afn bindata.AssetFunc, dbURL, dialect string) error

Migrate Run new bindataInstance and UP

func NewAPIError added in v0.4.0

func NewAPIError(code int, msg, desc string) *apierrors.APIError

NewAPIError new api error

func PageQueryParamters

func PageQueryParamters(req *restful.Request) (pageIndex, pageSize uint)

PageQueryParamters get page params from request

func RedisClient added in v0.8.0

func RedisClient() *redis.Client

RedisClient get redis client return for simple or sentinel(failover)mode

func Serve added in v0.5.3

func Serve(svc []*restful.WebService)

Serve rest webservice

func Validate added in v0.2.0

func Validate() *validator.Validate

Validate validate single instance

func ValidateEntity added in v0.4.0

func ValidateEntity(entity interface{}) error

ValidateEntity validate entity

func ValidateEntityAndWriteResp added in v0.5.4

func ValidateEntityAndWriteResp(res *restful.Response, entity interface{}, apiErr *apierrors.APIError) error

ValidateEntityAndWriteResp validate entity and write header and error as entity

func ValidateTranslator added in v0.4.0

func ValidateTranslator() ut.Translator

ValidateTranslator validate trans

Types

type BaseController added in v0.4.4

type BaseController struct {
}

BaseController has some basic methods

func (BaseController) ValidatePageableParams added in v0.4.4

func (bc BaseController) ValidatePageableParams(req *restful.Request, res *restful.Response) (pageable *Pageable, ok bool)

ValidatePageableParams validate pagination params return paable struct

func (BaseController) ValidateResourceID added in v0.4.4

func (bc BaseController) ValidateResourceID(req *restful.Request, res *restful.Response, IDKey string) (ID uint, ok bool)

ValidateResourceID validate REST resource ID in pathparam and return it if validated.

type BaseDO added in v0.5.1

type BaseDO struct {
	ID        uint       `gorm:"primary_key" json:"id"`            // primary key
	CreatedAt time.Time  `json:"created_at,omitempty"`             // created time
	UpdatedAt time.Time  `json:"updated_at,omitempty"`             //updated time
	DeletedAt *time.Time `sql:"index" json:"deleted_at,omitempty"` //deleted time
}

Model gorm model

type BaseDTO added in v0.5.1

type BaseDTO struct {
	ID        uint     `json:"id,omitempty" description:"资源ID"`
	CreatedAt JSONTime `json:"created_at,omitempty" description:"创建日期"`
	UpdatedAt JSONTime `json:"updated_at,omitempty" description:"更新日期"`
}

BaseDTO Data Transfer Object

type BaseResource added in v0.4.5

type BaseResource struct {
	BaseController
}

BaseResource RESTful webservice name ,alias BaseController.

type ComplexBaseDO added in v0.5.1

type ComplexBaseDO struct {
	BaseDO
	Version  uint   `json:"version"` //version opt lock
	Addition string `json:"addition,omitempty"`
}

ComplexBaseDO gorm model composed Model add Addition

type EmptyRespEntity added in v0.5.4

type EmptyRespEntity struct {
	apierrors.APIError
}

EmptyRespEntity 空响应体

type Entity added in v0.3.0

type Entity struct {
	*apierrors.APIError
	Data interface{} `json:"data,omitempty"`
}

Entity response entity

type JSONDate

type JSONDate time.Time

JSONDate 格式化输出时间

func (JSONDate) MarshalJSON

func (j JSONDate) MarshalJSON() ([]byte, error)

MarshalJSON json解码

func (*JSONDate) UnmarshalJSON added in v0.4.2

func (j *JSONDate) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON JSONDate反序列化

type JSONTime

type JSONTime time.Time

JSONTime 格式化输出时间

func (JSONTime) MarshalJSON

func (j JSONTime) MarshalJSON() ([]byte, error)

MarshalJSON json解码

func (*JSONTime) UnmarshalJSON added in v0.4.2

func (j *JSONTime) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON JSONDate反序列化

type MultiDBSource added in v0.6.0

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

func (MultiDBSource) Size added in v0.6.0

func (s MultiDBSource) Size() int

Size get db connection size

type Page

type Page struct {
	List   interface{} `json:"list,omitempty"`
	Total  uint        `json:"total,omitempty"`
	Offset uint        `json:"offset,omitempty"`
	Index  uint        `json:"page_index,omitempty"`
	Size   uint        `json:"page_size,omitempty"`
	Pages  uint        `json:"pages,omitempty"`
}

Page page

func NewPagination

func NewPagination(db *gorm.DB, model interface{}, pageIndex, pageSize uint) *Page

NewPagination pagination query

type PageDTO added in v0.5.4

type PageDTO struct {
	Total uint `json:"total"`
	Index uint `json:"page_index"`
	Size  uint `json:"page_size"`
	Pages uint `json:"pages"`
}

PageDTO common page struct

type Pageable added in v0.4.4

type Pageable struct {
	Page  uint
	Size  uint
	Sorts []Sort
}

Pageable 可分页参数

type Pagination

type Pagination struct {
	Offset    uint        `json:"offset,omitempty"`
	Limit     uint        `json:"limit,omitempty"`
	Condition interface{} `json:"condition,omitempty"`
}

Pagination 新建分页查询

type RespEntity added in v0.5.4

type RespEntity struct {
	apierrors.APIError
}

RespEntity common response entity

type Response added in v0.3.0

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

Response rest response

func NewResponse added in v0.3.1

func NewResponse(res *restful.Response) *Response

NewResponse new return

func (*Response) Created added in v0.3.0

func (re *Response) Created(entity interface{})

Created 201 return

func (*Response) Entity added in v0.5.4

func (re *Response) Entity(entity interface{})

func (*Response) Error added in v0.3.0

func (re *Response) Error(status int, err *apierrors.APIError)

Error error response

func (*Response) Ok added in v0.3.0

func (re *Response) Ok(entity interface{})

Ok 200 return

func (*Response) Status added in v0.5.4

func (re *Response) Status(statusCode int) *Response

type ResponseEntitySample added in v0.3.8

type ResponseEntitySample struct {
	ErrCode int         `json:"err_code,omitempty"`
	ErrMsg  string      `json:"err_msg,omitempty"`
	ErrDesc string      `json:"err_desc,omitempty"`
	Data    interface{} `json:"data,omitempty"`
}

ResponseEntity response entity for go-restful Writes(ResponseEntity{Data: Type{}})

func NewWriteSample added in v0.3.9

func NewWriteSample(entity interface{}) ResponseEntitySample

NewWriteSample new writesample

type SampleBaseDO added in v0.5.1

type SampleBaseDO struct {
	ID uint `gorm:"primary_key" json:"id"`
}

SampleBaseDO model with id pk

type Sort added in v0.4.4

type Sort struct {
	Field     string
	Direction string
}

Sort 分页查询排序

Directories

Path Synopsis
filters

Jump to

Keyboard shortcuts

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