lot of new functionalities
This commit is contained in:
31
servers/web/protected_api/handler.go
Normal file
31
servers/web/protected_api/handler.go
Normal file
@@ -0,0 +1,31 @@
|
||||
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)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user