You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+17-11Lines changed: 17 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
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.
4
4
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.
6
6
7
7
## Software Prerequisites
8
8
@@ -13,8 +13,8 @@ Please make sure your IP is added to the whitelist in your MongoDB Atlas project
13
13
14
14
Let us create a simple Go service with two endpoints:
15
15
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
@@ -62,7 +68,7 @@ On the MongoDB Atlas you always have not just a single database server, but a cl
62
68
63
69

64
70
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.
66
72
67
73
Adding the initialization of the session variable completes the first step:
68
74
@@ -87,20 +93,20 @@ func main() {
87
93
88
94
## Using the mongo connection
89
95
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?
91
97
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?
93
99
94
100
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).
95
101
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:
97
103
98
104
```
99
105
session := mongoConn.Copy() // "session" can be used safely
100
106
defer session.Close()
101
107
```
102
108
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.
_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._
142
148
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.
144
150
145
151
We are ready to test our endpoints!
146
152
147
153
## Testing the endpoints
148
154
149
-
You can start the service with following command (don't forget a point at the end):
0 commit comments