Files
dicom-iso/internal/middleware/auth.go
2026-06-05 08:11:44 +07:00

22 lines
604 B
Go

package middleware
import (
"net/http"
)
// APIKey returns middleware that validates the X-API-Key header
// against the expected key. Returns 401 if missing or invalid.
func APIKey(expectedKey string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-API-Key") != expectedKey {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"error":"unauthorized"}`))
return
}
next.ServeHTTP(w, r)
})
}
}