building restful services with go and mongodb

19
Building RESTful Services with Go and MongoDB Shiju Varghese GopherCon India 2015 20 February, 2015

Upload: shiju-varghese

Post on 15-Jul-2015

866 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Building RESTful Services With Go and MongoDB

Building RESTful Services with Go and

MongoDB

Shiju Varghese

GopherCon India 201520 February, 2015

Page 2: Building RESTful Services With Go and MongoDB

● Cloud Solutions Architect● Gopher

@shijucvhttps://github.com/shijuvar

About Me

Page 3: Building RESTful Services With Go and MongoDB

Outline

● Building HTTP Servers in Go.● Working with MongoDB using mgo.● REST API Demo with Go and MongoDB.

Page 4: Building RESTful Services With Go and MongoDB

Building HTTP Servers

Page 5: Building RESTful Services With Go and MongoDB

net/http Package

● Provides HTTP client and server implementations.

● Allows you to build HTTP servers in Go.● Provides composability and extensibility.

Page 6: Building RESTful Services With Go and MongoDB

Processing HTTP Requests

● ServeMux - HTTP Request multiplexer (router).

● Handler - Serve HTTP Requests.

Page 7: Building RESTful Services With Go and MongoDB

http.Handler Interface

● Serve HTTP Requests. ● Handlers are responsible for writing

response headers and bodies.

type Handler interface {

ServeHTTP(ResponseWriter, *Request)

}

Page 8: Building RESTful Services With Go and MongoDB

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)

}

Page 9: Building RESTful Services With Go and MongoDB

A Simple HTTP Serverpackage 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)

}

Page 10: Building RESTful Services With Go and MongoDB

Persistence with MongoDB

Page 11: Building RESTful Services With Go and MongoDB

mgo (mango)

● MongoDB driver for the Go.● Written in Go.● Created in 2010.● Sponsored by MongoDB from 2011.

Page 12: Building RESTful Services With Go and MongoDB

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

}

Page 13: Building RESTful Services With Go and MongoDB

Working with Collections

collection := session.DB( "notesdb").C("notes")

var notes []Noteiter := 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

Page 14: Building RESTful Services With Go and MongoDB

Demo - REST API with Go and MongoDB

Page 15: Building RESTful Services With Go and MongoDB

Modeltype Note struct {

Id bson.ObjectId `bson:"_id" json:"id"`Title string `json:"title"`Description string `json:"description"`CreatedOn time.Time `json:"createdon"`

}

Page 16: Building RESTful Services With Go and MongoDB

Routing with gorilla/mux Packager := 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")

Page 17: Building RESTful Services With Go and MongoDB

Handler Functionsfunc 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) {

}

Page 18: Building RESTful Services With Go and MongoDB

Source Codehttps://github.com/shijuvar/gophercon-rest-demo

Page 19: Building RESTful Services With Go and MongoDB

Thank You

Github - https://github.com/shijuvar Twitter - https://twitter.com/shijucv

Shiju Varghese