Skip to content

Commit

Permalink
Rename endpoint /status to /welcome to differentiate it better from /…
Browse files Browse the repository at this point in the history
…actuator/health
  • Loading branch information
miguno committed Sep 9, 2024
1 parent 8aaa46e commit d6b764f
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 35 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
A template project to create a Docker image for a Java application.
The [example application](src/main/java/com/miguno/javadockerbuild/App.java)
uses Spring Boot to expose an HTTP endpoint at
[`/status`](http://localhost:8123/status).
[`/welcome`](http://localhost:8123/welcome).

> **Golang developer?** Check out https://github.com/miguno/golang-docker-build-tutorial
Expand Down Expand Up @@ -93,7 +93,7 @@ $ docker run -p 8123:8123 miguno/java-docker-build-tutorial:latest
```
Running container from docker image ...
Starting container for image 'miguno/java-docker-build-tutorial:latest', exposing port 8123/tcp
- Run 'curl http://localhost:8123/status' to send a test request to the containerized app.
- Run 'curl http://localhost:8123/welcome' to send a test request to the containerized app.
- Enter Ctrl-C to stop the container.
. ____ _ __ _ _
Expand Down Expand Up @@ -123,8 +123,8 @@ Starting container for image 'miguno/java-docker-build-tutorial:latest', exposin
running container.

```shell
$ curl http://localhost:8123/status
{"status":"idle"}
$ curl http://localhost:8123/welcome
{"welcome":"Hello, World!"}
```

# Local usage without Docker
Expand Down
12 changes: 6 additions & 6 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ audit:
# benchmark the app's HTTP endpoint with plow (requires https://github.com/six-ddc/plow)
benchmark-plow:
@echo plow -c 100 --duration=30s http://localhost:${APP_PORT}/status
@plow -c 100 --duration=30s http://localhost:${APP_PORT}/status
@echo plow -c 100 --duration=30s http://localhost:${APP_PORT}/welcome
@plow -c 100 --duration=30s http://localhost:${APP_PORT}/welcome

# benchmark the app's HTTP endpoint with wrk (requires https://github.com/wg/wrk)
benchmark-wrk:
@echo wrk -t 10 -c 100 --latency --duration 30 http://localhost:${APP_PORT}/status
@wrk -t 10 -c 100 --latency --duration 30 http://localhost:${APP_PORT}/status
@echo wrk -t 10 -c 100 --latency --duration 30 http://localhost:${APP_PORT}/welcome
@wrk -t 10 -c 100 --latency --duration 30 http://localhost:${APP_PORT}/welcome

# alias for 'compile'
build: compile
Expand Down Expand Up @@ -135,8 +135,8 @@ site: compile

# send request to the app's HTTP endpoint (requires Docker and running app container)
send-request-to-app:
@echo curl http://localhost:${APP_PORT}/status
@curl http://localhost:${APP_PORT}/status
@echo curl http://localhost:${APP_PORT}/welcome
@curl http://localhost:${APP_PORT}/welcome

# static code analysis with spotbugs
spotbugs: compile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
//// For the Spring Boot Admin client (the "real" app being developed).
.requestMatchers(
new AntPathRequestMatcher("/"),
// Permit public access to this app's example endpoint at `/status`.
new AntPathRequestMatcher("/status"),
// Permit public access to this app's example endpoint at `/welcome`.
new AntPathRequestMatcher("/welcome"),
// Permit public access to Swagger.
new AntPathRequestMatcher("/swagger-ui.html"),
new AntPathRequestMatcher("/v3/api-docs"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public String root() {
<p>Enjoy playing around with this application!</p>
<h2>Example Endpoints</h2>
<ul>
<li><a href="/status"><code>/status</code></a> &mdash; this app's example endpoint</li>
<li><a href="/welcome"><code>/welcome</code></a> &mdash; this app's example endpoint</li>
<li><a href="/actuator/health"><code>/actuator/health</code></a> &mdash; Spring built-in feature</li>
<li><a href="/actuator/prometheus"><code>/actuator/prometheus</code></a> &mdash; Spring built-in feature</li>
<li><a href="/admin">Spring Boot Admin dashboard</a> <strong>(requires login, see below)</strong></li>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.miguno.javadockerbuild.controllers;

import com.miguno.javadockerbuild.models.Status;
import com.miguno.javadockerbuild.models.Welcome;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -9,18 +9,18 @@
/** The only API endpoint exposed by this example application. */
@SuppressFBWarnings("SPRING_ENDPOINT")
@RestController
public class StatusController {
public class WelcomeController {

private static final String template = "Hello, %s!";

/**
* Returns a greeting to the client.
* Returns a welcome message to the client.
*
* @param name The name to greet.
* @return A personalized greeting.
* @return A personalized welcome message.
*/
@GetMapping("/status")
public Status status(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Status(String.format(template, name));
@GetMapping("/welcome")
public Welcome welcome(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Welcome(String.format(template, name));
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.miguno.javadockerbuild.models;

public record Status(String status) {}
public record Welcome(String welcome) {}
4 changes: 2 additions & 2 deletions src/test/java/com/miguno/javadockerbuild/SmokeTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.miguno.javadockerbuild;

import com.miguno.javadockerbuild.controllers.StatusController;
import com.miguno.javadockerbuild.controllers.WelcomeController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SmokeTest {

@Autowired private StatusController controller;
@Autowired private WelcomeController controller;

@Test
void verifyThatApplicationContextLoads() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;

/** An example integration test for the API endpoint `/status`. */
/** An example integration test for the API endpoint `/welcome`. */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class StatusControllerIT {
public class WelcomeControllerIT {

@Autowired private TestRestTemplate template;

@Test
public void getStatus() throws Exception {
ResponseEntity<String> response = template.getForEntity("/status", String.class);
public void welcome() throws Exception {
ResponseEntity<String> response = template.getForEntity("/welcome", String.class);
String expectedJson = """
{"status":"Hello, World!"}
{"welcome":"Hello, World!"}
""";
JSONAssert.assertEquals(expectedJson, response.getBody(), JSONCompareMode.STRICT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/** Example unit tests for the API endpoint `/status`. */
/** Example unit tests for the API endpoint `/welcome`. */
@SpringBootTest
@AutoConfigureMockMvc
public class StatusControllerTest {
public class WelcomeControllerTest {

@Autowired private MockMvc mvc;

@Test
public void getStatus() throws Exception {
public void getWelcome() throws Exception {
String expectedJson = """
{"status":"Hello, World!"}
{"welcome":"Hello, World!"}
""";

mvc.perform(MockMvcRequestBuilders.get("/status").accept(MediaType.APPLICATION_JSON))
mvc.perform(MockMvcRequestBuilders.get("/welcome").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().json(expectedJson));
}
Expand Down
4 changes: 2 additions & 2 deletions start_container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ declare -r OS="$(uname -s)"
declare -r ARCH="$(uname -m)"

# Check requirements
if ! command -v docker &> /dev/null; then
if ! command -v docker &>/dev/null; then
echo "ERROR: 'docker' command not available. Is Docker installed?"
exit 1
fi
Expand All @@ -28,6 +28,6 @@ if [[ "$OS" = "Darwin" && "$ARCH" = "arm64" ]]; then
fi

echo "Starting container for image '$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG', exposing port ${APP_PORT}/tcp"
echo "- Run 'curl http://localhost:${APP_PORT}/status' to send a test request to the containerized app."
echo "- Run 'curl http://localhost:${APP_PORT}/welcome' to send a test request to the containerized app."
echo "- Enter Ctrl-C to stop the container."
docker run $DOCKER_OPTIONS -p "$APP_PORT:$APP_PORT" "$DOCKER_IMAGE_NAME":"$DOCKER_IMAGE_TAG"

0 comments on commit d6b764f

Please sign in to comment.