mongodb - 如何使用 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/
解决方案
我需要从目录中移动我的应用程序并使用本教程中的$GOPATH/src
方法安装 mongodb 驱动程序。go get go.mongodb.org/mongo-driver
推荐阅读
- reactjs - 可以在 jsx 中使用的道具的正确类型是什么?
- python - 无法在 pyspark 中将 JSON 转换为 StructField
- amazon-cloudformation - 如何使用 cloudformation 将资源从一个 yaml 模板调用到另一个 yaml 模板
- java - 即使为 SSLContext 配置了接受所有证书的 TrustManager,Weblogic 也会使用配置的密钥库
- python - 为什么可以将文件作为字符串传递给 open 函数
- c# - Blazor Valued 已更改,同时保持 2 向绑定
- opencv - 计算像素深度
- amazon-web-services - 通过 SQS lambda 发送 SQS 消息
- serverless-framework - 避免使用无服务器创建新资源(如果存在)
- html - 面板主体引导程序中的水平滚动条