chassis

package module
v0.9.3 Latest Latest
Warning

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

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

README

chassis-go

Build Status

中文

Integrate awesome go framework for developing web service

go webservice chassis

usage:

go get c6x.io/chassis

Features:

  • gorm
  • go-restful
  • YAML config files
  • Validate
  • DB migrate
  • API errors
  • Redis cache
  • Memory cache

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

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

AddMetaDataTags add metadata tags to Webservice all routes

func AddMetaDataTagsAndWriteSample

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

AddMetaDataTagsAndWriteSample AddMetaDataTags() and WriteSample

func AddWriteSample

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

AddWriteSample setting a webservice all routes to write sample with ResponseEntity

func CloseAllDB

func CloseAllDB() error

Close close all db connection

func Copy

func Copy(target interface{}, from interface{})

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

func DB

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

DB get the default(first) *Db connection

func DBs

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

DBs get all database connections

func EnvIsProd

func EnvIsProd() bool

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

func ExecSQLFile

func ExecSQLFile(fileName string) error

ExecSQLFile danger!!! just for importing test data.

func Migrate

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

Migrate Run new bindataInstance and UP

func NewAPIError

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

NewAPIError new api error

func PageQueryParams added in v0.9.3

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

PageQueryParams get page params from request

func RedisClient

func RedisClient() *redis.Client

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

func RedisClusterClient

func RedisClusterClient() *redis.ClusterClient

RedisClusterClient redis cluster client

func Serve

func Serve(svc []*restful.WebService)

Serve rest webservice

func Validate

func Validate() *validator.Validate

Validate validate single instance

func ValidateEntity

func ValidateEntity(entity interface{}) error

ValidateEntity validate entity

func ValidateEntityAndWriteResp

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

ValidateEntityAndWriteResp validate entity and write header and error as entity

func ValidateTranslator

func ValidateTranslator() ut.Translator

ValidateTranslator validate trans

Types

type BaseController

type BaseController struct {
}

BaseController has some basic methods

func (BaseController) ValidatePageableParams

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

ValidatePageableParams validate pagination params return paable struct

func (BaseController) ValidateResourceID

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

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

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

type BaseResource struct {
	BaseController
}

BaseResource RESTFul webservice name ,alias BaseController.

type ComplexBaseDO

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

type EmptyRespEntity struct {
	apierrors.APIError
}

EmptyRespEntity 空响应体

type Entity

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

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

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

UnmarshalJSON JSONDate反序列化

type MultiDBSource

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

func (MultiDBSource) Size

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

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

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

type RespEntity struct {
	apierrors.APIError
}

RespEntity common response entity

type Response

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

Response rest response

func NewResponse

func NewResponse(res *restful.Response) *Response

NewResponse new return

func (*Response) Created

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

Created 201 return

func (*Response) Entity

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

func (*Response) Error

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

Error error response

func (*Response) Ok

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

Ok 200 return

func (*Response) Status

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

type ResponseEntitySample

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

func NewWriteSample(entity interface{}) ResponseEntitySample

NewWriteSample new write sample

type SampleBaseDO

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

SampleBaseDO model with id pk

type Sort

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