restful

package module
v0.0.0-...-bc9e848 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2024 License: MIT Imports: 14 Imported by: 0

README

gin-restful

mysql restful api框架,基于gin实现

Documentation

Index

Constants

View Source
const (
	TimeFormat string = "2006-01-02 15:04:05"
)

Variables

This section is empty.

Functions

func CheckDBResult

func CheckDBResult(result *gorm.DB)

func ContextWithRequestBody

func ContextWithRequestBody(c *gin.Context, req *http.Request) error

ContextWithRequestBody 将 RequestBody 设置到 ctx 里去,之后可以使用 RequestBodyFromContext 读取到

func ContextWithResource

func ContextWithResource(c *gin.Context, resource IResource)

ContextWithResource 将 Resource 设置到 ctx 里去,之后可以使用 ResourceFromContext 读取到

func GetMethodName

func GetMethodName(method HttpMethod) string

GetMethodName 根据 HTTP Method 返回对应的操作值

func GetMethodsName

func GetMethodsName(methods map[HttpMethod]HandlerFunc) []string

GetMethodsName 获取 HTTP 请求方法列表

func GetQuery

func GetQuery(c *gin.Context) map[string]string

GetQuery 获取get请求

func New

func New() *restful

Types

type Controller

type Controller struct {
	HaveDetail bool
	// contains filtered or unexported fields
}

Controller 定义 Restful 路由转发相关 api 结构体

func NewController

func NewController() *Controller

func (*Controller) Init

func (ctrl *Controller) Init(instance interface{}, root IRoot)

Init Resource 初始化

func (*Controller) Mount

func (ctrl *Controller) Mount(router *gin.RouterGroup, urlPath string)

Mount 将 Resource 方法注册到路由

func (*Controller) Print

func (ctrl *Controller) Print(url string)

func (*Controller) RegisterMethod

func (ctrl *Controller) RegisterMethod(methodType MethodType, httpMethod HttpMethod, postfix string, handler HandlerFunc)

RegisterMethod 操作方法和返回类型注册

type HandlerDecorator

type HandlerDecorator func(HandlerFunc) HandlerFunc

HandlerDecorator 装饰器/中间件方法

type HandlerFunc

type HandlerFunc func(*gin.Context) Response

func InstallDecorators

func InstallDecorators(handler HandlerFunc, decorators []HandlerDecorator) HandlerFunc

type HttpMethod

type HttpMethod int32

HttpMethod 原生方法

const (
	HTTPMethodGet HttpMethod = iota + 1
	HTTPMethodPost
	HTTPMethodPut
	HTTPMethodPatch
	HTTPMethodDelete
)

HTTP METHOD 方法定义

type IController

type IController interface {
	Init(interface{}, IRoot)
	Mount(*gin.RouterGroup, string)
	RegisterMethod(MethodType, HttpMethod, string, HandlerFunc)
	Print(string)
}

IController 接口定义,启动时可以调用

type IDecorator

type IDecorator interface {
	GetDecorators() []HandlerDecorator
}

type IDelete

type IDelete interface {
	Delete(*gin.Context) Response
}

type IDeleteInit

type IDeleteInit interface {
	InitDelete(IResource)
}

type IGet

type IGet interface {
	Get(*gin.Context) Response
}

type IGetInit

type IGetInit interface {
	InitGet(IResource)
}

type IList

type IList interface {
	List(*gin.Context) Response
}

type IListInit

type IListInit interface {
	InitList(IResource)
}

type IModel

type IModel interface {
	Database() *gorm.DB
}

type IPatch

type IPatch interface {
	Patch(*gin.Context) Response
}

type IPatchInit

type IPatchInit interface {
	InitPatch(IResource)
}

type IPost

type IPost interface {
	Post(*gin.Context) Response
}

type IPostInit

type IPostInit interface {
	InitPost(IResource)
}

type IPut

type IPut interface {
	Put(*gin.Context) Response
}

type IPutInit

type IPutInit interface {
	InitPut(IResource)
}

type IResource

type IResource interface {
	// Query 获取查询句柄,推荐优先用QueryWithContext/QueryPrimaryKey
	Query() *gorm.DB
	// QueryWithContext 获取已绑定context的查询句柄
	QueryWithContext(*gin.Context) *gorm.DB
	// QueryPrimaryKey 获取添加PrimaryKey条件的查询句柄
	QueryPrimaryKey(*gin.Context) *gorm.DB

	// GetDB 获取gorm DB实例
	GetDB() *gorm.DB
	// GetModel 获取资源的Model封装
	GetModel() *model.Model
	// GetSerializer 获取具体Model的序列化实例
	GetSerializer(*model.Model) ISerializer
	// GetPartialSerializer 获取具体Model的Partial序列化实例
	GetPartialSerializer(*model.Model) ISerializer

	// GetPrimaryKey 获取PrimaryKey
	GetPrimaryKey(*gin.Context) interface{}
}

IResource 接口定义,运行时可以调用

func ResourceFromContext

func ResourceFromContext(c *gin.Context) IResource

ResourceFromContext 从 ctx 里读取 Resource

type IRoot

type IRoot interface {
	RegisterResource(string, IController)
	Mount(*gin.RouterGroup)
	GetValidator() IValidator
	Print(string)
	Validate() *validator.Validate
}

type ISearch

type ISearch interface {
	Search(*gin.Context) Response
}

type ISearchInit

type ISearchInit interface {
	InitSearch(IResource)
}

type ISerializer

type ISerializer interface {
	WithDefaults([]string) ISerializer
	Parse(*gin.Context, []byte) error
	ParseFromQuery(*gin.Context, map[string]string) error
	ParseFromBody(*gin.Context) error
	Validate(*gin.Context) *response.Error
	ValidateData() map[string]interface{}
	GetWithDefault(string, interface{}) interface{}
	Get(string) (interface{}, error)
	JsonData() map[string]interface{}
	StructData() interface{}
}

type IValidator

type IValidator interface {
	Register(interface{})
	Validate(context.Context, interface{}) *response.Error
	ValidatePartial(context.Context, interface{}, []string) *response.Error
}

type MethodType

type MethodType int32

MethodType 操作方法类型定义

const (
	ListMethod   MethodType = 1
	DetailMethod MethodType = 2
)

Restful请求方法类型,针对列表和详情页有单独的处理

type ModelValidator

type ModelValidator interface {
	Validate(validator.StructLevel)
}

type ModelValidatorCtx

type ModelValidatorCtx interface {
	ValidateCtx(context.Context, validator.StructLevel)
}

type RequestBody

type RequestBody struct {
	Have  bool
	Value []byte
	Req   *http.Request
}

RequestBody 请求body,为了支持修改专门设置

func NewRequestBody

func NewRequestBody(req *http.Request) (*RequestBody, error)

NewRequestBody 创建RequestBody

func RequestBodyFromContext

func RequestBodyFromContext(c *gin.Context) *RequestBody

RequestBodyFromContext 从 ctx 里读取 RequestBody

func (*RequestBody) Get

func (rb *RequestBody) Get() []byte

func (*RequestBody) Set

func (rb *RequestBody) Set(b []byte)

type Resource

type Resource struct {
	*Controller
	// 必选
	DB    *gorm.DB
	Model *model.Model
	// contains filtered or unexported fields
}

Resource 定义 Restful Resource 结构体

func NewResource

func NewResource(m IModel) *Resource

func (*Resource) GetDB

func (resource *Resource) GetDB() *gorm.DB

func (*Resource) GetModel

func (resource *Resource) GetModel() *model.Model

func (*Resource) GetPartialSerializer

func (resource *Resource) GetPartialSerializer(m *model.Model) ISerializer

func (*Resource) GetPrimaryKey

func (resource *Resource) GetPrimaryKey(c *gin.Context) interface{}

func (*Resource) GetSerializer

func (resource *Resource) GetSerializer(m *model.Model) ISerializer

func (*Resource) Init

func (resource *Resource) Init(instance interface{}, root IRoot)

Init Resource 初始化

func (*Resource) Query

func (resource *Resource) Query() *gorm.DB

func (*Resource) QueryPrimaryKey

func (resource *Resource) QueryPrimaryKey(c *gin.Context) *gorm.DB

func (*Resource) QueryWithContext

func (resource *Resource) QueryWithContext(ctx *gin.Context) *gorm.DB

type Response

type Response interface {
	Response(*gin.Context)
}

type Serializer

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

func NewSerializer

func NewSerializer(m *model.Model, v IValidator, partial bool) *Serializer

NewSerializer Serializer 实例化,设置partial=true后,不会解析默认值,不会校验未传入的字段

func (*Serializer) Get

func (s *Serializer) Get(key string) (interface{}, error)

Get 务必在 Validate 后调用,这里为了使用方便,未做错误检查 该函数的key是json/form的key key不存在时,会返回error

func (*Serializer) GetWithDefault

func (s *Serializer) GetWithDefault(key string, value interface{}) interface{}

GetWithDefault 务必在 Validate 后调用,这里为了使用方便,未做错误检查 否则只能返回两个参数,不便于调用 该函数的key是json/form的key

func (*Serializer) JsonData

func (s *Serializer) JsonData() map[string]interface{}

JsonData 务必在 Validate 后调用,这里为了使用方便,未做错误检查 否则只能返回两个参数,不便于调用 该函数返回的map,key为数据库列,不一定是json/form的key

func (*Serializer) Parse

func (s *Serializer) Parse(c *gin.Context, b []byte) error

Parse 注意返回的是model的指针

func (*Serializer) ParseFromBody

func (s *Serializer) ParseFromBody(c *gin.Context) error

ParseFromBody 从request解析,对Parse的封装

func (*Serializer) ParseFromQuery

func (s *Serializer) ParseFromQuery(c *gin.Context, query map[string]string) error

ParseFromQuery 注意返回的是model的指针

func (*Serializer) StructData

func (s *Serializer) StructData() interface{}

StructData 务必在 Validate 后调用,这里为了使用方便,未做错误检查 该函数返回的类型与model相同,可以断言转回

func (*Serializer) Validate

func (s *Serializer) Validate(c *gin.Context) *response.Error

Validate 根据struct tag校验输入数据

func (*Serializer) ValidateData

func (s *Serializer) ValidateData() map[string]interface{}

ValidateData 务必在 Validate 后调用,这里为了使用方便,未做错误检查 否则只能返回两个参数,不便于调用 该函数返回的map,key为数据库列,不一定是json/form的key 注意,该方法应仅在需要存储数据到db时使用,参数解析务必使用 JsonData 方法

func (*Serializer) WithDefaults

func (s *Serializer) WithDefaults(keys []string) ISerializer

WithDefaults 固定解析部分字段的默认值

type Validator

type Validator struct {
	Validator *validator.Validate
}

func (*Validator) Register

func (v *Validator) Register(model interface{})

func (*Validator) Validate

func (v *Validator) Validate(ctx context.Context, data interface{}) *response.Error

func (*Validator) ValidatePartial

func (v *Validator) ValidatePartial(ctx context.Context, data interface{}, fields []string) *response.Error

Directories

Path Synopsis
Package model
Package model

Jump to

Keyboard shortcuts

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