Building RESTful Services with Go and MongoDB Shiju Varghese GopherCon India 2015 20 February, 2015
● Cloud Solutions Architect ● Gopher @shijucv https://github.com/shijuvar About Me
Outline ● Building HTTP Servers in Go. ● Working with MongoDB using mgo. ● REST API Demo with Go and MongoDB.
Building HTTP Servers
net/http Package ● Provides HTTP client and server implementations. ● Allows you to build HTTP servers in Go. ● Provides composability and extensibility.
Processing HTTP Requests ● ServeMux - HTTP Request multiplexer (router). ● Handler - Serve HTTP Requests.
http.Handler Interface ● Serve HTTP Requests. ● Handlers are responsible for writing response headers and bodies. type Handler interface { ServeHTTP(ResponseWriter , *Request) }
HandlerFunc An adapter to allow the use of ordinary functions as HTTP handlers. type HandlerFunc func(ResponseWriter, *Request) func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) { f(w, r) }
A Simple HTTP Server package main import ( "fmt" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello, world!") } func main() { http.HandleFunc("/", handler) http.ListenAndServe (":8080", nil) }
Persistence with MongoDB
mgo (mango) ● MongoDB driver for the Go. ● Written in Go. ● Created in 2010. ● Sponsored by MongoDB from 2011.
Connecting to MongoDB Dial establishes a new session to the cluster of servers defined by the url parameter. session, err := mgo.Dial( "localhost") if err != nil { return err }
Working with Collections collection := session.DB( "notesdb").C("notes") var notes []Note iter := collection.Find(nil).Iter() result := Note{} for iter.Next(&result) { notes = append(notes, result) } c := session.DB(database).C(collection) Querying against collections Accessing collections
Demo - REST API with Go and MongoDB
Model type Note struct { Id bson.ObjectId `bson:"_id" json:"id"` Title string `json:"title"` Description string `json:"description"` CreatedOn time.Time `json:"createdon"` }
Routing with gorilla/mux Package r := mux.NewRouter() r.HandleFunc("/api/notes", NotesHandler).Methods( "GET") r.HandleFunc("/api/notes", CreateNoteHandler).Methods( "POST") r.HandleFunc("/api/notes/{id}" , UpdateNoteHandler).Methods( "PUT") r.HandleFunc("/api/notes/{id}" , DeleteNoteHandler).Methods( "DELETE")
Handler Functions func CreateNoteHandler (w http.ResponseWriter, r *http.Request) { } func NotesHandler(w http.ResponseWriter, r *http.Request) { } func UpdateNoteHandler (w http.ResponseWriter, r *http.Request) { } func DeleteNoteHandler (w http.ResponseWriter, r *http.Request) { }
Source Code https://github.com/shijuvar/gophercon-rest-demo
Thank You Github - https://github.com/shijuvar Twitter - https://twitter.com/shijucv Shiju Varghese

Building RESTful Services With Go and MongoDB

  • 1.
    Building RESTful Services withGo and MongoDB Shiju Varghese GopherCon India 2015 20 February, 2015
  • 2.
    ● Cloud SolutionsArchitect ● Gopher @shijucv https://github.com/shijuvar About Me
  • 3.
    Outline ● Building HTTPServers in Go. ● Working with MongoDB using mgo. ● REST API Demo with Go and MongoDB.
  • 4.
  • 5.
    net/http Package ● ProvidesHTTP client and server implementations. ● Allows you to build HTTP servers in Go. ● Provides composability and extensibility.
  • 6.
    Processing HTTP Requests ●ServeMux - HTTP Request multiplexer (router). ● Handler - Serve HTTP Requests.
  • 7.
    http.Handler Interface ● ServeHTTP Requests. ● Handlers are responsible for writing response headers and bodies. type Handler interface { ServeHTTP(ResponseWriter , *Request) }
  • 8.
    HandlerFunc An adapter toallow the use of ordinary functions as HTTP handlers. type HandlerFunc func(ResponseWriter, *Request) func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) { f(w, r) }
  • 9.
    A Simple HTTPServer package main import ( "fmt" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello, world!") } func main() { http.HandleFunc("/", handler) http.ListenAndServe (":8080", nil) }
  • 10.
  • 11.
    mgo (mango) ● MongoDBdriver for the Go. ● Written in Go. ● Created in 2010. ● Sponsored by MongoDB from 2011.
  • 12.
    Connecting to MongoDB Dialestablishes a new session to the cluster of servers defined by the url parameter. session, err := mgo.Dial( "localhost") if err != nil { return err }
  • 13.
    Working with Collections collection:= session.DB( "notesdb").C("notes") var notes []Note iter := collection.Find(nil).Iter() result := Note{} for iter.Next(&result) { notes = append(notes, result) } c := session.DB(database).C(collection) Querying against collections Accessing collections
  • 14.
    Demo - RESTAPI with Go and MongoDB
  • 15.
    Model type Note struct{ Id bson.ObjectId `bson:"_id" json:"id"` Title string `json:"title"` Description string `json:"description"` CreatedOn time.Time `json:"createdon"` }
  • 16.
    Routing with gorilla/muxPackage r := mux.NewRouter() r.HandleFunc("/api/notes", NotesHandler).Methods( "GET") r.HandleFunc("/api/notes", CreateNoteHandler).Methods( "POST") r.HandleFunc("/api/notes/{id}" , UpdateNoteHandler).Methods( "PUT") r.HandleFunc("/api/notes/{id}" , DeleteNoteHandler).Methods( "DELETE")
  • 17.
    Handler Functions func CreateNoteHandler(w http.ResponseWriter, r *http.Request) { } func NotesHandler(w http.ResponseWriter, r *http.Request) { } func UpdateNoteHandler (w http.ResponseWriter, r *http.Request) { } func DeleteNoteHandler (w http.ResponseWriter, r *http.Request) { }
  • 18.
  • 19.
    Thank You Github -https://github.com/shijuvar Twitter - https://twitter.com/shijucv Shiju Varghese