patterngoMinor
Beginnings of Go web server with mongoDb
Viewed 0 times
beginningswithmongodbwebserver
Problem
I am just learning how to use go and I'm a bit confused on how my code is supposed to be organised package wise.
Am I right to have the
My directory structure is
main.go
app.go
user/user.go
user/user_provider.go
```
package user
import (
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2"
)
type provider struct {
Collection *mgo.Collection
}
func Provider(mongoSession *mgo.Session) provider {
p := provider{}
p.Collection = mongoSession.DB("test").C("user")
return p
}
func(p *provider) InsertUser(user User) error {
return p.Collection.Insert(&user)
}
func (p *provider) GetUser(username string) (error, User) {
result := User{}
err := p.Collection.
Am I right to have the
user package? I'm planning to redirect my /user/ routes to a handler in this package but I'm unsure if this is the go way to do thingsMy directory structure is
/go_rest_api
/user
-user.go
-user_provider.go
-app.go
-main.gomain.go
package main
func main() {
a := App{}
a.Initialize()
a.Run()
}app.go
package main
import (
"fmt"
"log"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2"
"go_rest_api/user"
)
type App struct {
Router *mux.Router
Mongo *MongoConnection
}
type MongoConnection struct {
Session *mgo.Session
}
func(a* App) GetMongoSession() *mgo.Session {
return a.Mongo.Session.Copy()
}
func(a *App) Initialize() {
session, err := mgo.Dial("127.0.0.1:27017")
if err != nil {
panic(err)
}
session.SetMode(mgo.Monotonic, true)
a.Mongo = &MongoConnection{session}
a.Router = mux.NewRouter()
}
func(a *App) Run() {
defer a.Mongo.Session.Close()
testMongo(a.GetMongoSession())
}
func testMongo(session *mgo.Session) {
userProvider := user.Provider(session)
err := userProvider.InsertUser(user.User{"test"})
if err != nil {
log.Fatal(err)
}
user := user.User{}
err, user = userProvider.GetUser("test")
if err != nil {
log.Fatal(err)
}
fmt.Println("username:", user.Username)
}user/user.go
package user
type User struct {
Username string
}user/user_provider.go
```
package user
import (
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2"
)
type provider struct {
Collection *mgo.Collection
}
func Provider(mongoSession *mgo.Session) provider {
p := provider{}
p.Collection = mongoSession.DB("test").C("user")
return p
}
func(p *provider) InsertUser(user User) error {
return p.Collection.Insert(&user)
}
func (p *provider) GetUser(username string) (error, User) {
result := User{}
err := p.Collection.
Solution
You might wanted to start writing some comments on your code.
For example I've read all your code but there are some method that I don't understand like :
Okay we know It is getting the mongo session. but for what purpose?
I think it is a good think to keep comment the code, so others can understand it quickly. especially when someone who new to mongoDB. :D
For example I've read all your code but there are some method that I don't understand like :
func(a* App) GetMongoSession() *mgo.Session {
return a.Mongo.Session.Copy()
}Okay we know It is getting the mongo session. but for what purpose?
// We get the mongo session for .... you can start explain why
func(a* App) GetMongoSession() *mgo.Session {
return a.Mongo.Session.Copy()
}I think it is a good think to keep comment the code, so others can understand it quickly. especially when someone who new to mongoDB. :D
Code Snippets
func(a* App) GetMongoSession() *mgo.Session {
return a.Mongo.Session.Copy()
}// We get the mongo session for .... you can start explain why
func(a* App) GetMongoSession() *mgo.Session {
return a.Mongo.Session.Copy()
}Context
StackExchange Code Review Q#155587, answer score: 2
Revisions (0)
No revisions yet.