Last active
July 9, 2020 21:40
-
-
Save RicardoLinck/2b10efd2c50ddff7f6d19b788174708c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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