Skip to content

Commit b8334d9

Browse files
committed
README.md
1 parent da89ed8 commit b8334d9

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
They say, Golang is a very good choice, if you build a service for being deployed in the cloud infrastructure. And that is mostly true. However, if you have a stateful service with some database in the backend, you may find it difficult to setup such cloud infrastructure and to establish a communication between the Go service and the database server.
44

5-
Fortunately, there are solutions already in existence that simplify our life. For example, if you want to have your data stored in MongoDB, you may just use MongoDB Atlas - a fully [managed database service in the cloud](https://www.mongodb.com/cloud/atlas). We do not explain here, how to setup a MongoDB cluster there. It is very well done [here](https://docs.atlas.mongodb.com/getting-started/). We focus on how to create a connection to MongoDB Atlas with Go and interact with the database cluster.
5+
Fortunately, there are solutions already in existence that simplify our life. For example, if you want to have your data stored in MongoDB, you may just use MongoDB Atlas - a fully [managed database service in the cloud](https://www.mongodb.com/cloud/atlas). We do not explain here, how to setup a MongoDB cluster. It is very well done [here](https://docs.atlas.mongodb.com/getting-started/). We focus on how to create a connection to MongoDB Atlas with Go and interact with the database cluster.
66

77
## Software Prerequisites
88

@@ -13,8 +13,8 @@ Please make sure your IP is added to the whitelist in your MongoDB Atlas project
1313

1414
Let us create a simple Go service with two endpoints:
1515

16-
- `/save` to receive a record to store
17-
- `/read` to read the previously stored record
16+
- `/save` to receive a record and to store it
17+
- `/read` to return the previously stored record back
1818

1919
The service will listen at the port `8080`:
2020

@@ -45,15 +45,21 @@ func get(w http.ResponseWriter, req *http.Request) {}
4545
```
4646
func createConnection() (*mgo.Session, error) {
4747
dialInfo := mgo.DialInfo{
48-
Addrs: []string{"abc-shard-00-00.gcp.mongodb.net:27017", "abc-shard-00-01.gcp.mongodb.net:27017", "abc-shard-00-02.gcp.mongodb.net:27017"},
48+
Addrs: []string{
49+
"abc-shard-00-00.gcp.mongodb.net:27017",
50+
"abc-shard-00-01.gcp.mongodb.net:27017",
51+
"abc-shard-00-02.gcp.mongodb.net:27017"
52+
},
4953
Username: "MongoUser", // your mongodb user
5054
Password: "YourVerySecurePassword", // ...and mongodb password
5155
}
56+
5257
tlsConfig := &tls.Config{}
5358
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
5459
conn, err := tls.Dial("tcp", addr.String(), tlsConfig) // add TLS config
5560
return conn, err
5661
}
62+
5763
return mgo.DialWithInfo(&dialInfo)
5864
}
5965
```
@@ -62,7 +68,7 @@ On the MongoDB Atlas you always have not just a single database server, but a cl
6268

6369
![Shard Addresses](images/shards.png "Shards")
6470

65-
We also added a TLS config into our code, because the MongoDB Atlas denies unencrypted connections.
71+
We also added a TLS config into the code, because the MongoDB Atlas denies unencrypted connections.
6672

6773
Adding the initialization of the session variable completes the first step:
6874

@@ -87,20 +93,20 @@ func main() {
8793

8894
## Using the mongo connection
8995

90-
What we have now is a singleton `mongoConn` that can be used directly which is not a good idea. Why create it at all? Why cannot we establish a connection every time the client calls our endpoints?
96+
What we have now is a singleton `mongoConn` that can be used directly which is not a good idea. Why create it at all? Why cannot we establish a connection every time the client app calls our endpoints?
9197

92-
Because `mgo.DialWithInfo(...)` can take several seconds before the connection to the MongoDB Atlas is ready. There is a couple of necessary steps like sending and accepting certificates, authorization etc. that needs to be done, before your service can proceed to the next step.
98+
Because `mgo.DialWithInfo(...)` can take several seconds before the connection to the MongoDB Atlas is ready. There is a couple of necessary steps like sending and accepting certificates, authorization etc. that needs to be done, before your service can proceed to the next step. You probably want your endpoints to answer within milliseconds, not seconds, right?
9399

94100
And of course you cannot use the singleton `mongoConn` in all your endpoints for an obvious reason (due to the side effects by using of a common connection in concurrent HTTP sessions).
95101

96-
So, we use a copy of the singleton `mongoConn` which works quick and safe:
102+
So, we use a copy of the singleton `mongoConn` which works quick enough and safe:
97103

98104
```
99105
session := mongoConn.Copy() // "session" can be used safely
100106
defer session.Close()
101107
```
102108

103-
Let us implement `/save` and `/read` now. We store the data in a sort of generic way: everything what the client app sends us we are going to save into the Mongo database as a byte array.
109+
Let us implement `/save` and `/read` now. We store the data in a sort of generic way: everything what the client app sends us we are going to store in the Mongo database as a byte array.
104110

105111
```
106112
type MyEntity struct {
@@ -140,13 +146,13 @@ func get(w http.ResponseWriter, req *http.Request) {
140146

141147
_Normally you would not want to `panic` in case of an error, but return an HTTP error code in the response. However, we want to keep it simple for now._
142148

143-
Do not forget to close a copy of your session. MongoDB Atlas considers sessions as a resource and like every database server has a limit for the amount of opened connections.
149+
Do not forget to close a copy of your session. MongoDB Atlas considers sessions as a resource and like every other database server has a limit for the amount of opened connections.
144150

145151
We are ready to test our endpoints!
146152

147153
## Testing the endpoints
148154

149-
You can start the service with following command (don't forget a point at the end):
155+
You can start the service with following command:
150156

151157
```
152158
> go run main.go .

0 commit comments

Comments
 (0)