Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Rafael Ponte rponte

🏠
Working from home
View GitHub Profile
@rponte
rponte / ValidPixKey.kt
Last active Apr 12, 2021
Micronaut Bean Validation does NOT support Custom property paths
View ValidPixKey.kt
package br.com.zup.edu.pix.registra
import io.micronaut.core.annotation.AnnotationValue
import io.micronaut.validation.validator.constraints.ConstraintValidator
import io.micronaut.validation.validator.constraints.ConstraintValidatorContext
import javax.inject.Singleton
import javax.validation.Constraint
import javax.validation.Payload
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.CLASS
@rponte
rponte / HowToUse.java
Last active Mar 26, 2021
Decorator simples para cuidar da segurança de forma transparente
View HowToUse.java
public class HowToUse {
public static void main(String[] args) {
MercadoLivreHttpClient client = new MercadoLivreHttpClient();
MercadoLivreHttpClient securedClient = new MercadoLivreSecuredHttpClient(client);
HttpResponse<?> response = securedClient.post(null);
}
}
@rponte
rponte / CreateProposalEndpoint.kt
Last active Apr 5, 2021
Micronaut: mixing @transaction with gRPC Endpoint methods
View CreateProposalEndpoint.kt
@Singleton
open class CreateProposalEndpoint(@Inject val repository: ProposalRespository) : ProposalGrpcServiceGrpc.ProposalGrpcServiceImplBase() {
@Transactional // starts the current transaction
open override fun create(request: CreateProposalRequest, responseObserver: StreamObserver<CreateProposalResponse>) {
val proposal = repository.save(request.toModel()) // with Hibernate, the INSERT may not be flushed here
// returns to the client without waiting for the commit
responseObserver.onNext(CreateProposalResponse.newBuilder().build())
@rponte
rponte / grpc-load-balancing.md
Last active May 5, 2021
gRPC Load Balancing may not be that easy
View grpc-load-balancing.md

Why gRPC load balancing is so tricky?

At a high-level, we need to understand two points:

  • gRPC is built on HTTP/2, and HTTP/2 is designed to have a single long-lived TCP connection (a sticky and persistent connection);
  • To do gRPC load balancing, we need to shift from connection balancing to request balancing;

Some interesting articles about this subject

@rponte
rponte / BeanValidationConfig.java
Last active Apr 8, 2021
Spring Boot: integrating Bean Validation between Spring and Hibernate (supporting DI on custom validations with Hibernate)
View BeanValidationConfig.java
@Configuration
public class BeanValidationConfig {
/**
* The idea here is to configure Hibernate to use the same ValidatorFactory used by Spring,
* so that Hibernate will be able to handle custom validations that need to use dependency injection (DI)
*/
@Bean
public HibernatePropertiesCustomizer hibernatePropertiesCustomizer(final Validator validator) {
return new HibernatePropertiesCustomizer() {
@rponte
rponte / a01_ExceptionHandlerInterceptor.kt
Last active Apr 24, 2021
Micronaut: Implementing a Micronaut AOP Interceptor for exception handling in gRPC endpoints
View a01_ExceptionHandlerInterceptor.kt
package br.com.zup.edu.shared.grpc
import io.grpc.BindableService
import io.grpc.stub.StreamObserver
import io.micronaut.aop.MethodInterceptor
import io.micronaut.aop.MethodInvocationContext
import org.slf4j.LoggerFactory
import javax.inject.Inject
import javax.inject.Singleton
@rponte
rponte / a01_ExceptionHandlerInterceptor.kt
Last active Feb 27, 2021
Micronaut: Implementing a gRPC Server Interceptor for exception handling
View a01_ExceptionHandlerInterceptor.kt
package br.com.zup.edu.shared.grpc
import io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener
import io.grpc.Metadata
import io.grpc.ServerCall
import io.grpc.ServerCallHandler
import io.grpc.ServerInterceptor
import org.slf4j.LoggerFactory
import javax.inject.Inject
import javax.inject.Singleton
@rponte
rponte / ClientHostResolver.java
Last active Apr 27, 2021
SpringBoot: how to obtain the user IP address?
View ClientHostResolver.java
package br.com.zup.edu.nossocartao.propostas.shared.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.stream.Stream;
@Component