diff --git a/appengine-java21/helloworld/http-server/README.md b/appengine-java21/helloworld/http-server/README.md new file mode 100644 index 00000000000..8c85a89b255 --- /dev/null +++ b/appengine-java21/helloworld/http-server/README.md @@ -0,0 +1,20 @@ +# Standalone HTTP Server on Google App Engine Standard with Java 21 + +This sample shows how to deploy an application to Google App Engine from source. The `entrypoint` field listed in the [`app.yaml`](src/main/appengine/app.yaml) is not required, +as GAE will determine the entrypoint by searching the `target` directory for the .jar file with a Main-Class Manifest entry. + +## Setup + +See [Prerequisites](../README.md#Prerequisites). + +## Deploy to App Engine Standard + +``` +gcloud app deploy +``` + +To view your app, use command: +``` +gcloud app browse +``` +Or navigate to `https://.appspot.com`. diff --git a/appengine-java21/helloworld/http-server/pom.xml b/appengine-java21/helloworld/http-server/pom.xml new file mode 100644 index 00000000000..88ff04908fb --- /dev/null +++ b/appengine-java21/helloworld/http-server/pom.xml @@ -0,0 +1,49 @@ + + 4.0.0 + com.example.appengine + http-server + 0.1 + + + + com.google.cloud.samples + shared-configuration + 1.2.0 + + + + + 11 + 11 + + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.3.0 + + + + com.example.appengine.Main + + + + + + + com.google.cloud.tools + appengine-maven-plugin + 2.8.0 + + GCLOUD_CONFIG + http-server + + + + + diff --git a/appengine-java21/helloworld/http-server/src/main/appengine/app.yaml b/appengine-java21/helloworld/http-server/src/main/appengine/app.yaml new file mode 100644 index 00000000000..b84dd5b9b43 --- /dev/null +++ b/appengine-java21/helloworld/http-server/src/main/appengine/app.yaml @@ -0,0 +1,18 @@ +# Copyright 2025 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 gae_java21_runtime] +runtime: java21 +# No need for an entrypoint with single fatjar with correct manifest class-path entry. +# [END gae_java21_runtime] diff --git a/appengine-java21/helloworld/http-server/src/main/java/com/example/appengine/Main.java b/appengine-java21/helloworld/http-server/src/main/java/com/example/appengine/Main.java new file mode 100644 index 00000000000..0b075cd3266 --- /dev/null +++ b/appengine-java21/helloworld/http-server/src/main/java/com/example/appengine/Main.java @@ -0,0 +1,52 @@ +/* + * Copyright 2025 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.appengine; + +import com.sun.net.httpserver.HttpServer; +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; + +public class Main { + + public static void main(String[] args) throws IOException { + // Create an instance of HttpServer bound to port defined by the + // PORT environment variable when present, otherwise on 8080. + int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080")); + HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); + + // Set root URI path. + server.createContext("/", (var t) -> { + byte[] response = "Hello World!".getBytes(); + t.sendResponseHeaders(200, response.length); + try (OutputStream os = t.getResponseBody()) { + os.write(response); + } + }); + + // Create a second URI path. + server.createContext("/foo", (var t) -> { + byte[] response = "Foo!".getBytes(); + t.sendResponseHeaders(200, response.length); + try (OutputStream os = t.getResponseBody()) { + os.write(response); + } + }); + + server.start(); + } +}