Giter Site home page Giter Site logo

eastmacro / gin_api_framework Goto Github PK

View Code? Open in Web Editor NEW

This project forked from edisonlz/gin_api_framework

0.0 1.0 0.0 25.63 MB

Gin+Beego+API+Doc

License: Apache License 2.0

Go 4.44% JavaScript 25.34% CSS 2.33% HTML 64.59% Python 0.20% PHP 2.72% ApacheConf 0.03% Roff 0.25% Smarty 0.10%

gin_api_framework's Introduction

欢迎使用GO Gin API Framework

@(示例笔记本)[马克飞象|帮助|Markdown]

GIN API Framework是一款专为Go Gin 框架打造的API Framework,通过精心的设计与技术实现,集成了大部分稳定开发组件,memcache consistance Hash,redis,nsq,api doc ,mysql 等。特点概述:

  • 功能丰富 :支持大部分服务器组件,支持API Doc;
  • 得心应手 :简单的实例,非常容易上手;
  • 深度整合 :深度整合memcache,redis,mysql,beego ,gin 框架。
  • 性能优先 :为啥不直接使用beego那,因为gin是目前httprouter性能最高,占用资源最少的框架,而beego orm是目前最接近django的框架,so...。

[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 Alt text

###框架结构 ###API 目录结构

  • controllers : API 接口代码
  • routers:API路由配置代码
  • middleware:中间件代码
  • docs: 生成api文档代码

###公共目录

  • utils:常用方法代码
  • vendor:godep save 生成依赖代码
  • background:后端定时/异步服务代码
  • models:ORM数据模型代码
  • static:静态文件代码

###Web 目录结构

  • web-controllers [controller代码]
  • web-routers [http 路由配置]
  • conf [beego config file]

代码块

// @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,
    })

}

ORM层

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,
    })
}

go pprof debug

ginpprof.Wrapper(router)}

go http://127.0.0.1:8080/debug/pprof/

go Async Queue Message by Redis

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)

}

###Profile

API goroutine monitor profile
https://github.com/DeanThompson/ginpprof

API 响应时间

#####默认情况都是微秒 Alt text

run_api

Web 后台使用beego框架构建

#####服务展示 Alt text

#####启动服务 $ go run web_main.go

#####目录框架结构

  • web-controllers [controller代码]
  • web-routers [http 路由配置]
  • conf [beego config file]
  • 其他目录例如models 为公用

#####View Controller 构建

type MainController struct {
    beego.Controller
}

func (this *MainController) Get() {
    this.TplName = "index.html"
    this.Layout = "layout/layout.html"
    this.Render()
}

#####作者:(优酷)hep

gin_api_framework's People

Contributors

edisonlz avatar vania123 avatar

Watchers

James Cloos avatar

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.