Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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 c...
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 Han...
HandlerFunc
An adapter to allow the use of ordinary
functions as HTTP handlers.
type HandlerFunc func(ResponseWriter, *Req...
A Simple HTTP Server
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.F...
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 ...
Working with Collections
collection := session.DB( "notesdb").C("notes")
var notes []Note
iter := collection.Find(nil).Ite...
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:"des...
Routing with gorilla/mux Package
r := mux.NewRouter()
r.HandleFunc("/api/notes", NotesHandler).Methods( "GET")
r.HandleFun...
Handler Functions
func CreateNoteHandler (w http.ResponseWriter, r *http.Request) {
}
func NotesHandler(w http.ResponseWri...
Source Code
https://github.com/shijuvar/gophercon-rest-demo
Thank You
Github - https://github.com/shijuvar
Twitter - https://twitter.com/shijucv
Shiju Varghese
Upcoming SlideShare
Loading in …5
×

Building RESTful Services With Go and MongoDB

3,052 views

Published on

Slides for the talk "Building RESTful Services With Go and MongoDB" at GopherCon India 2015.

Published in: Technology
  • Thank you so much sir.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Building RESTful Services With Go and MongoDB

  1. 1. Building RESTful Services with Go and MongoDB Shiju Varghese GopherCon India 2015 20 February, 2015
  2. 2. ● Cloud Solutions Architect ● Gopher @shijucv https://github.com/shijuvar About Me
  3. 3. Outline ● Building HTTP Servers in Go. ● Working with MongoDB using mgo. ● REST API Demo with Go and MongoDB.
  4. 4. Building HTTP Servers
  5. 5. net/http Package ● Provides HTTP client and server implementations. ● Allows you to build HTTP servers in Go. ● Provides composability and extensibility.
  6. 6. Processing HTTP Requests ● ServeMux - HTTP Request multiplexer (router). ● Handler - Serve HTTP Requests.
  7. 7. http.Handler Interface ● Serve HTTP Requests. ● Handlers are responsible for writing response headers and bodies. type Handler interface { ServeHTTP(ResponseWriter , *Request) }
  8. 8. 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) }
  9. 9. 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) }
  10. 10. Persistence with MongoDB
  11. 11. mgo (mango) ● MongoDB driver for the Go. ● Written in Go. ● Created in 2010. ● Sponsored by MongoDB from 2011.
  12. 12. 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 }
  13. 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. 14. Demo - REST API with Go and MongoDB
  15. 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. 16. 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")
  17. 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. 18. Source Code https://github.com/shijuvar/gophercon-rest-demo
  19. 19. Thank You Github - https://github.com/shijuvar Twitter - https://twitter.com/shijucv Shiju Varghese

×