@(示例笔记本)[马克飞象|帮助|Markdown]
GIN API Framework是一款专为Go Gin 框架打造的API Framework,通过精心的设计与技术实现,集成了大部分稳定开发组件,memcache consistance Hash,redis,nsq,api doc ,mysql 等。特点概述:
[TOC]
$ go run main.go
$ go run gen_doc.go
$ go run orm_sync.go orm syncdb
$ go run main.go $ open http://127.0.0.1:8080/doc
// @Title User Query By ID // @API_GROUP User // @Description 查询用户接口通过用户ID // @Success 200 {object} // @Param uid query string false "user id" // @Failure 400 no enough input // @Failure 500 get common error // @router /user/query [get] func UserQueryByIdHandler(c *gin.Context) { suid := c.Query("uid") uid , error := strconv.Atoi(suid) if error != nil { c.JSON(400, gin.H{ "status": "fail", "msg": "字符串转换成整数失败", }) return } u := user.UserQueryById(uid) c.JSON(http.StatusOK, gin.H { "status": "success", "user": u, }) }
func UserList() (users []User) { o := orm.NewOrm() qs := o.QueryTable("user") var us []User cnt, err := qs.Filter("id__gt", 0).OrderBy("-id").Limit(10, 0).All(&us) if err == nil { fmt.Printf("count", cnt) for _, u := range us { fmt.Println(u) } } return us }
inmem_store := cache.NewInMemoryStore(time.Second) memcached_store := cache.NewMemcachedStore([]string{"localhost:11211"},time.Minute * 5) v1.GET("/list", cache.CachePage(inmem_store, time.Minute * 5 ,controllers.UserListHandler))
router.Use(nice.Recovery(recoveryHandler)) func recoveryHandler(c *gin.Context, err interface{}) { c.JSON(400, gin.H{ "status": "fail", "err": err, }) }
ginpprof.Wrapper(router)} go http://127.0.0.1:8080/debug/pprof/
package main import ( "Gin_API_Framework/utils/redis_model" _"encoding/json" "log" ) func sync_hello(dic map[string]interface{}) { log.Println("[sync_hello]...") log.Println("[recive dict]",dic) for key,value:=range dic { log.Println(key,value) } } func aysnc_do(queue *redis_model.RedisQueue) { value := map[string]interface{}{} value["hello"] = 1 value["world"] = 2 queue.ASync(value) } func main(){ queue := redis_model.NewRedisQueue("channel.test") aysnc_do(queue) //queue do work queue.Do(sync_hello) }
API goroutine monitor profile https://github.com/DeanThompson/ginpprof
run_api
$ go run web_main.go
type MainController struct { beego.Controller } func (this *MainController) Get() { this.TplName = "index.html" this.Layout = "layout/layout.html" this.Render() }