Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Build REST API with Golang

Build REST API with Golang

This talk in GDG DevFest Cordoba 2017

Esteban Dorado Roldan

December 02, 2017
Tweet

More Decks by Esteban Dorado Roldan

Other Decks in Programming

Transcript

  1. 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
  2. REST Protocol Describes six (6) constraints: 1. Uniform Interface 2.

    Cacheable 3. Client-Server 4. Stateless 5. Code on Demand 6. Layered System
  3. 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
  4. 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.
  5. 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
  6. 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)
  7. 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
  8. 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'}")) }
  9. 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))) }
  10. 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)) }
  11. 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
  12. 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
  13. 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
  14. 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
  15. Q&A