簡體   English   中英

如何使用 go 和 mongodb 在 heroku 上部署應用程序而不會出錯?

[英]How to deploy app on heroku with go and mongodb without an error?

無法部署。 得到一個錯誤:

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/

我需要從$GOPATH/src目錄移動我的應用程序並使用go get go.mongodb.org/mongo-driver安裝 mongodb 驅動程序,就像教程中一樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM