From 1b5e68eea9461b63be1fdf1571556dd42ef60583 Mon Sep 17 00:00:00 2001 From: hzbb Date: Sun, 13 Oct 2024 18:35:17 +0800 Subject: [PATCH] first commit --- Dockerfile | 23 ++++++ Procfile | 1 + README.md | 39 ++++++++++ docker-compose.yml | 12 +++ pom.xml | 64 ++++++++++++++++ .../com/gazgeek/helloworld/Application.java | 17 +++++ .../helloworld/controller/HomeController.java | 14 ++++ src/main/resources/application.properties | 1 + .../com/gazgeek/helloworld/HealthTest.java | 75 +++++++++++++++++++ .../helloworld/HelloWorldResponse.java | 26 +++++++ .../helloworld/HomeControllerTest.java | 50 +++++++++++++ 11 files changed, 322 insertions(+) create mode 100644 Dockerfile create mode 100644 Procfile create mode 100644 README.md create mode 100644 docker-compose.yml create mode 100644 pom.xml create mode 100644 src/main/java/com/gazgeek/helloworld/Application.java create mode 100644 src/main/java/com/gazgeek/helloworld/controller/HomeController.java create mode 100644 src/main/resources/application.properties create mode 100644 src/test/java/com/gazgeek/helloworld/HealthTest.java create mode 100644 src/test/java/com/gazgeek/helloworld/HelloWorldResponse.java create mode 100644 src/test/java/com/gazgeek/helloworld/HomeControllerTest.java diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..370e490 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +FROM harbor.hzbb.top/tools/maven:v3.8.8 + +MAINTAINER hzbb2221 + +ENV LANG=C.UTF-8 JVM_OPTS_ENV=$JVM_OPTS AGENT_NAME_ENV=$AGENT_NAME SW_ADDR_ENV=$SW_ADDR + +RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\ + echo 'Asia/Shanghai' >/etc/timezone + +COPY . /data/springboot-helloworld +WORKDIR /data/springboot-helloworld + +RUN mvn clean install + +EXPOSE 8080 + +CMD java -jar \ +$JVM_OPTS_ENV \ +-XX:+UnlockExperimentalVMOptions -XX:MaxRAMFraction=1 \ +#-javaagent:/tmp/agent/skywalking-agent.jar \ +#-Dskywalking.agent.service_name=$AGENT_NAME_ENV \ +#-Dskywalking.collector.backend_service=$SW_ADDR_ENV \ +target/helloworld-0.0.1-SNAPSHOT.jar \ No newline at end of file diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..c480fec --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: java $JAVA_OPTS -jar target/*.jar \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..da9af90 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# Spring Boot Hello World + +A spring boot enabled hello world application + +[![Build Status](https://travis-ci.org/gazgeek/springboot-helloworld.svg?branch=master)](https://travis-ci.org/gazgeek/springboot-helloworld) + +[![Coverage Status](https://coveralls.io/repos/gazgeek/springboot-helloworld/badge.svg)](https://coveralls.io/r/gazgeek/springboot-helloworld) + +- Travis CI build and test +- Continuous deployment to Heroku on success + +## Usage + +- Directly using maven +``` +mvn spring-boot:run +``` + +- From within your IDE right click run +``` +Application.java +``` + +- From executable jar file +``` +mvn clean install +java -jar target/helloworld-0.0.1-SNAPSHOT.jar +``` + +- To run this as a docker application (assumption docker is installed on your machine) +``` +docker pull gazgeek/springboot-helloworld +docker container run -p 8080:8080 -d gazgeek/springboot-helloworld + +Go to Browser and type http://localhost:8080/ or do curl http://localhost:8080/ on command prompt +``` + + + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..34b66ac --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +version: '3' +services: + springboot-helloworld: + build: + context: ./ + dockerfile: Dockerfile + container_name: springboot-helloworld + restart: always + environment: + - JVM_OPTS_ENV=-Xms1024m -Xmx1024m -Xmn512m -XX:+UseConcMarkSweepGC + ports: + - 8080:8080 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..1ffcbb0 --- /dev/null +++ b/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + com.gazgeek + helloworld + 0.0.1-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.2.2.RELEASE + + + + 1.8 + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.jacoco + jacoco-maven-plugin + 0.7.2.201409121644 + + + prepare-agent + + prepare-agent + + + + + + org.eluder.coveralls + coveralls-maven-plugin + 3.0.1 + + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.boot + spring-boot-starter-test + test + + + + \ No newline at end of file diff --git a/src/main/java/com/gazgeek/helloworld/Application.java b/src/main/java/com/gazgeek/helloworld/Application.java new file mode 100644 index 0000000..12a0a5e --- /dev/null +++ b/src/main/java/com/gazgeek/helloworld/Application.java @@ -0,0 +1,17 @@ +package com.gazgeek.helloworld; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; + +@EnableAutoConfiguration +@ComponentScan +public class Application { + + + public static void main(String[] args) throws Exception { + SpringApplication.run(Application.class, args); + } + + +} \ No newline at end of file diff --git a/src/main/java/com/gazgeek/helloworld/controller/HomeController.java b/src/main/java/com/gazgeek/helloworld/controller/HomeController.java new file mode 100644 index 0000000..c2df952 --- /dev/null +++ b/src/main/java/com/gazgeek/helloworld/controller/HomeController.java @@ -0,0 +1,14 @@ +package com.gazgeek.helloworld.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HomeController { + + @RequestMapping("/") + String home() { + return "Hello from GazGeek!"; + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..41a52d4 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +server.port: ${port:8080} \ No newline at end of file diff --git a/src/test/java/com/gazgeek/helloworld/HealthTest.java b/src/test/java/com/gazgeek/helloworld/HealthTest.java new file mode 100644 index 0000000..75f73b2 --- /dev/null +++ b/src/test/java/com/gazgeek/helloworld/HealthTest.java @@ -0,0 +1,75 @@ +package com.gazgeek.helloworld; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; +import org.springframework.boot.test.WebIntegrationTest; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; + +import java.net.URI; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.springframework.http.HttpStatus.OK; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +@WebIntegrationTest(randomPort = true) +public class HealthTest { + + @Value("${local.server.port}") + private Integer port; + + private RestTemplate restTemplate = new TestRestTemplate(); + + + @Test + public void checkHealth() { + getRequest("/health") + .assertStatusCode(OK) + .assertResponseBody("{\"status\":\"UP\"}"); + } + + + private HealthResponse getRequest(String uri) { + return new HealthResponse(restTemplate.getForEntity(getUri(uri), String.class)); + } + + + protected URI getUri(String uri) { + return UriComponentsBuilder + .newInstance() + .scheme("http") + .host("localhost") + .port(port) + .path(uri) + .build() + .toUri(); + } + + + private static class HealthResponse { + + private ResponseEntity responseEntity; + + public HealthResponse(ResponseEntity responseEntity) { + this.responseEntity = responseEntity; + } + + public HealthResponse assertStatusCode(HttpStatus expected) { + assertThat(responseEntity.getStatusCode(), is(expected)); + return this; + } + + public HealthResponse assertResponseBody(String expected) { + assertThat(responseEntity.getBody(), is(expected)); + return this; + } + } +} diff --git a/src/test/java/com/gazgeek/helloworld/HelloWorldResponse.java b/src/test/java/com/gazgeek/helloworld/HelloWorldResponse.java new file mode 100644 index 0000000..2d5e714 --- /dev/null +++ b/src/test/java/com/gazgeek/helloworld/HelloWorldResponse.java @@ -0,0 +1,26 @@ +package com.gazgeek.helloworld; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.*; + +public class HelloWorldResponse { + + private ResponseEntity responseEntity; + + public HelloWorldResponse(ResponseEntity entity) { + this.responseEntity = entity; + } + + public HelloWorldResponse assertStatusCode(HttpStatus expected) { + assertThat(responseEntity.getStatusCode(), is(expected)); + return this; + } + + public HelloWorldResponse assertResponseBody(String expected) { + assertThat(responseEntity.getBody(), is(expected)); + return this; + } +} diff --git a/src/test/java/com/gazgeek/helloworld/HomeControllerTest.java b/src/test/java/com/gazgeek/helloworld/HomeControllerTest.java new file mode 100644 index 0000000..2271622 --- /dev/null +++ b/src/test/java/com/gazgeek/helloworld/HomeControllerTest.java @@ -0,0 +1,50 @@ +package com.gazgeek.helloworld; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; +import org.springframework.boot.test.WebIntegrationTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; + +import java.net.URI; + +import static org.springframework.http.HttpStatus.OK; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +@WebIntegrationTest(randomPort = true) +public class HomeControllerTest { + + @Value("${local.server.port}") + private Integer port; + + private RestTemplate restTemplate = new TestRestTemplate(); + + @Test + public void helloWorld() { + getRequest("/") + .assertStatusCode(OK) + .assertResponseBody("Hello from GazGeek!"); + } + + private HelloWorldResponse getRequest(String uri) { + return new HelloWorldResponse(restTemplate.getForEntity(getUri(uri), String.class)); + } + + + protected URI getUri(String uri) { + return UriComponentsBuilder + .newInstance() + .scheme("http") + .host("localhost") + .port(port) + .path(uri) + .build() + .toUri(); + } + +}