首页 > 解决方案 > 如何使用 go 和 mongodb 在 heroku 上部署应用程序而不会出错?

问题描述

无法部署。收到错误:

cannot load go.mongodb.org/mongo-driver/mongo: open /tmp/build_aa982e7b99ad67f15e2c45be4077d6e9/vendor/go.mongodb.org/mongo-driver/mongo: no such file or directory

我尝试过导入方式:

1)“go.mongodb.org/mongo-driver/mongo”

在本地工作正常,但在部署期间崩溃

2)“github.com/mongodb/mongo-go-driver/mongo”

不工作

main.go:

package main

import (
    "fmt"
    "log"
    "net/http"
    "os"
    "context"

    "github.com/gin-gonic/gin"
    _ "github.com/heroku/x/hmetrics/onload"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

const (
    usernameKey = "xxx"
    passwordKey = "xxx"
    hostKey     = "ds131905.mlab.com:31905"
    databaseKey = "heroku_zj4jbgpn"
)

func main() {
    port := os.Getenv("PORT")

    if port == "" {
        log.Fatal("$PORT must be set")
    }

    router := gin.New()
    router.Use(gin.Logger())
    router.LoadHTMLGlob("templates/*.tmpl.html")
    router.Static("/static", "static")

    router.GET("/", func(c *gin.Context) {
        c.HTML(http.StatusOK, "index.tmpl.html", nil)
    })

    connectToMongoDb()

    router.Run(":" + port)
}


func connectToMongoDb() {
    ctx := context.TODO()

    db, err := configDB(ctx)
    if err != nil {
        log.Fatalf("Database configuration failed: %v", err)
    }

    fmt.Printf("Found a single document: %+v\n", db)

}

func configDB(ctx context.Context) (*mongo.Database, error) {
    uri := fmt.Sprintf(`mongodb://%s:%s@%s/%s`,
        usernameKey,
        passwordKey,
        hostKey,
        databaseKey,
    )
    client, err := mongo.NewClient(options.Client().ApplyURI(uri))
    if err != nil {
        return nil, fmt.Errorf("couldn't connect to mongo: %v", err)
    }

    err = client.Connect(ctx)
    if err != nil {
        return nil, fmt.Errorf("todo: mongo client couldn't connect with background context: %v", err)
    }
    fmt.Println("----->>>>> Mongo client CONNECTED!!!")
    todoDB := client.Database(databaseKey)
    return todoDB, nil
}

部署日志:

-----> Go app detected
-----> Fetching stdlib.sh.v8... done
-----> 
       Detected go modules via go.mod
-----> 
       Detected Module Name: github.com/heroku/go-getting-started
-----> 
 !!    The go.mod file for this project does not specify a Go version
 !!    
 !!    Defaulting to go1.12.5
 !!    
 !!    For more details see: https://devcenter.heroku.com/articles/go-apps-with-modules#build-configuration
 !!    
-----> Using go1.12.5
-----> Determining packages to install

       Detected the following main packages to install:
            github.com/heroku/go-getting-started

-----> Running: go install -v -tags heroku -mod=vendor github.com/heroku/go-getting-started 
build github.com/heroku/go-getting-started: cannot load go.mongodb.org/mongo-driver/mongo: open /tmp/build_aa982e7b99ad67f15e2c45be4077d6e9/vendor/go.mongodb.org/mongo-driver/mongo: no such file or directory
 !     Push rejected, failed to compile Go app.
 !     Push failed

.bash_progile

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

项目地点:

/Users/denis/go/src/github.com/heroku/aqueous-ridge-14148/

Mongo司机位置:

/Users/denis/go/src/go.mongodb.org/mongo-driver/

标签: mongodbgoheroku

解决方案


我需要从目录中移动我的应用程序并使用教程中的$GOPATH/src方法安装 mongodb 驱动程序。go get go.mongodb.org/mongo-driver


推荐阅读