Skip to content

Instantly share code, notes, and snippets.

@alehano
Forked from codegangsta/martini-mgo.go
Created April 17, 2014 05:46
Show Gist options
  • Select an option

  • Save alehano/10955835 to your computer and use it in GitHub Desktop.

Select an option

Save alehano/10955835 to your computer and use it in GitHub Desktop.

Revisions

  1. alehano revised this gist Apr 17, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion martini-mgo.go
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ func DB() martini.Handler {

    // ?
    c.Next()
    c.Close()
    s.Close()
    }
    }

  2. alehano revised this gist Apr 17, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions martini-mgo.go
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,10 @@ func DB() martini.Handler {
    return func(c martini.Context) {
    s := session.Clone()
    c.Map(s.DB("advent"))

    // ?
    c.Next()
    c.Close()
    }
    }

  3. @codegangsta codegangsta renamed this gist Dec 8, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @codegangsta codegangsta created this gist Dec 8, 2013.
    48 changes: 48 additions & 0 deletions finished.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    package main

    import (
    "github.com/codegangsta/martini"
    "github.com/codegangsta/martini-contrib/binding"
    "github.com/codegangsta/martini-contrib/render"
    "labix.org/v2/mgo"
    )

    type Wish struct {
    Name string `form:"name"`
    Description string `form:"description"`
    }

    func DB() martini.Handler {
    session, err := mgo.Dial("mongodb://localhost")
    if err != nil {
    panic(err)
    }

    return func(c martini.Context) {
    s := session.Clone()
    c.Map(s.DB("advent"))
    }
    }

    func GetAll(db *mgo.Database) []Wish {
    var wishlist []Wish
    db.C("wishes").Find(nil).All(&wishlist)
    return wishlist
    }

    func main() {
    m := martini.Classic()
    m.Use(render.Renderer())
    m.Use(DB())

    m.Get("/wishes", func(r render.Render, db *mgo.Database) {
    r.HTML(200, "list", GetAll(db))
    })

    m.Post("/wishes", binding.Form(Wish{}), func(r render.Render, db *mgo.Database, wish Wish) {
    db.C("wishes").Insert(wish)
    r.HTML(200, "list", GetAll(db))
    })

    m.Run()
    }