Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #11 from microservices-demo/add_health_endpoint
Browse files Browse the repository at this point in the history
Initial checkin for healthcheck
  • Loading branch information
pidster authored Nov 1, 2016
2 parents 038527e + 5daa3fc commit fef970e
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package works.weave.socks.cart.configuration;

import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.mongodb.MongoClientOptions;

@Configuration
@AutoConfigureBefore(MongoAutoConfiguration.class)
public class MongoConfiguration {

@Bean
public MongoClientOptions optionsProvider() {
MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder();
optionsBuilder.serverSelectionTimeout(10000);
MongoClientOptions options = optionsBuilder.build();
return options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import works.weave.socks.cart.cart.CartResource;
import works.weave.socks.cart.entities.Cart;


@RestController
@RequestMapping(path = "/carts")
public class CartsController {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package works.weave.socks.cart.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import works.weave.socks.cart.entities.HealthCheck;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class HealthCheckController {

@Autowired
private MongoTemplate mongoTemplate;

@ResponseStatus(HttpStatus.OK)
@RequestMapping(method = RequestMethod.GET, path = "/health")
public
@ResponseBody
Map<String, List<HealthCheck>> getHealth() {
Map<String, List<HealthCheck>> map = new HashMap<String, List<HealthCheck>>();
List<HealthCheck> healthChecks = new ArrayList<HealthCheck>();
Date dateNow = Calendar.getInstance().getTime();

HealthCheck app = new HealthCheck("cart", "OK", dateNow);
HealthCheck database = new HealthCheck("cart-db", "OK", dateNow);

try {
mongoTemplate.executeCommand("{ buildInfo: 1 }");
} catch (Exception e) {
database.setStatus("err");
}

healthChecks.add(app);
healthChecks.add(database);

map.put("health", healthChecks);
return map;
}
}
59 changes: 59 additions & 0 deletions src/main/java/works/weave/socks/cart/entities/HealthCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package works.weave.socks.cart.entities;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.Calendar;
import java.util.Date;

@JsonIgnoreProperties(ignoreUnknown = true)
public class HealthCheck {
private String service;
private String status;

@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
private Date date = Calendar.getInstance().getTime();

public HealthCheck() {

}

public HealthCheck(String service, String status, Date date) {
this.service = service;
this.status = status;
this.date = date;
}

@Override
public String toString() {
return "HealthCheck{" +
"service='" + service + '\'' +
", status='" + status + '\'' +
", date='" + date +
'}';
}

public String getService() {
return service;
}

public void setService(String service) {
this.service = service;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
server.port=${port:8081}
spring.data.mongodb.uri=mongodb://${db:cart-db}:27017/data
endpoints.health.enabled=false
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package works.weave.socks.cart.controllers;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.junit.Assert.assertThat;

import works.weave.socks.cart.entities.HealthCheck;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class UnitHealthCheckController {

@Autowired
private HealthCheckController healthCheckController;

@Test
public void shouldGetHealth() {
Map<String, List<HealthCheck>> results = healthCheckController.getHealth();
assertThat(results.get("health").size(), is(equalTo(2)));
}
}

0 comments on commit fef970e

Please sign in to comment.