8000 Update App Engine samples to listen to "PORT" env var (#1511) · niyata/java-docs-samples@b9f7749 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit b9f7749

Browse files
i0n-kurtisvg
authored andcommitted
Update App Engine samples to listen to "PORT" env var (GoogleCloudPlatform#1511)
1 parent 1cf264b commit b9f7749

File tree

4 files changed

+16
-8
lines changed
  • appengine-java11
    • appengine-simple-jetty-main/src/main/java/com/example/appengine/demo/jettymain
    • custom-entrypoint
    • http-server/src/main/java/com/example/appengine
    • sparkjava-helloworld/src/main/java/com/example/appengine/sparkdemo

4 files changed

+16
-8
lines changed

appengine-java11/appengine-simple-jetty-main/src/main/java/com/example/appengine/demo/jettymain/Main.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ public static void main(String[] args) throws Exception {
3232
System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StrErrLog");
3333
System.setProperty("org.eclipse.jetty.LEVEL", "INFO");
3434

35-
// Create a basic Jetty server object that will listen on port 8080.
35+
// Create a basic Jetty server object that will listen on port defined by
36+
// the PORT environment variable when present, otherwise on 8080.
3637
// Note: If you set this to port 0, a randomly available port will be
3738
// assigned. You can find the assigned port in the logs or programmatically
3839
// obtain it.
39-
Server server = new Server(8080);
40+
int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
41+
Server server = new Server(port);
4042

4143
// The WebAppContext is the interface to provide configuration for a web
4244
// application. In this example, the context path is being set to "/" so

appengine-java11/custom-entrypoint/Main.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
public class Main {
2424

2525
public static void main(String[] args) throws IOException {
26-
// Create an instance of HttpServer bound to port 8080.
27-
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
26+
// Create an instance of HttpServer bound to port defined by the
27+
// PORT environment variable when present, otherwise on 8080.
28+
int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
29+
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
2830

2931
// Set root URI path.
3032
server.createContext("/", (var t) -> {

appengine-java11/http-server/src/main/java/com/example/appengine/Main.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
public class Main {
2525

2626
public static void main(String[] args) throws IOException {
27-
// Create an instance of HttpServer with port 8080.
28-
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
27+
// Create an instance of HttpServer bound to port defined by the
28+
// PORT environment variable when present, otherwise on 8080.
29+
int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
30+
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
2931

3032
// Set root URI path.
3133
server.createContext("/", (var t) -> {

appengine-java11/sparkjava-helloworld/src/main/java/com/example/appengine/sparkdemo/Main.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
public class Main {
2222

2323
public static void main(String[] args) {
24-
// Starts the webapp on localhost:8080.
25-
Spark.port(8080);
24+
// Starts the webapp on localhost and the port defined by the PORT
25+
// environment variable when present, otherwise on 8080.
26+
int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
27+
Spark.port(port);
2628
Spark.get("/", (req, res) -> "Hello World!");
2729
}
2830
}

0 commit comments

Comments
 (0)
0