Skip to content

Commit da89ed8

Browse files
committed
README.md
1 parent 1cbe1db commit da89ed8

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,24 @@ func main() {
8989

9090
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?
9191

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 with the next step.
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.
9393

9494
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).
9595

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

9898
```
9999
session := mongoConn.Copy() // "session" can be used safely
100100
defer session.Close()
101101
```
102102

103-
Let us implement `/save` and `/read` now. We store the data in a sort of generic way: everything what the client sends us, we are going to save into the Mongo database as a byte array.
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.
104104

105105
```
106+
type MyEntity struct {
107+
Data []byte `json:"data" bson:"data"`
108+
}
109+
106110
func post(w http.ResponseWriter, req *http.Request) {
107111
payload, err := ioutil.ReadAll(req.Body)
108112
if err != nil {
@@ -136,7 +140,7 @@ func get(w http.ResponseWriter, req *http.Request) {
136140

137141
_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._
138142

139-
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 of opened connections.
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.
140144

141145
We are ready to test our endpoints!
142146

main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import (
1212

1313
var mongoConn *mgo.Session
1414

15+
type MyEntity struct {
16+
Data []byte `json:"data" bson:"data"`
17+
}
18+
1519
func createConnection() (*mgo.Session, error) {
1620
dialInfo := mgo.DialInfo{
1721
Addrs: []string{
@@ -29,10 +33,6 @@ func createConnection() (*mgo.Session, error) {
2933
return mgo.DialWithInfo(&dialInfo)
3034
}
3135

32-
type MyEntity struct {
33-
Data []byte `json:"data" bson:"data"`
34-
}
35-
3636
func main() {
3737
var err error
3838
mongoConn, err = createConnection()

0 commit comments

Comments
 (0)