Created
May 26, 2020 15:25
-
-
Save RicardoLinck/55eb76af1511c807880b7863d685a838 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 ( | |
"log" | |
"net/http" | |
"github.com/gorilla/mux" | |
) | |
func main() { | |
router := mux.NewRouter() | |
router.Use(loggingMiddleware) | |
router.HandleFunc("/ishealthy", handleIsHealthy).Methods(http.MethodGet) | |
sub := router.PathPrefix("/sub").Subrouter() | |
sub.Use(subMiddleware) | |
sub.HandleFunc("/a", handleSubHealthyA).Methods(http.MethodGet) | |
sub.HandleFunc("/b", handleSubHealthyB).Methods(http.MethodGet) | |
http.ListenAndServe(":8080", router) | |
} | |
func handleIsHealthy(w http.ResponseWriter, r *http.Request) { | |
w.WriteHeader(http.StatusOK) | |
log.Println("Returning 200 - Healthy") | |
w.Write([]byte("Healthy")) | |
} | |
func handleSubHealthyA(w http.ResponseWriter, r *http.Request) { | |
w.WriteHeader(http.StatusOK) | |
log.Println("Returning 200 - Healthy a") | |
w.Write([]byte("Healthy a")) | |
} | |
func handleSubHealthyB(w http.ResponseWriter, r *http.Request) { | |
w.WriteHeader(http.StatusOK) | |
log.Println("Returning 200 - Healthy b") | |
w.Write([]byte("Healthy b")) | |
} | |
func subMiddleware(next http.Handler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
log.Println("Another middleware") | |
next.ServeHTTP(w, r) | |
}) | |
} | |
func loggingMiddleware(next http.Handler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
log.Printf("Url requested: %s", r.RequestURI) | |
next.ServeHTTP(w, r) | |
log.Println("Request finished") | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment