From c2c6061988939102a03c8c733f57b64e19fa144a Mon Sep 17 00:00:00 2001 From: Christopher Moyer Date: Tue, 2 Aug 2022 17:32:03 -0400 Subject: [PATCH] added endpoint for countdown --- Dockerfile | 10 ---- pom.xml | 41 +++++++------- .../com/example/demo/DemoApplication.java | 19 ------- .../countdown/CountdownController.java | 53 +++++++++++++++++++ .../countdown/MainApplication.java | 13 +++++ src/main/resources/application.properties | 0 .../example/demo/DemoApplicationTests.java | 28 ---------- 7 files changed, 88 insertions(+), 76 deletions(-) delete mode 100644 Dockerfile delete mode 100644 src/main/java/com/example/demo/DemoApplication.java create mode 100644 src/main/java/com/synergyhelix/countdown/CountdownController.java create mode 100644 src/main/java/com/synergyhelix/countdown/MainApplication.java delete mode 100644 src/main/resources/application.properties delete mode 100644 src/test/java/com/example/demo/DemoApplicationTests.java diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 6535c0b..0000000 --- a/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -FROM maven:3-jdk-8-alpine - -WORKDIR /usr/src/app - -COPY . /usr/src/app -RUN mvn package - -ENV PORT 5000 -EXPOSE $PORT -CMD [ "sh", "-c", "mvn -Dserver.port=${PORT} spring-boot:run" ] diff --git a/pom.xml b/pom.xml index 2d1e8c8..f4d403e 100644 --- a/pom.xml +++ b/pom.xml @@ -1,33 +1,25 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - - com.example - demo - 0.0.1-SNAPSHOT - jar - - demo - Demo project for Spring Boot - org.springframework.boot spring-boot-starter-parent - 2.0.1.RELEASE + 2.7.2 - + com.synergyhelix + countdown + 1.0.0-SNAPSHOT + countdown + Countdown Timer API - UTF-8 - UTF-8 - 1.8 + 11 - org.springframework.boot - spring-boot-starter-web + spring-boot-starter @@ -35,6 +27,18 @@ spring-boot-starter-test test + + + org.springframework.boot + spring-boot-starter-web + + + + org.apache.commons + commons-lang3 + 3.12.0 + + @@ -46,5 +50,4 @@ - - + \ No newline at end of file diff --git a/src/main/java/com/example/demo/DemoApplication.java b/src/main/java/com/example/demo/DemoApplication.java deleted file mode 100644 index a3127b4..0000000 --- a/src/main/java/com/example/demo/DemoApplication.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.example.demo; - -import org.springframework.boot.*; -import org.springframework.boot.autoconfigure.*; -import org.springframework.web.bind.annotation.*; - -@SpringBootApplication -@RestController -public class DemoApplication { - - @GetMapping("/") - String home() { - return "Spring is here!"; - } - - public static void main(String[] args) { - SpringApplication.run(DemoApplication.class, args); - } -} \ No newline at end of file diff --git a/src/main/java/com/synergyhelix/countdown/CountdownController.java b/src/main/java/com/synergyhelix/countdown/CountdownController.java new file mode 100644 index 0000000..5974dd5 --- /dev/null +++ b/src/main/java/com/synergyhelix/countdown/CountdownController.java @@ -0,0 +1,53 @@ +package com.synergyhelix.countdown; + +import org.apache.commons.lang3.time.DurationFormatUtils; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.*; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.net.URL; +import java.time.Duration; +import java.time.LocalDateTime; + +@RestController +public class CountdownController { + + @GetMapping(value = "/", produces = MediaType.IMAGE_JPEG_VALUE) + public byte[] countdown() throws IOException { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ImageIO.write(createImage(), "jpg", outputStream); + + return outputStream.toByteArray(); + } + + private BufferedImage createImage() throws IOException { + final BufferedImage image = ImageIO.read(new URL( + "https://media-cdn.tripadvisor.com/media/photo-s/04/18/a2/85/chicago.jpg")); + + Graphics g = image.getGraphics(); + + Color color = new Color(0, 0, 0, 127); + g.setColor(color); + g.fillRect(10, 5, 530, 75); + + g.setColor(Color.white); + g.setFont(new Font("Courier", Font.BOLD, 57)); + g.drawString(calculateTimeUntilThing(), 15, 60); + + g.dispose(); + + return image; + } + + private String calculateTimeUntilThing() { + LocalDateTime trip = LocalDateTime.parse("2022-08-13T09:00:00"); + + Duration duration = Duration.between(LocalDateTime.now(), trip); + + return DurationFormatUtils.formatDuration(duration.toMillis(), "dd'D' HH'H' mm'M' ss'S'", true); + } +} diff --git a/src/main/java/com/synergyhelix/countdown/MainApplication.java b/src/main/java/com/synergyhelix/countdown/MainApplication.java new file mode 100644 index 0000000..a755b39 --- /dev/null +++ b/src/main/java/com/synergyhelix/countdown/MainApplication.java @@ -0,0 +1,13 @@ +package com.synergyhelix.countdown; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class MainApplication { + + public static void main(String[] args) { + SpringApplication.run(MainApplication.class, args); + } + +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index e69de29..0000000 diff --git a/src/test/java/com/example/demo/DemoApplicationTests.java b/src/test/java/com/example/demo/DemoApplicationTests.java deleted file mode 100644 index 8595e84..0000000 --- a/src/test/java/com/example/demo/DemoApplicationTests.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.example.demo; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.junit4.SpringRunner; -import static org.assertj.core.api.Assertions.assertThat; - -@RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -public class DemoApplicationTests { - - @Test - public void contextLoads() { - } - - @Autowired - private TestRestTemplate restTemplate; - - @Test - public void homeResponse() { - String body = this.restTemplate.getForObject("/", String.class); - assertThat(body).isEqualTo("Spring is here!"); - } -} -- 2.45.2