Giter VIP home page Giter VIP logo

gin-tonic's Introduction

Build Status Coverage Status

中文 | English

Gin-tonic 能够帮助研发人员快速构建Restful API,它可以将GORM的model暴露为CRUD handler.

快速开始

需要为model实现API访问相关的接口

/* 定义模型 */
type MyModel struct {
  Name string `json:name`
  Age uint `json:age`
  Address string `json:address`
}

// 通过InsertableFields方法返回值定义可以插入的字段,例如返回 'age','name' 和 'address' 字段可以插入
func (f *Foo)InsertableFields() []string {
	return []string{"age", "name", "address"}
}

// 通过UpdateableFields方法返回值定义可以更新的字段,例如返回 'age' 和 'address' 字段可更新
func (f *Foo)UpdateableFields() []string {
	return []string{"age", "address"}
}

// 通过SortableFields方法返回值定义可以用于list接口参数指定排序的字段,例如返回 'age' 字段可用于排序
func (f *Foo)SortableFields() []string {
	return []string{"age"}
}

// 通过FilterableFields方法返回值定义可以用于list接口参数过滤字段白名单,例如返回 'age' 字段可用于筛选
func (f *Foo)FilterableFields() []string {
	return []string{"age", "name", "address"}
}

列表接口示例

import (
	"github.com/webliupeng/gin-tonic/helpers"
)

router.Get("/models", helpers.List(&MyModel{}))


// 也可以自己定义gorm的查询条件
router.Get("/models2", helpers.List(&MyModel{}, func(db *gorm.DB, c *gin.Context) {

  return db.Where("file", "=", 321)

}))

gin-tonic 生成的list handler默认支持排序,过滤,分页。

querystring参数表达式:

表达式 用法
> field_gt=val
>= field_gte=val
< field_lt=val
<= field_lte=val
like field_like=val
in field_in=val1,val2,valn
not in field_not=val1,val2,valn

分页参数:

表达式 用法
.maxResults 最大返回记录数
.offset 记录起始位置
curl http://hostname/models?.maxResults=100&.offset=10 # equals LIMIT 10, 100
curl http://hostname/models?age_lt=10  #list custerms filtered by age less than 10
curl http://hostname/models?age=10

创建记录处理器示例

创建处理器会通过gin的ShouldBindBodyWith解析请求body,如果model定义了validator的规则,将会校验请求的数据,通过才会插入数据库。

router.Post("/models", helpers.Create(func(c *gin.Context) interface{} {
  mol := &Models{}

  // 可以在这里设定默认值

  return mol
}))
curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"name": "a"}' \
  http://yourdomain/models

更新处理器示例 更新处理器会通过gin的ShouldBindBodyWith解析请求body,如果model定义了validator的规则,将会校验请求的数据,通过才会更新入数据库。

router.Put("/models/:id", 
  helpers.FindOne(&MyModel{}, "id", "mymodel"), // 通过 id 参数查询一条MyModel的记录,并将结果暂存在context
  helpers.Update("customer") // 更新mymodel
)

删除处理器示例

router.Delete("/models/:id", 
  helpers.FindOne(&Customer{}, "id", "customer"), // 通过 id 参数查询一条MyModel的记录,并将结果暂存在context
  helpers.Delete("customer") // delete the record by context name 
)

配置

  • Gin-tonic是通过Viper 来读取配置的 gin-tonic,推荐使用以下环境变量来配置数据库。
export GTC_DB_HOST=localhost
export GTC_DB_NAME=dbname
export GTC_DB_PORT=3306
export GTC_DB_USER=username
export GTC_DB_PASSWORD=password

gin-tonic's People

Contributors

webliupeng avatar garfield-yin avatar dependabot[bot] avatar

Stargazers

 avatar

Watchers

James Cloos avatar wei avatar

Forkers

ypqmbj

gin-tonic's Issues

es issue

es.go中,处理es请求时

gorequest.Endtruct()函数返回的Response, []byte, []errs, Response和errs都可能为nil,需要额外判断。

resp, body, errs := request.Post(url).Send(&dsl).EndStruct(&resultBody)

if resp.StatusCode >= 400 {
if errs == nil {
errs = []error{}
}
errs = append(errs, errors.New(string(body)))
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.