Skip to content

Commit

Permalink
Expose ServiceRegistry, LoadBalancer- and RewriteMap-configuration fo…
Browse files Browse the repository at this point in the history
…r info
  • Loading branch information
BartRobeyns committed Sep 23, 2018
1 parent 969d951 commit e7f8e21
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package eu.europa.ec.cc.zkapachebridge.rest;

import eu.europa.ec.cc.zkapachebridge.ZkApacheBridgeApplication;
import eu.europa.ec.cc.zkapachebridge.test.CommonTestConfig;
import org.hamcrest.core.StringContains;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Map;

import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@SpringBootTest(
classes = {CommonTestConfig.class, ZkApacheBridgeApplication.class},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:/application-test.properties")
public class InfoControllerTest {


@LocalServerPort
int port;

@Autowired
TestRestTemplate restTemplate;

@Test
public void getServiceInformation() {
assertTrue(
"service info didn't return zk-apache-bridge itself as endpoint",
this.restTemplate.getForObject("/api/info", Map.class).containsKey("zk-apache-bridge"));
}

@Test
public void getLoadBalancerConfiguration() {
assertThat(
this.restTemplate.getForObject("/api/info/loadbalancer", String.class),
new StringContains("ProxyPass /zk-apache-bridge"));
}

@Test
public void getRewriteMapConfiguration() {
assertThat(
this.restTemplate.getForObject("/api/info/rewritemap", String.class),
new StringContains("zk-apache-bridge"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public void onApplicationEvent(ServiceRegistryUpdatedEvent event) {
}
}

public String getConfiguration() throws Exception {
return buildLoadBalancerContents();
}

String buildLoadBalancerContents() throws Exception {
Map<String, Object> model = buildServicesFreemarkerModel();
Template template = freemarkerConfiguration.getTemplate(loadBalancerConfigTemplate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public void onApplicationEvent(ServiceRegistryUpdatedEvent event) {
rewriteMapWriter.write(contents);
}

public String getConfiguration() {
return buildRewriteMapContents();
}

private String buildRewriteMapContents() {
Map<String, EndpointCollection> services = serviceRegistry.getServices();
StringBuffer sb = new StringBuffer();
Expand All @@ -49,4 +53,5 @@ private String buildRewriteMapContents() {
.append("\n"));
return sb.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package eu.europa.ec.cc.zkapachebridge.rest;

import eu.europa.ec.cc.zkapachebridge.apache.loadbalancer.LoadBalancerUpdater;
import eu.europa.ec.cc.zkapachebridge.apache.rewritemap.RewriteMapUpdater;
import eu.europa.ec.cc.zkapachebridge.serviceregistry.EndpointCollection;
import eu.europa.ec.cc.zkapachebridge.serviceregistry.ServiceRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/info")
public class InfoController {

@Autowired
ServiceRegistry serviceRegistry;

@Autowired
LoadBalancerUpdater loadBalancerUpdater;

@Autowired
RewriteMapUpdater rewriteMapUpdater;

@RequestMapping(method = RequestMethod.GET)
public Map<String, EndpointCollection> getServiceInformation() {
return serviceRegistry.getServices();
}

@RequestMapping(path="loadbalancer", method=RequestMethod.GET)
public String getLoadBalancerConfiguration() throws Exception {
return loadBalancerUpdater.getConfiguration();
}

@RequestMapping(path="rewritemap", method=RequestMethod.GET)
public String getRewriteMapConfiguration() {
return rewriteMapUpdater.getConfiguration();
}
}
2 changes: 2 additions & 0 deletions zkclientsample/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ services:
image: ecdevops.eu/zk-apache-bridge-docker-rewritemap
ports:
- 80:80
- 9090:8080
- 5005:5005
links:
- zookeeper
Expand All @@ -50,6 +51,7 @@ services:
image: ecdevops.eu/zk-apache-bridge-docker-loadbalancer
ports:
- 8080:80
- 9091:8080
- 5006:5005
links:
- zookeeper
Expand Down

0 comments on commit e7f8e21

Please sign in to comment.