Slide 1

Slide 1 text

Build REST API with Golang Esteban Dorado GDG DevFest Cordoba

Slide 2

Slide 2 text

Esteban Dorado @Mr_Esti

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Pet servers Cattle servers

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

REST API

Slide 7

Slide 7 text

What is REST? ● What does it stand for? Representational State Transfer ● What is it? A style of software architecture for distributed systems ● Who/Where/When? Came about in 2000 doctoral dissertation of Roy Fielding

Slide 8

Slide 8 text

REST Protocol Describes six (6) constraints: 1. Uniform Interface 2. Cacheable 3. Client-Server 4. Stateless 5. Code on Demand 6. Layered System

Slide 9

Slide 9 text

What is RESTful API? A RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data. A RESTful API -- also referred to as a RESTful web service -- is based on REST technology

Slide 10

Slide 10 text

PROs of RESTful API? REST technology is generally preferred to the more robust Simple Object Access Protocol (SOAP) technology because REST leverages less bandwidth, making it more suitable for internet usage.

Slide 11

Slide 11 text

REST - Core Where: URL based Resources: Data, files, methods... How: HTTP What: Up to you

Slide 12

Slide 12 text

Example URIs ● GET http://www.example.com/v1/hr/employees/123818237 ● POST http://www.example.com/v1/hr/employees ● DELETE http://www.example.com/v1/hr/employees/145657723 ● PUT http://www.example.com/v1/hr/employees/345875384

Slide 13

Slide 13 text

HTTP methods HTTP Verb CRUD HTTP Codes POST Create 201 (Created), 404 (Not Found), 409 (Conflict) GET Read 200 (OK), 404 (Not Found) PUT Update/Replace 405 (Method Not Allowed), 200 (OK), 204 (No Content), 404 (Not Found) PATCH Update/Modify 405 (Method Not Allowed), 200 (OK), 204 (No Content), 404 (Not Found) DELETE Delete 405 (Method Not Allowed), 200 (OK), 404 (Not Found)

Slide 14

Slide 14 text

Resources in REST? ● Resources can be served in different representations: ○ XML, JSON, HTML, etc. ● Content negotiation methods ● Headers: ○ Accept or Content-Type ● Query parameters ○ GET /v1/users/10543?format=json ● Uri extension ○ GET /v1/users/10543.xml

Slide 15

Slide 15 text

Alternatives of REST API http://graphql.org https://grpc.io

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

REST API ¿ ?

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

EXPLORE

Slide 20

Slide 20 text

SIMPLE API https://goreportcard.com/report/github.com/mresti/guidetoprodu ction_api https://github.com/mresti/rest-api-golang/tree/simple_api

Slide 21

Slide 21 text

api.go var ( counter int port = 9000 ) func count(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { w.WriteHeader(http.StatusMethodNotAllowed) return } counter++ w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusAccepted) w.Write([]byte("{'Message': 'Hello DevFest Granada 2017'}")) }

Slide 22

Slide 22 text

api.go func stats(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { w.WriteHeader(http.StatusMethodNotAllowed) return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) w.Write([]byte(fmt.Sprintf("{'Visits': %d}", counter))) }

Slide 23

Slide 23 text

api.go func main() { http.HandleFunc("/", count) http.HandleFunc("/stats", stats) log.Println("Listening at port ", port) log.Panic( http.ListenAndServe(fmt.Sprintf(":%d", port), nil)) }

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

api.go func main() { http.HandleFunc("/favicon.ico", func(_ http.ResponseWriter, _ *http.Request) {}) http.HandleFunc("/", count) http.HandleFunc("/stats", stats) log.Println("Listening at port ", port) log.Panic( http.ListenAndServe(fmt.Sprintf(":%d", port), nil)) } Add line

Slide 28

Slide 28 text

Let’s build your REST API as a PRO

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

PRO API https://goreportcard.com/report/github.com/mresti/guidetoprodu ction_api https://github.com/mresti/rest-api-golang/tree/professional _api

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Pipeline: API in golang Build Integration Code Goreports

Slide 33

Slide 33 text

API: .travis.yml sudo: false language: go go: - 1.8.x - 1.9.x - pip matrix: allow_failures: - go: pip fast_finish: true Script: - make # Run format code, run tests and build code - make integrationtest # Run integration tests

Slide 34

Slide 34 text

Capture screen shot on ChromeOS device, and place here. Be sure to hide the bottom task bar that includes your image, open apps, etc. Ideal image size should be 1626x1080. When using those sizes, image position is {x: 147px, y: 63px} Add a CURVED SHADOW to the screen shot https://goreportcard.com/report/github.com/mresti/restful_a pi_golang

Slide 35

Slide 35 text

API: dockerfile FROM golang:1.9.2 ENV PATH /api:$PATH ARG API_PORT ENV PORT $API_PORT WORKDIR /api # Copy binary demo ADD ./api-linux-amd64 /api/demo # Modified files for titan RUN chmod 555 /api/demo # Expose ports EXPOSE $PORT # Run Titan CMD demo -port $PORT

Slide 36

Slide 36 text

API: docker-compose.yml version: "2.1" services: api: build: context: . args: API_PORT: "8081" ports: - "8081:8081"

Slide 37

Slide 37 text

¿Will we build our gRPC API?

Slide 38

Slide 38 text

Learn API gRPC https://goreportcard.com/report/github.com/mresti/guidetoprodu ction_api https://codelabs.developers.google.com/codelabs/cloud-grpc/ index.html#0

Slide 39

Slide 39 text

Research & Development

Slide 40

Slide 40 text

Beers with friends...

Slide 41

Slide 41 text

Q&A

Slide 42

Slide 42 text

@Mr_Esti