diff --git a/run/helloworld-source/README.md b/run/helloworld-source/README.md new file mode 100644 index 00000000000..635ed14952b --- /dev/null +++ b/run/helloworld-source/README.md @@ -0,0 +1,16 @@ +# Cloud Run Hello World Sample + +This sample shows how to deploy a Hello World [Spring Boot](https://spring.io/projects/spring-boot) +application to Cloud Run using source deploy. + +For more details on how to work with this sample read the [Google Cloud Run Java Samples README](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/run). + +[![Run in Google Cloud][run_img]][run_link] + +[run_img]: https://storage.googleapis.com/cloudrun/button.svg +[run_link]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/helloworld-source + +## Dependencies + +* **Spring Boot**: Web server framework. +* **Junit**: [development] Test running framework. diff --git a/run/helloworld-source/pom.xml b/run/helloworld-source/pom.xml new file mode 100644 index 00000000000..069c0690c5a --- /dev/null +++ b/run/helloworld-source/pom.xml @@ -0,0 +1,116 @@ + + + + 4.0.0 + com.example.run + helloworld + 0.0.1-SNAPSHOT + jar + + + + + com.google.cloud.samples + shared-configuration + 1.2.0 + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + UTF-8 + UTF-8 + 21 + 21 + 3.2.2 + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + test + + + junit + junit + test + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot.version} + + + + repackage + + + + + + + com.google.cloud.tools + jib-maven-plugin + 3.4.0 + + + gcr.io/PROJECT_ID/helloworld + + + + + + org.jacoco + jacoco-maven-plugin + 0.8.11 + + + + prepare-agent + + + + report + prepare-package + + report + + + + + + + diff --git a/run/helloworld-source/src/main/java/com/example/helloworld/HelloworldApplication.java b/run/helloworld-source/src/main/java/com/example/helloworld/HelloworldApplication.java new file mode 100644 index 00000000000..c3165c9aca3 --- /dev/null +++ b/run/helloworld-source/src/main/java/com/example/helloworld/HelloworldApplication.java @@ -0,0 +1,45 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START cloudrun_helloworld_service] + +package com.example.helloworld; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@SpringBootApplication +public class HelloworldApplication { + + @Value("${NAME:World}") + String name; + + @RestController + class HelloworldController { + @GetMapping("/") + String hello() { + return "Hello " + name + "!"; + } + } + + public static void main(String[] args) { + SpringApplication.run(HelloworldApplication.class, args); + } +} +// [END cloudrun_helloworld_service] diff --git a/run/helloworld-source/src/main/resources/application.properties b/run/helloworld-source/src/main/resources/application.properties new file mode 100644 index 00000000000..3cebd9ca826 --- /dev/null +++ b/run/helloworld-source/src/main/resources/application.properties @@ -0,0 +1,16 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# [START cloudrun_helloworld_properties] +server.port=${PORT:8080} +# [END cloudrun_helloworld_properties] diff --git a/run/helloworld-source/src/test/java/com/example/helloworld/HelloworldApplicationTests.java b/run/helloworld-source/src/test/java/com/example/helloworld/HelloworldApplicationTests.java new file mode 100644 index 00000000000..c35e651b6bc --- /dev/null +++ b/run/helloworld-source/src/test/java/com/example/helloworld/HelloworldApplicationTests.java @@ -0,0 +1,45 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.helloworld; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +public class HelloworldApplicationTests { + + @Autowired private MockMvc mockMvc; + + @Test + public void returnsHelloWorld() throws Exception { + mockMvc + .perform(get("/")) + .andExpect(status().isOk()) + .andExpect(content().string("Hello World!")); + } +} diff --git a/run/helloworld/pom.xml b/run/helloworld/pom.xml index 70e213d033f..59c54561f70 100644 --- a/run/helloworld/pom.xml +++ b/run/helloworld/pom.xml @@ -41,8 +41,8 @@ limitations under the License. UTF-8 UTF-8 - 17 - 17 + 21 + 21 3.2.2 @@ -92,6 +92,25 @@ limitations under the License. + + org.jacoco + jacoco-maven-plugin + 0.8.11 + + + + prepare-agent + + + + report + prepare-package + + report + + + +