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

API REST & UPLOADS ESCRITO EM GO

API REST & UPLOADS ESCRITO EM GO

Esta apresentação teve como objetivo mostrar de forma simples como funciona toda arquitetura REST e seu ecossistema.

Foi apresentando um exemplo de como implementar uma api para receber uploads de arquivos.

Foi apresentando API Layers, Gateways, Proxy e SSL, Proxy Reverse, Frameworks...

02b3f64a1d67d061c3be08729322a90b?s=128

Jefferson Otoni Lima

September 28, 2016
Tweet

More Decks by Jefferson Otoni Lima

Other Decks in Technology

Transcript

  1. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 1/41 Do zero ao

    deploy Receiving les in an api REST Je erson Otoni Lima CEO & Co-Fundador | S3wf & S3commerce entrepreneur 2007 Full-Stack programmer
  2. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 2/41 Alguns projetos atuais

    que participo! Web service REST para Uploads e Downloads Web service REST para ser consumido Microserviços Integrações com Mercado Livre / Moip / Correio / JadLog / B2W Integrações com Amazon / S3 / EFS / API Gatewy / SES /SQS
  3. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 3/41

  4. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 4/41 Recebendo arquivos em

    uma api REST ! Cronstruindo um restful para ser consumido ...
  5. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 5/41 Criação do servidor

    REST Qual Gateway ou Proxy Reverse usar ? Qual arquitetura implementar ? Como vamos Escalar ? Qual linguagem/Framework usar ? Vamos fazer decomposição do sistema ?
  6. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 6/41

  7. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 7/41 API Layers, Gateways,

    Proxy e SSL Api Gateway (https://console.aws.amazon.com/apigateway) (Amazon) Azure (https://docs.microsoft.com/en-us/azure/api-management/) (Microsoft) Dreamfactory (https://www.dreamfactory.com/) (Dreamfactory) Firebase (https:// rebase.google.com/docs/database/rest/start?hl=pt-br) (Google) kong (https://getkong.org/) (Gerenciador de APIs e Microserviços open - source) trae k (https://trae k.io/) (HTTP reverse proxy and load balancer) letsencrypt (https://letsencrypt.org/getting-started) (SSL letsencrypt free)
  8. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 8/41

  9. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 9/41 Nossa arquitetura com

    Gateway
  10. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 10/41 Nossa arquitetura com

    Proxy Reverse
  11. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 11/41 Escalando nosso web

    service AWS Elastic Beanstalk AWS Elastic Load Balancing
  12. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 12/41 Antes vamos contextualizar

    alguns conceitos importantes !
  13. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 13/41 O que é

    REST ? REST (https://pt.wikipedia.org/wiki/REST) (Representational State Transfer) É uma abstração da arquitetura da World Wide Web [pattern] 2000 por Roy Fielding (https://en.wikipedia.org/wiki/Roy_Fielding) RPC (https://pt.wikipedia.org/wiki/Chamada_de_procedimento_remoto) não é um exemplo de REST {Apresentação Socket / RPC (http://go-talks.appspot.com/github.com/je otoni/gotalks/server-rest.slide) } RESTful (São as aplicações ou sistemas que usam princípio REST)
  14. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 14/41

  15. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 15/41 Concepções REST Resources

    => /insert, /update, /delete, /search [URI] Representation => Html, Json, Xml, ... / Content-Type State Transfer => GET, POST, PUT, DELETE ...
  16. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 16/41

  17. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 17/41 Como funciona o

    protocolo HTTP ? Hypertext Transfer Protocol (https://pt.wikipedia.org/wiki/Hypertext_Transfer_Protocol) Mensagem HTTP códigos http (https://pt.wikipedia.org/wiki/Lista_de_c%C3%B3digos_de_estado_HTTP) Cabeçalho da mensagem RFC 2616 (https://tools.ietf.org/html/rfc2616) Corpo da mensagem Requisição (Request-Line,Request-header,Method, URI e HTTP-Version) Métodos (GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS e CONNECT)
  18. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 18/41

  19. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 19/41 Clientes REST cURL

    (criar requisições em diversos protocolos) (https://ec.haxx.se) Postman (https://www.getpostman.com/) HTTPie (https://httpie.org/) Hurl.it (https://www.hurl.it/)
  20. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 20/41

  21. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 21/41 Frameworks REST awesome-go

    (https://github.com/avelino/awesome-go/blob/master/README.md) (links diversos) Slumer (https://github.com/sogko/slumber) (API REST) Mux (https://github.com/gorilla/mux) (simples roteador url) HttpRouter (https://github.com/julienschmidt/httprouter) (poderoso roteador url) Frisby (https://github.com/verdverm/frisby) (REST API TEST) Pressly chi (https://github.com/pressly/chi) (Http Service REST) Go JSON handler (https://github.com/rs/formjson) gores (https://github.com/alioygur/gores) (handles HTML, JSON, XML)
  22. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 22/41

  23. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 23/41 Opa.. Golang now,

    vamos implementar!
  24. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 24/41 Construindo nosso server

    REST / ListenAndServe ( ) // http // http.ListenAndServe(":9999", nil) // servindo um site estático // http.ListenAndServe(":9999", http.FileServer(http.Dir("/static/site")))) // ssl // http.ListenAndServeTLS(":443", server.crt, yourkey.key, nil) // Criando chaves ssl // ~/user-linux$ openssl req -nodes -newkey rsa:2048 -keyout yourkey.key -out serve.csr
  25. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 25/41 De nindo Rotas

    Handlers!
  26. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 26/41 Server httpRouter {Criando

    rotas} func main() { router := httprouter.New() router.GET("/", Index) // curl -X GET localhost:9999 router.GET("/user/:uid", getuser) // curl -X GET localhost:9999/user/23322 router.POST("/adduser/:uid", adduser) // curl -X POST localhost:9999/adduser/232 router.DELETE("/deluser/:uid", deleteuser) // curl -X DELETE localhost:9999/deluser/232 router.PUT("/moduser/:uid", modifyuser) // curl -X PUT localhost:9999/moduser/232 // abrindo porta log.Fatal(http.ListenAndServe(":9999", router)) }
  27. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 27/41 Rotas e Tipos

    de Uploads func main() { router := mux.NewRouter() router. // multipart/form-data =namefile=@file.tar HandleFunc("/upload-form", uploadFormFile).Methods("POST") router. // multipart/form-data =namefile[]=@file.tar HandleFunc("/upload-form-multi", uploadFormMuilt).Methods("POST") router. // --data-binary "@file1.jpg" HandleFunc("/upload-bin", uploadBinary) // listenAndServer(router) }
  28. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 28/41 Subindo varias portas

    com ListenAndServe() func listenAndServer(router *mux.Router) { // conf := &http.Server{Handler: router, Addr: ":8081"} go conf.ListenAndServe() // go func() { http.ListenAndServe(":8082", router) }() // go func() { http.ListenAndServe(":8083", nil) }() // func() { http.ListenAndServe(":8084", nil) }() }
  29. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 29/41 Copy para servidor

    único arquivio func uploadFormFile(w http.ResponseWriter, r *http.Request) { //sizeUpload := r.ContentLength / 1048576 // 1Mb convert to Mb file, handler, _ := r.FormFile("fileupload") r.ParseMultipartForm(10485760) defer file.Close() pathUserAcess := PathLocal + "/" + acessekey + "/" + handler.Filename existPath, _ := os.Stat(PathLocal + "/" + acessekey) if existPath == nil { os.MkdirAll(PathLocal+"/"+acessekey, 0777) } f, _ := os.OpenFile(pathUserAcess, os.O_WRONLY|os.O_CREATE, 0777) defer f.Close() fmt.Println("Copy: ", pathUserAcess) sizef, _ := io.Copy(f, file) fmt.Fprintln(w, "", 500, `{"msg":"ok upload size recebido"}`, sizef, "Bytes") }
  30. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 30/41 Copy para servidor

    multipolos arquivos func uploadFormFileMulti(w http.ResponseWriter, r *http.Request, files []*multipart.FileHeader) { if len(r.MultipartForm.File["fileupload[]"]) > 0 { fmt.Println("size array: ", len(files)) var uploadSize int64 for i, _ := range files { file, err := files[i].Open() defer file.Close() pathUserAcess := PathLocal + "/" + acessekey + "/" + files[i].Filename existPath, _ := os.Stat(PathLocal + "/" + acessekey) if existPath == nil { os.MkdirAll(PathLocal+"/"+acessekey, 0777) } f, _ := os.Create(pathUserAcess) defer f.Close() fmt.Println("Copy: ", pathUserAcess) sizef, _ := io.Copy(f, file) if err == nil { uploadSize += sizef } } } }
  31. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 31/41 Uploads Binary func

    uploadBinary(w http.ResponseWriter, r *http.Request) { pathUpKeyUser := PathLocal + "/" + acessekey nameFileUp := r.Header.Get("Name-File") pathUpKeyUserFull := pathUpKeyUser + "/" + nameFileUp existPath, _ := os.Stat(pathUpKeyUserFull) if existPath == nil { os.MkdirAll(pathUpKeyUser, 0777) } ff, _ := os.OpenFile(pathUpKeyUserFull, os.O_WRONLY|os.O_CREATE, 0777) defer ff.Close() fmt.Println("Copy: ", pathUpKeyUserFull) sizef, _ := io.Copy(ff, r.Body) fmt.Fprintln(w, "", 500, `{"msg":"ok upload size recebido"}`, sizef, "Bytes") }
  32. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 32/41

  33. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 33/41 cURL testando nosso

    REST
  34. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 34/41 Upload Form /

    multipart/form-data curl -i -X POST http://localhost:9999/upload-form \ -u "API_KEY:383883jef903xxxx838xxxx" \ --form "fileupload=@files/file2.pdf"
  35. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 35/41 Upload Form multi

    / multipart/form-data curl -i --request POST localhost:9999/upload-form-multi \ -u "API_KEY:383883jef903xxxx838xxxx" \ --form 'email=jefferson&password=3838373773' \ --form "fileupload[]=@files/file1.jpg" \ --form "fileupload[]=@files/file2.pdf"
  36. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 36/41 Upload Binary //

    http://https://xxxxxx.execute-api.us-xxx-x.amazonaws.com/upload-bin \ curl -i --request POST http://localhost:9999/upload-bin \ -u "API_KEY:383883jef903xxxx838xxxx" \ -H "Accept: binary/octet-stream" \ -H "Content-Type: binary/octet-stream" \ -H "Name-File: file2.pdf" \ --data-binary "@files/file2.pdf"
  37. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 37/41 Referências github.com/avelino/awesome-go/blob/master/README.md (https://github.com/avelino/awesome-

    go/blob/master/README.md) golang.org/pkg/net (https://golang.org/pkg/net) godoc.org/golang.org/x/net/websocket (https://godoc.org/golang.org/x/net/websocket) golang.org/pkg/net/rpc (https://golang.org/pkg/net/rpc) github.com/gorilla/mux (https://github.com/gorilla/mux) github.com/ orix/wsdl2go (https://github.com/ orix/wsdl2go) github.com/julienschmidt/httprouter (https://github.com/julienschmidt/httprouter)
  38. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 38/41 Dúvidas?

  39. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 39/41

  40. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 40/41 Thank you Je

    erson Otoni Lima CEO & Co-Fundador | S3wf & S3commerce entrepreneur 2007 Full-Stack programmer
  41. 29/08/2018 Do zero ao deploy https://go-talks.appspot.com/github.com/jeffotoni/gotalks/restful.slide#1 41/41