我正在开发一个在Golang和使用Mongo的数据库后端的网络应用程序.然而,我意识到,每当我没有互联网连接或使用公共wifi时,我就无法连接到我的Mongo Atlas.这将导致我的本地服务器死机,我将不得不依赖于特定的连接来运行我的应用程序和自检.
我有没有办法在没有适当的互联网连接的情况下继续发展?如有任何帮助,我们不胜感激.下面是我初始化数据库的代码.
玩.
func main() {
fmt.Println("hello world")
ctx := context.Background()
config.InitializeConfig()
dbDisconnect := database.InitializeDatabase(ctx)
defer func() {
if err := dbDisconnect(ctx); err != nil {
panic(err)
}
}()
// Other Initializations and App logic
}
InitializeDb.go
var clientInstance *mongo.Client
func InitializeDatabase(ctx context.Context) func(ctx context.Context) error {
serverAPI := options.ServerAPI(options.ServerAPIVersion1)
opts := options.Client().ApplyURI(getDBLink()).SetServerAPIOptions(serverAPI)
client, err := mongo.Connect(ctx, opts)
if err != nil {
panic(err)
}
dc := client.Disconnect
clientInstance = client
if err := client.Database("admin").RunCommand(context.TODO(), bson.D{{Key: "ping", Value: 1}}).Err(); err != nil {
panic(err)
}
fmt.Println("Pinged your deployment. You successfully connected to db")
return dc
}