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.

Desenvolvendo APIs em Go usando Middlewares

708 views

Published on

Como usar o conceito de Middlewares em Go

Published in: Technology
  • Be the first to comment

Desenvolvendo APIs em Go usando Middlewares

  1. 1. Desenvolvendo APIs usando middlewares Elton Minetto @eminetto http://eltonminetto.net http://asemanago.com.br elton@planrockr.com
  2. 2. Http Is The Foundation Of The Web
  3. 3. Um cliente manda uma request r.Method - HTTP method (GET, POST, PUT, PATCH, DELETE etc.) r.URL.Path - Request path (/things/123) r.URL.String() - Full URL r.URL.Query() - Query parameters (q=something&p=2) r.Body - io.ReadCloser of the request body
  4. 4. O servidor retorna uma response type ResponseWriter interface { Header() Header Write([]byte) (int, error) WriteHeader(int) }
  5. 5. Middlewares
  6. 6. Between the request and response Enrico Zimuel HTTP middleware is not for your Domain work. The middleware is a path in to, and out of, the core Domain. Paul M. Jones Run code before and after handler code Mat Ryer
  7. 7. Exemplos
  8. 8. func middlewareOne(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Println("Executing before middlewareOne") next.ServeHTTP(w, r) log.Println("Executing after middlewareOne") }) }
  9. 9. func middlewareTwo(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Println("Executing before middlewareTwo") if r.URL.Path != "/" { return } next.ServeHTTP(w, r) log.Println("Executing after middlewareTwo") }) } func final(w http.ResponseWriter, r *http.Request) { log.Println("Executing finalHandler") w.Write([]byte("OK")) }
  10. 10. func main() { finalHandler := http.HandlerFunc(final) http.Handle("/", middlewareOne(middlewareTwo(finalHandler))) http.ListenAndServe(":8000", nil) }
  11. 11. https://github.com/justinas/alice
  12. 12. func main() { finalHandler := http.HandlerFunc(final) chain := alice.New(middlewareOne, middlewareTwo).Then(finalHandler) http.ListenAndServe(":8000", chain) }
  13. 13. package main import ( "github.com/justinas/alice" "net/http" "planrockr" ) func getCurrentSubscription(w http.ResponseWriter, r *http.Request) { ... } func main() { chain := alice.New(planrockr.Auth, planrockr.GetUserByToken). Then(http.HandlerFunc(getCurrentSubscription)) http.ListenAndServe(":8000", chain) }
  14. 14. package main import ( "github.com/justinas/alice" "net/http" "planrockr" ) func processEvent(next http.Handler) http.Handler { ... } func main() { chain := alice.New(processEvent, planrockr.ValidateHookData). Then(http.HandlerFunc(planrockr.Enqueue)) http.ListenAndServe(":8000", chain) }
  15. 15. Links https://gist.github.com/eminetto/ e3bab34426ac9a0b83a538a0e421bbc8
  16. 16. Contato @eminetto http://eltonminetto.net http://asemanago.com.br elton@planrockr.com

×