Skip to content

Configuration basics

Mariusz Kopylec edited this page Jun 19, 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.

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 ReverseProxyConfiguration {

    @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 request mapping
                .add(requestMapping("mapping 1") // Replaced request mapping
                        .set(requestServerNameRewriter().outgoingServers("example-prod-1.com")))
                .update("mapping 2", configurer -> configurer // Updated request 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 >>