Skip to content

Configuration basics

Mariusz Kopylec edited this page Oct 4, 2019 · 9 revisions

Charon can be configured in many ways. This can be done via CharonConfigurer API. Charon can be configured globally or per request mapping. Request mapping configuration always overwrites the global one for a particular mapping. For example if Charon is configured like below:

import static com.github.mkopylec.charon.configuration.CharonConfigurer.charonConfiguration;
import static com.github.mkopylec.charon.configuration.RequestMappingConfigurer.requestMapping;
import static com.github.mkopylec.charon.forwarding.interceptors.rewrite.RequestServerNameRewriterConfigurer.requestServerNameRewriter;

@Configuration
class CharonConfiguration {

    @Bean
    CharonConfigurer charonConfigurer() {
        return charonConfiguration()
                .set(requestServerNameRewriter().outgoingServers("host1:8080"))
                .add(requestMapping("mapping 1"))
                .add(requestMapping("mapping 2")
                        .set(requestServerNameRewriter().outgoingServers("host2:8081")));
    }
}

then requests handled by mapping 1 mapping will be forwarded to http://host1:8080 but those handled by mapping 2 mapping will be forwarded to http://host2:8081.

Filter's order

Charon uses a servlet (in the WebMVC module) or a web (in the WebFlux module) filter to do its job. By default the filter is invoked before any other filter in the application. The order of the filter is configurable:

import static com.github.mkopylec.charon.configuration.CharonConfigurer.charonConfiguration;

@Configuration
class CharonConfiguration {

    @Bean
    CharonConfigurer charonConfigurer() {
        return charonConfiguration()
            .filterOrder(<int>);
    }
}

Multiple Spring Boot profiles

To create a different configuration for a different Spring Boot profile a common configuration can be extracted and then adjusted to the profile:

import static com.github.mkopylec.charon.configuration.CharonConfigurer.charonConfiguration;
import static com.github.mkopylec.charon.configuration.RequestMappingConfigurer.requestMapping;
import static com.github.mkopylec.charon.forwarding.interceptors.rewrite.RequestServerNameRewriterConfigurer.requestServerNameRewriter;

@Configuration
class CharonConfiguration {

    @Profile("default")
    @Bean("charonConfigurer")
    CharonConfigurer defaultCharonConfigurer() {
        return charonConfigurer();
    }

    @Profile("production")
    @Bean("charonConfigurer")
    CharonConfigurer productionCharonConfigurer() {
        return charonConfigurer()
                .set(requestServerNameRewriter().outgoingServers("example-prod.com")) // Replaced server name rewriter
                .add(requestMapping("mapping prod")) // New mapping
                .add(requestMapping("mapping 1") // Replaced mapping
                        .set(requestServerNameRewriter().outgoingServers("example-prod-1.com")))
                .update("mapping 2", configurer -> configurer // Updated mapping
                        .set(requestServerNameRewriter().outgoingServers("example-prod-2.com")));
    }

    private CharonConfigurer charonConfigurer() {
        return charonConfiguration()
                .set(requestServerNameRewriter().outgoingServers("example.com"))
                .add(requestMapping("mapping 1"))
                .add(requestMapping("mapping 2")
                        .set(requestServerNameRewriter().outgoingServers("example-2.com")));
    }
}

<< previous | next >>