Skip to content

Instantly share code, notes, and snippets.

@cocotton
Last active April 19, 2017 20:13
Show Gist options
  • Save cocotton/433cd43d495b46e34ca96a01d33a2d51 to your computer and use it in GitHub Desktop.
Save cocotton/433cd43d495b46e34ca96a01d33a2d51 to your computer and use it in GitHub Desktop.
package main
import (
"os"
"gopkg.in/mgo.v2"
"github.com/gorilla/mux"
"github.com/myUser/myProject/myProject/somePackage"
)
// This could be into an Example package
type example struct {
db *mgo.session
router *mux.Router
someVar string
}
func main () {
e := example{}
e.init()
e.initRouter()
e.initDB("someDB")
}
// This could be in an Example package
func (e *example) init() {
e.someVar = os.Getenv("SOME_ENV_VAR")
}
// This could be in an Example package
func (e* example) initDB(dbName string) {
e.db, _ = mgo.Dial(dbName)
e.db.SetMode(mgo.Monotonic, true)
}
// This could be in an Example package
func (e *example) initRouter() {
e.router = mux.NewRouter()
e.router.HandleFunc("/someRoute", func(w http.ResponseWriter, r *http.Request) {
somePackage.SomeRouteHandler(w, r, e.db, e.somevar)
}).Methods("POST")
}
package somePackage
func SomeRouteHandler(w http.ResponseWriter, r *http.Request, s *mgo.session, var string) {
// Do something here
s.DoSomeStuff
a := var + " hello"
b := anotherFunc(s, var)
// Keep doing things
}
func anotherFunc(s *mgo.session, var string) string {
// Do some other stuff here
return aString
}
package main
import (
"os"
"gopkg.in/mgo.v2"
"github.com/gorilla/mux"
"github.com/myUser/myProject/myProject/somePackage"
)
// This could be into an Example package
type Example struct {
DB *mgo.session
Router *mux.Router
SomeVar string
}
func main () {
e := Example{}
e.init()
e.initRouter()
e.initDB("someDB")
}
// This could be in an Example package
func (e *example) init() {
e.SomeVar = os.Getenv("SOME_ENV_VAR")
}
// This could be in an Example package
func (e* example) initDB(dbName string) {
e.DB, _ = mgo.Dial(dbName)
e.DB.SetMode(mgo.Monotonic, true)
}
// This could be in an Example package
func (e *example) initRouter() {
e.Router = mux.NewRouter()
e.Router.HandleFunc("/someRoute", e.SomeRouteHandler).Methods("POST")
}
package somePackage
func (e *example) SomeRouteHandler(w http.ResponseWriter, r *http.Request) {
// Do something here
e.DB.DoSomeStuff
a := e.SomeVar + " hello"
b := anotherFunc(e.DB, e.SomeVar)
// Keep doing things
}
func anotherFunc(s *mgo.session, var string) string {
// Do some other stuff here
return aString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment