func GeocodeUnaryServerInterceptor(apiKey string) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
client := geo.NewGoogleGeo(apiKey)
geocode := geoctx.GetGeoInfo(ctx)
res, _ := client.Geocode(geocode)
if err != nil {
geoctx.SetGeoLocate(ctx, 0, 0)
} else {
geoctx.SetGeoLocate(ctx, res.Lng, res.Lat)
}
return handler(ctx, err)
}
}
実際に作ったミドルウェア