Skip to content

Instantly share code, notes, and snippets.

@RicardoLinck
Last active July 9, 2020 21:40
Show Gist options
  • Save RicardoLinck/2b10efd2c50ddff7f6d19b788174708c to your computer and use it in GitHub Desktop.
Save RicardoLinck/2b10efd2c50ddff7f6d19b788174708c to your computer and use it in GitHub Desktop.
package main
import (
"context"
"log"
"net/http"
"github.com/google/uuid"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter()
router.Use(guidMiddleware)
router.HandleFunc("/ishealthy", handleIsHealthy).Methods(http.MethodGet)
http.ListenAndServe(":8080", router)
}
func handleIsHealthy(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
uuid := r.Context().Value("uuid")
log.Printf("[%v] Returning 200 - Healthy", uuid)
w.Write([]byte("Healthy"))
}
func guidMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
uuid := uuid.New()
r = r.WithContext(context.WithValue(r.Context(), "uuid", uuid))
next.ServeHTTP(w, r)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment