gev

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

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

Go to latest
Published: Aug 29, 2017 License: MIT Imports: 19 Imported by: 0

README

gev

一个基于 gin 的 restful 框架

# 安装
go get github.com/inu1255/gev
功能
  • 直接将普通的 struct 或 func 转换成接口
  • 自动生成swagger接口文档
  • 可以通过注释修改接口 relativePath,httpMethod 以及为swagger添加说明
示例
package main

import "github.com/inu1255/gev"

type User struct {
    Name     string
    Password string
}

// 只有返回 (xxx, error) 的函数会被路由
func (this *User) Other(s string) string {
    return s
}

// @desc 返回 一个json对象
func (this *User) Hello(s string) (*User, error) {
    user := &User{
        Name:     "hello",
        Password: s,
    }
    return user, nil
}

// 包含一个 struct或者pointer 时使用 POST
// @desc post 一个json对象
// @desc 第二个的介绍
// @desc 第三个的介绍
func (this *User) World(user *User) (string, error) {
    return user.Name + " " + user.Password, nil
}

// @desc 说你好
// @path /say
func SayHello(name string) (string, error) {
    return "hello " + name, nil
}

func main() {
    app := gev.New()

    user := new(User)
    maker := gev.NewRouteMaker()
    //  将实例映射成两个接口
    //  /user/hello
    //  /user/world
    maker.AddRoute(user)
    // 将函数映射成
    // /say          ps: 默认是 /say/hello 通过33行的 `@path /say` 修改
    maker.AddRoute(SayHello)
    maker.RouteAll(app)

    app.Swagger("/api")
    app.Run(":8017")
}
运行
go run main.go

访问 http://localhost:8017/api/

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Log = log.New(os.Stdout, "[ gev ] ", log.Ltime|log.Lshortfile)

Functions

func AutoRestart

func AutoRestart()

func BodyManager

func BodyManager(param *Param)

func CloneValue

func CloneValue(src reflect.Value) reflect.Value

func ContextManager

func ContextManager(param *Param)

func CrossDomainMW

func CrossDomainMW() gin.HandlerFunc

func DefaultManager

func DefaultManager(param *Param)

default paramManager

func Error

func Error(code int, msg string) error

func FileManager

func FileManager(param *Param)

func SelfManager

func SelfManager(param *Param)

self param for struct

func SetErrorHander

func SetErrorHander(eh IErrorHander)

Types

type BaseService

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

func (*BaseService) After

func (this *BaseService) After(data interface{}, err error)

func (*BaseService) Before

func (this *BaseService) Before(ctx *gin.Context) bool

func (*BaseService) Finish

func (this *BaseService) Finish(err interface{})

type Class

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

func (*Class) New

func (this *Class) New() IClass

func (*Class) Self

func (this *Class) Self() IClass

IClass 接口

func (*Class) SetSelf

func (this *Class) SetSelf(self IClass)

type Engine

type Engine struct {
	*gin.Engine
	// contains filtered or unexported fields
}

func New

func New() *Engine

func (*Engine) Any

func (this *Engine) Any(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*Engine) Body

func (this *Engine) Body(body interface{}) IRouter

func (*Engine) DELETE

func (this *Engine) DELETE(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*Engine) Data

func (this *Engine) Data(data interface{}) IRouter

func (*Engine) FileParam

func (this *Engine) FileParam(name, desc string) *core.Param

func (*Engine) GET

func (this *Engine) GET(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*Engine) Group

func (this *Engine) Group(relativePath string, handlers ...gin.HandlerFunc) *RouterGroup

func (*Engine) HEAD

func (this *Engine) HEAD(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*Engine) Handle

func (this *Engine) Handle(httpMethod string, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*Engine) Info

func (this *Engine) Info(info ...string) IRouter

func (*Engine) OPTIONS

func (this *Engine) OPTIONS(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*Engine) PATCH

func (this *Engine) PATCH(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*Engine) POST

func (this *Engine) POST(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*Engine) PUT

func (this *Engine) PUT(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*Engine) PathParam

func (this *Engine) PathParam(name, desc string) *core.Param

func (*Engine) QueryParam

func (this *Engine) QueryParam(name, desc string) *core.Param

func (*Engine) Run

func (this *Engine) Run(addr ...string) (err error)

func (*Engine) Swagger

func (this *Engine) Swagger(relativePath string)

type ErrorHander

type ErrorHander int

func (*ErrorHander) Api

func (this *ErrorHander) Api(c *gin.Context, data interface{}, err error)

func (*ErrorHander) Err

func (this *ErrorHander) Err(c *gin.Context, code int, err error)

func (*ErrorHander) Ok

func (this *ErrorHander) Ok(c *gin.Context, data interface{})

type IClass

type IClass interface {
	Self() IClass
	SetSelf(IClass)
	New() IClass
}

type IErrorHander

type IErrorHander interface {
	Ok(c *gin.Context, data interface{})
	Err(c *gin.Context, code int, err error)
	Api(c *gin.Context, data interface{}, err error)
}

处理错误接口

type IRouter

type IRouter interface {
	DELETE(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes
	GET(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes
	HEAD(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes
	OPTIONS(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes
	PATCH(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes
	POST(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes
	PUT(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes
	Handle(httpMethod string, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes
	Group(string, ...gin.HandlerFunc) *RouterGroup
	Body(body interface{}) IRouter
	Data(data interface{}) IRouter
	Info(info ...string) IRouter
	QueryParam(name, desc string) *core.Param
	PathParam(name, desc string) *core.Param
	FileParam(name, desc string) *core.Param
}

type IService

type IService interface {
	Before(ctx *gin.Context) bool
	Finish(err interface{})
	After(data interface{}, err error)
}

type ITagName

type ITagName interface {
	TagName(string) string
}

type Method

type Method struct {
	Doc        map[string][]string // remment info
	Name       string
	StructName string // "" if not a struct function
	Func       reflect.Value
	Params     []*Param
	OutType    reflect.Type
	Tag        string
}

func (*Method) GetTag

func (this *Method) GetTag() string

func (*Method) HttpMethod

func (this *Method) HttpMethod() string

httpmethod from remment like below @method DELETE or return POST if contains body Param

func (*Method) OnlyOneParam

func (this *Method) OnlyOneParam() bool

func (*Method) ParamDesc

func (this *Method) ParamDesc(paramName string) string

param description from remment like below @param paramName paramDescs

func (*Method) Path

func (this *Method) Path() (path string)

route path from remment like below @path /foo/bar or return path made by methodName and pathParams

func (*Method) RecvType

func (this *Method) RecvType() reflect.Type

return struct type if this is a struct method

type Param

type Param struct {
	Name   string
	In     string // param in path,query,body or file
	Desc   string
	Type   reflect.Type
	New    paramLoader
	Method *Method
}

type RouteMaker

type RouteMaker struct {
	Methods []*Method
	// contains filtered or unexported fields
}

func NewRouteMaker

func NewRouteMaker() *RouteMaker

func (*RouteMaker) AddMethod

func (this *RouteMaker) AddMethod(f interface{}, tags ...string) *Method

read route information from one function

func (*RouteMaker) AddParamManager

func (this *RouteMaker) AddParamManager(fn paramManager)

add paramManager function LIFO the last add paramManager match condition will take effect

func (*RouteMaker) AddRoute

func (this *RouteMaker) AddRoute(f interface{}, tags ...string)

read route information,save to this.Methods

func (*RouteMaker) RouteTo

func (this *RouteMaker) RouteTo(app IRouter)

add route to app

type RouterGroup

type RouterGroup struct {
	*gin.RouterGroup
	// contains filtered or unexported fields
}

func (*RouterGroup) Any

func (this *RouterGroup) Any(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*RouterGroup) Body

func (this *RouterGroup) Body(body interface{}) IRouter

func (*RouterGroup) DELETE

func (this *RouterGroup) DELETE(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*RouterGroup) Data

func (this *RouterGroup) Data(data interface{}) IRouter

func (*RouterGroup) FileParam

func (this *RouterGroup) FileParam(name, desc string) *core.Param

func (*RouterGroup) GET

func (this *RouterGroup) GET(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*RouterGroup) Group

func (this *RouterGroup) Group(relativePath string, handlers ...gin.HandlerFunc) *RouterGroup

func (*RouterGroup) HEAD

func (this *RouterGroup) HEAD(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*RouterGroup) Handle

func (this *RouterGroup) Handle(httpMethod string, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*RouterGroup) Info

func (this *RouterGroup) Info(info ...string) IRouter

func (*RouterGroup) OPTIONS

func (this *RouterGroup) OPTIONS(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*RouterGroup) PATCH

func (this *RouterGroup) PATCH(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*RouterGroup) POST

func (this *RouterGroup) POST(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*RouterGroup) PUT

func (this *RouterGroup) PUT(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

func (*RouterGroup) PathParam

func (this *RouterGroup) PathParam(name, desc string) *core.Param

func (*RouterGroup) QueryParam

func (this *RouterGroup) QueryParam(name, desc string) *core.Param

Jump to

Keyboard shortcuts

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