parcoursmob/utils/protectapi/api_key.go

20 lines
440 B
Go

package protectapi
import (
"net/http"
"github.com/rs/zerolog/log"
)
func ApiKey(apiKey string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if apiKey == "" || r.Header.Get("X_API_KEY") != apiKey {
log.Info().Msg("call to api not allowed")
w.WriteHeader(http.StatusForbidden)
}
next.ServeHTTP(w, r)
})
}
}