31 lines
756 B
Go
31 lines
756 B
Go
package protected_api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.coopgo.io/coopgo-apps/parcoursmob/core/application"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Handler struct {
|
|
cfg *viper.Viper
|
|
applicationHandler *application.ApplicationHandler
|
|
}
|
|
|
|
func NewHandler(cfg *viper.Viper, applicationHandler *application.ApplicationHandler) *Handler {
|
|
return &Handler{
|
|
cfg: cfg,
|
|
applicationHandler: applicationHandler,
|
|
}
|
|
}
|
|
|
|
func (h *Handler) ApiKeyMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
apiKey := r.Header.Get("X-API-Key")
|
|
if apiKey != h.cfg.GetString("services.api.api_key") {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
} |