32 lines
723 B
Go
32 lines
723 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"yobble-gateway-go/internal/logger"
|
|
"yobble-gateway-go/pkg/geoip"
|
|
)
|
|
|
|
type CountryMW struct {
|
|
Geo *geoip.GeoIPService
|
|
}
|
|
|
|
func (m *CountryMW) AddCountryHeaderIPMiddleware(next http.Handler) http.Handler {
|
|
op := "middleware.AddCountryHeaderIPMiddleware"
|
|
log := logger.NewLoggerWithOp(op)
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
realIP := GetRealIP(r)
|
|
log.Debug(op, "realIP", realIP)
|
|
_, countryCode := m.Geo.IsCountryBlocked(realIP)
|
|
log.Debug(op, "countryCode", countryCode)
|
|
|
|
if countryCode != "" {
|
|
r.Header.Set("X-Country-Code", countryCode)
|
|
} else {
|
|
r.Header.Set("X-Country-Code", "Undefined")
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|