func Handle() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := context.WithValue(r.Context(), "user", "admin") r = r.WithContext(ctx) w.WriteHeader(http.StatusOK) w.Write([]byte("Hello, world!")) }) } func Auth(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("Authorization") != "secret_token" { w.WriteHeader(http.StatusUnauthorized) w.Write([]byte("Unauthorized")) return } next.ServeHTTP(w, r) }) }
func Logger(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Printf("%s %s\n", r.Method, r.URL.Path) next.ServeHTTP(w, r) }) }By using the Context package library in conjunction with the Grafana package, developers can create custom middleware to enhance the functionality of their server.