Build Restapi With Gin and Mgo

  1. 首先安装govendor
1
go get -u github.com/kardianos/govendor

并添加 $GOPATH/bin/ 到环境变量中 这样可以执行 govendor 命令来确认已经完成安装

  1. 新建目录 例如 $GOPATH/src/gin-mgo-api 并创建一个 main.go, 然后 govendor init 初始化之
  2. 使用 govendor 来管理依赖
1
2
govendor fetch github.com/gin-gonic/gin
govendor fetch gopkg.in/mgo.v2
  1. 创建MongoDB数据连接

db/connect.go

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package db

import (
	"fmt"
	"gopkg.in/mgo.v2"
)

var (
	Session *mgo.Session
	Mongo *mgo.DialInfo
)

const (
	MongoDBUrl = "mongodb://localhost:27017/super"
)

func Connect() {
	uri := MongoDBUrl
	mongo, err := mgo.ParseURL(uri)
	s, err := mgo.Dial(uri)
	if err != nil {
		fmt.Printf("Can't connect to mongo, go error %v\n", err)
		panic(err.Error())
	}
	s.SetSafe(&mgo.Safe{})
	fmt.Println("Connected to", uri)
	Session = s
	Mongo = mongo
}
  1. 创建数据库连接的中间件

middlewares/middlewares.go

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package middlewares

import (
	"fmt"
	"gin-mgo-api/db"
	"github.com/gin-gonic/gin"
)

func Connect(c *gin.Context) {
	fmt.Println("Connect Middleware")
	if db.Session == nil {
		fmt.Println("Error DB Session")
	}
	s := db.Session.Clone()

	defer s.Close()

	c.Set("db", s.DB(db.Mongo.Database))
	c.Next()  //这一句是必须的, 否则数据连接在跑完这个方法之后就被关闭了
}

func Middleware(c *gin.Context) {
	fmt.Println("this is a middleware!")
}
  1. 建立一个Model

models/super.go

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package models

import (
	"gopkg.in/mgo.v2/bson"
)


type Super struct {
	Id    bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
	Name  string
	Value string
}
  1. 好了可以开始写点路由啥的了

main.go

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main

import (
	"gin-mgo-api/db"
	"gin-mgo-api/middlewares"
	"gin-mgo-api/models"
	"net/http"

	"github.com/gin-gonic/gin"
	mgo "gopkg.in/mgo.v2"
)

//在启动时先建立数据库连接
func init() {
	db.Connect()
}

func main() {
	router := gin.Default()
	gin.SetMode(gin.DebugMode)

	router.Use(middlewares.Connect)
	router.GET("/someGet", func(c *gin.Context) {
        //每个接口都可以用这样的方式获得数据库操作对象
		db := c.MustGet("db").(*mgo.Database)
		supers := []models.Super{}
        //没数据可以先插进去几条, 或者用MongoDB Compass操作
		// super := models.Super{Name: "hhhh", Value: "iiii"}
		// err := db.C("super").Insert(&super)
		err := db.C("super").Find(nil).All(&supers)
		if err != nil {
			c.Error(err)

		}
		c.JSON(http.StatusOK, supers)

	})

    //这里也可以这样 router.Run(":9090") 暂时还不知道有什么区别
    
	http.ListenAndServe(":9090", router)
}
Licensed under CC BY-NC-SA 4.0
记录平时瞎折腾遇到的各种问题, 方便查找
使用 Hugo 构建
主题 Stack 3.29.0Jimmy 设计