package es.moki.ratelimij.dropwizard.component.app;

import es.moki.ratelimij.dropwizard.RateLimitBundle;
import es.moki.ratelimij.dropwizard.component.app.api.LoginResource;
import es.moki.ratelimij.dropwizard.component.app.api.TrekResource;
import es.moki.ratelimij.dropwizard.component.app.api.UserResource;
import es.moki.ratelimij.dropwizard.component.app.auth.TestOAuthAuthenticator;
import es.moki.ratelimitj.core.limiter.request.RequestRateLimiterFactory;
import es.moki.ratelimitj.redis.request.RedisRateLimiterFactory;
import io.dropwizard.Application;
import io.dropwizard.Configuration;
import io.dropwizard.auth.AuthDynamicFeature;
import io.dropwizard.auth.AuthValueFactoryProvider;
import io.dropwizard.auth.PrincipalImpl;
import io.dropwizard.auth.oauth.OAuthCredentialAuthFilter;
import io.dropwizard.lifecycle.Managed;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;

public class RateLimitApplication extends Application<Configuration> {

    private RedisClient redisClient;

    @Override
    public void initialize(Bootstrap<Configuration> bootstrap) {
        redisClient = RedisClient.create("redis://localhost:7006");
        RequestRateLimiterFactory factory = new RedisRateLimiterFactory(redisClient);

        //RequestRateLimiterFactory factory = new InMemoryRateLimiterFactory();

        bootstrap.addBundle(new RateLimitBundle(factory));
    }

    @Override
    public void run(Configuration configuration, Environment environment) {

        environment.jersey().register(new LoginResource());
        environment.jersey().register(new UserResource());
        environment.jersey().register(new TrekResource());

        environment.jersey().register(new AuthDynamicFeature(
                new OAuthCredentialAuthFilter.Builder<PrincipalImpl>()
                        .setAuthenticator(new TestOAuthAuthenticator()).setPrefix("Bearer")
                        .buildAuthFilter()));
        environment.jersey().register(RolesAllowedDynamicFeature.class);
        environment.jersey().register(new AuthValueFactoryProvider.Binder<>(PrincipalImpl.class));

        //TODO move this cleanup into the tests
        environment.lifecycle().manage(new Managed() {
            @Override
            public void start() {
            }

            @Override
            public void stop() {
                flushRedis();
            }

            private void flushRedis() {
                try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
                    connection.sync().flushdb();
                }
                redisClient.shutdownAsync();
            }
        });

    }
}

@@ -794,48 +794,8 @@
 er;%0A
-import io.dropwizard.lifecycle.Managed;%0A
 impo
@@ -908,60 +908,8 @@
 nt;%0A
-import io.lettuce.core.api.StatefulRedisConnection;%0A
 impo
@@ -2183,561 +2183,8 @@
 ));%0A
-%0A        //TODO move this cleanup into the tests%0A        environment.lifecycle().manage(new Managed() %7B%0A            @Override%0A            public void start() %7B%0A            %7D%0A%0A            @Override%0A            public void stop() %7B%0A                flushRedis();%0A            %7D%0A%0A            private void flushRedis() %7B%0A                try (StatefulRedisConnection%3CString, String%3E connection = redisClient.connect()) %7B%0A                    connection.sync().flushdb();%0A                %7D%0A                redisClient.shutdownAsync();%0A            %7D%0A        %7D);%0A%0A
     

Clean up dropwizard test