Go和Gin:传递struct的数据库上下文?

Tan*_*per 8 dependency-injection go go-gin

我刚刚开始尝试Go,我正在寻找重新实现用它编写的节点中的API服务器.

尝试使用依赖注入来传递数据库上下文作为杜松子酒中间件,我遇到了障碍.到目前为止,我已将其设置为:

main.go:

package main

import (
        "fmt"
        "runtime"
        "log"
        "github.com/gin-gonic/gin"
        "votesforschools.com/api/public"
        "votesforschools.com/api/models"
)

type DB struct {
        models.DataStore
}

func main() {
        ConfigRuntime()
        ConfigServer()
}

func Database(connectionString string) gin.HandlerFunc {
        dbInstance, err := models.NewDB(connectionString)
        if err != nil {
                log.Panic(err)
        }

        db := &DB{dbInstance}

        return func(c *gin.Context) {
                c.Set("DB", db)
                c.Next()
        }
}


func ConfigRuntime() {
        nuCPU := runtime.NumCPU()
        runtime.GOMAXPROCS(nuCPU)
        fmt.Printf("Running with %d CPUs\n", nuCPU)
}

func ConfigServer() {

        gin.SetMode(gin.ReleaseMode)

        router := gin.New()
        router.Use(Database("<connectionstring>"))
        router.GET("/public/current-vote-pack", public.GetCurrentVotePack)
        router.Run(":1000")
}
Run Code Online (Sandbox Code Playgroud)

车型/ db.go

package models

import (
        "database/sql"
        _ "github.com/go-sql-driver/mysql"
)

type DataStore interface {
        GetVotePack(id string) (*VotePack, error)
}

type DB struct {
        *sql.DB
}

func NewDB(dataSource string) (*DB, error) {
        db, err := sql.Open("mysql", dataSource)
        if err != nil {
                return nil, err
        }
        if err = db.Ping(); err != nil {
                return nil, err
        }
        return &DB{db}, nil
}
Run Code Online (Sandbox Code Playgroud)

车型/ votepack.go

package models

import (
        "time"
        "database/sql"
)

type VotePack struct {
        id string
        question string
        description string
        startDate time.Time
        endDate time.Time
        thankYou string
        curriculum []string
}

func (db *DB) GetVotePack(id string) (*VotePack, error) {

        var votePack *VotePack

        err := db.QueryRow(
                "SELECT id, question, description, start_date AS startDate, end_date AS endDate, thank_you AS thankYou, curriculum WHERE id = ?", id).Scan(
                &votePack.id, &votePack.question, &votePack.description, &votePack.startDate, &votePack.endDate, &votePack.thankYou, &votePack.curriculum)

        switch {
        case err == sql.ErrNoRows:
                return nil, err
        case err != nil:
                return nil, err
         default:
                return votePack, nil
        }
}
Run Code Online (Sandbox Code Playgroud)

因此,有了上述所有内容,我想将models.DataSource作为中间件传递,因此可以像这样访问:

公共/ public.go

package public

import (
        "github.com/gin-gonic/gin"
)

func GetCurrentVotePack(context *gin.Context) {
        db := context.Keys["DB"]

        votePack, err := db.GetVotePack("c5039ecd-e774-4c19-a2b9-600c2134784d")
        if err != nil{
                context.String(404, "Votepack Not Found")
        }
        context.JSON(200, votePack)
}
Run Code Online (Sandbox Code Playgroud)

但是我得到了 public\public.go:10: db.GetVotePack undefined (type interface {} is interface with no methods)

当我在调试器中检查(使用带插件的Webstorm)时,db只是一个空对象.我正努力做好并避免使用全局变量

小智 15

我认为不context应该用作 DI 容器:https : //golang.org/pkg/context/

包上下文定义了上下文类型,它在 API 边界和进程之间携带截止日期、取消信号和其他请求范围的值。

我宁愿使用:

package public

type PublicController struct {
        Database *DB
}

func (c *PublicController) GetCurrentVotePack(context *gin.Context) {
        votePack, err := c.Database.GetVotePack("c5039ecd-e774-4c19-a2b9-600c2134784d")
        if err != nil{
                context.String(404, "Votepack Not Found")
        }
        context.JSON(200, votePack)
}
Run Code Online (Sandbox Code Playgroud)

并在 main 中配置您的控制器:

func main() {
        pCtrl := PublicController { Database: models.NewDB("<connectionstring>") }

        router := gin.New()
        router.GET("/public/current-vote-pack", pCtrl.GetCurrentVotePack)
        router.Run(":1000")
}
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案。如此处所述,上下文不应携带任何数据库配置(或类似配置)。 (3认同)

Sno*_*lem 8

其中的值context.Keys都是类型interface{},因此在转换回该类型之前,db无法从类型调用方法*DB.

安全的方式:

db, ok := context.Keys["DB"].(*DB)
if !ok {
        //Handle case of no *DB instance
}
// db is now a *DB value
Run Code Online (Sandbox Code Playgroud)

不太安全的方式,如果context.Keys["DB"]不是类型的值,将会恐慌*DB:

db := context.Keys["DB"].(*DB)
// db is now a *DB value
Run Code Online (Sandbox Code Playgroud)

Effective Go有一节关于此.

  • 虽然这解决了问题,但对我来说,它看起来是非常糟糕的设计。依赖某人将 interface{} 结构放入通用上下文中看起来不像正确的强类型设计。我想大部分错误是因为 gin 需要一个用于处理方法的函数而不是接口 (2认同)