go-mongodb-sample

module
v0.0.0-...-52e64fa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2024 License: MIT

README

Go MongoDB Sample

codecov Go Report Card

環境構築

openssl rand -base64 756 > mongodb-keyfile
chmod 400 mongodb-keyfile
docker-compose build
docker-compose up -d
# コレクションに対してインデックスを作成
go run app/migration/create_index.go
ローカル環境 の Replica Set の設定

mongo shell を開く。

docker exec -it go_mongodb_sample_db mongosh

以下を実行する。

rs.initiate({
  _id: "rs0",
  members: [{ _id: 0, host: "go_mongodb_sample_db:27017" }],
})
rs.status()
rs.initiate()

testdb に接続する方法。 レプリカの設定を以下のコマンドで行う必要がある。

var config = rs.conf();
config.members[0].host = "mongo_db:27017";
rs.reconfig(config, { force: true });

MongoDB Atlas に接続すると簡単にレプリカセットの動作確認ができるのでそれでもよい。

CI 用のデータベースの設定

mongo shell を開く。

docker exec -it go_mongodb_sample_db_ci mongosh

以下を実行する。

rs.initiate({
  _id: "rs0",
  members: [{ _id: 0, host: "go_mongodb_sample_db_ci:27018" }],
})
API Server の起動
# Echo API Serverの起動
go run app/main.go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL