├── .gitignore ├── .godir ├── Dockerfile ├── Dockerrun.aws.json ├── Procfile ├── environment.sh ├── readme.md ├── server.go └── templates ├── display.tmpl ├── layout.tmpl └── list.tmpl /.gitignore: -------------------------------------------------------------------------------- 1 | gin-bin 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /.godir: -------------------------------------------------------------------------------- 1 | github.com/topcoderinc/cribs -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM google/golang 2 | 3 | WORKDIR /gopath/src/github.com/topcoderinc/cribs 4 | ADD . /gopath/src/github.com/topcoderinc/cribs/ 5 | 6 | # go get all of the dependencies 7 | RUN go get github.com/codegangsta/martini 8 | RUN go get github.com/codegangsta/martini-contrib/render 9 | RUN go get github.com/codegangsta/martini-contrib/binding 10 | RUN go get labix.org/v2/mgo 11 | RUN go get labix.org/v2/mgo/bson 12 | 13 | RUN go get github.com/topcoderinc/cribs 14 | 15 | # set env variables to mongo 16 | ENV MONGO_DB YOUR-MONGO-DB 17 | ENV MONGO_URL YOUR-MONGO-URL 18 | 19 | EXPOSE 8080 20 | CMD [] 21 | ENTRYPOINT ["/gopath/bin/cribs"] -------------------------------------------------------------------------------- /Dockerrun.aws.json: -------------------------------------------------------------------------------- 1 | { 2 | "AWSEBDockerrunVersion": "1", 3 | "Ports": [ 4 | { 5 | "ContainerPort": "8080" 6 | } 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: cribs -port=$PORT -------------------------------------------------------------------------------- /environment.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export MONGO_URL='mongodb://localhost' 4 | export MONGO_DB='local' -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Topcoder Cribs 2 | 3 | A simple Go web app written with Martini and MongoDB that maintains links to images and videos for topcoder member cribs! -------------------------------------------------------------------------------- /server.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | "net/http" 6 | "github.com/codegangsta/martini" 7 | "github.com/codegangsta/martini-contrib/render" 8 | "github.com/codegangsta/martini-contrib/binding" 9 | "labix.org/v2/mgo" 10 | "labix.org/v2/mgo/bson" 11 | ) 12 | 13 | // the Crib struct that we can serialize and deserialize into Mongodb 14 | type Crib struct { 15 | Handle string `form:"handle"` 16 | URL string `form:"url"` 17 | Type string `form:"type"` 18 | Description string `form:"description"` 19 | } 20 | 21 | /* 22 | the function returns a martini.Handler which is called on each request. We simply clone 23 | the session for each request and close it when the request is complete. The call to c.Map 24 | maps an instance of *mgo.Database to the request context. Then *mgo.Database 25 | is injected into each handler function. 26 | */ 27 | func DB() martini.Handler { 28 | session, err := mgo.Dial(os.Getenv("MONGO_URL")) // mongodb://localhost 29 | if err != nil { 30 | panic(err) 31 | } 32 | 33 | return func(c martini.Context) { 34 | s := session.Clone() 35 | c.Map(s.DB(os.Getenv("MONGO_DB"))) // local 36 | defer s.Close() 37 | c.Next() 38 | } 39 | } 40 | 41 | // function to return an array of all Cribs from mondodb 42 | func All(db *mgo.Database) []Crib { 43 | var cribs []Crib 44 | db.C("cribs").Find(nil).All(&cribs) 45 | return cribs 46 | } 47 | 48 | // function to return a specific Crib by handle 49 | func Fetch(db *mgo.Database, handle string) Crib { 50 | var crib Crib 51 | db.C("cribs").Find(bson.M{"handle": handle}).One(&crib) 52 | return crib 53 | } 54 | 55 | func main() { 56 | 57 | m := martini.Classic() 58 | // specify the layout to use when rendering HTML 59 | m.Use(render.Renderer(render.Options { 60 | Layout: "layout", 61 | })) 62 | // use the Mongo middleware 63 | m.Use(DB()) 64 | 65 | // list of all cribs 66 | m.Get("/", func(r render.Render, db *mgo.Database) { 67 | r.HTML(200, "list", All(db)) 68 | }) 69 | 70 | /* 71 | create a new crib the form submission. Contains some martini magic. The call 72 | to binding.Form(Crib{}) parses out form data when the request comes in. 73 | It binds the data to the struct, maps it to the request context and 74 | injects into our next handler function to insert into Mongodb. 75 | */ 76 | m.Post("/", binding.Form(Crib{}), func(crib Crib, r render.Render, db *mgo.Database) { 77 | db.C("cribs").Insert(crib) 78 | r.HTML(200, "list", All(db)) 79 | }) 80 | 81 | // display the crib for a specific user 82 | m.Get("/:handle", func(params martini.Params, r render.Render, db *mgo.Database) { 83 | r.HTML(200, "display", Fetch(db, params["handle"])) 84 | }) 85 | 86 | http.ListenAndServe(":8080", m) 87 | 88 | } -------------------------------------------------------------------------------- /templates/display.tmpl: -------------------------------------------------------------------------------- 1 |

{{.Handle}}'s Crib

2 | 3 | {{ if eq .Type "Youtube" }} 4 | 5 | {{ else if eq .Type "Vimeo" }} 6 | 7 | {{ else }} 8 | 9 | {{end}} 10 | 11 |

{{ .Description }}

12 | -------------------------------------------------------------------------------- /templates/layout.tmpl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Topcoder Cribs 7 | 8 | 9 | 10 | 11 |

Topcoder Cribs!

12 | {{ yield }} 13 | 14 | -------------------------------------------------------------------------------- /templates/list.tmpl: -------------------------------------------------------------------------------- 1 |

Members' Cribs

2 | 7 | 8 |
9 |
10 | Add Your Crib! 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
--------------------------------------------------------------------------------