|
| 1 | +package com.github.simplesteph.udemy; |
| 2 | + |
| 3 | +import spark.Request; |
| 4 | +import spark.Response; |
| 5 | + |
| 6 | +import java.math.BigInteger; |
| 7 | +import java.net.InetAddress; |
| 8 | +import java.net.UnknownHostException; |
| 9 | +import java.text.NumberFormat; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +import static spark.Spark.get; |
| 14 | +import static spark.Spark.threadPool; |
| 15 | + |
| 16 | +public class EC2SampleApp { |
| 17 | + |
| 18 | + private static List<String> myBigList = new ArrayList<>(); |
| 19 | + private static Boolean isHealthy = true; |
| 20 | + |
| 21 | + public static void main(String[] args) { |
| 22 | + |
| 23 | + int maxThreads = 32; |
| 24 | + int minThreads = 2; |
| 25 | + int timeOutMillis = 30000; |
| 26 | + threadPool(maxThreads, minThreads, timeOutMillis); |
| 27 | + |
| 28 | + get("/", EC2SampleApp::hello); |
| 29 | + get("/cpu", EC2SampleApp::cpu); |
| 30 | + get("/ram", EC2SampleApp::ram); |
| 31 | + get("/ram/info", EC2SampleApp::ramInfo); |
| 32 | + get("/ram/clean", EC2SampleApp::ramClean); |
| 33 | + get("/health", EC2SampleApp::health); |
| 34 | + get("/health/flip", EC2SampleApp::flipHealth); |
| 35 | + get("/details", EC2SampleApp::details); |
| 36 | + } |
| 37 | + |
| 38 | + private static String hello(Request request, Response response) throws UnknownHostException { |
| 39 | + StringBuilder sb = new StringBuilder(); |
| 40 | + |
| 41 | + String myHostname = InetAddress.getLocalHost().getHostName(); |
| 42 | + sb.append("Hello World By: ").append(myHostname).append("<br/>"); |
| 43 | + String sourceIP = request.ip(); |
| 44 | + sb.append("Receive Request From: ").append(sourceIP).append("<br/>"); |
| 45 | + return sb.toString(); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + private static String cpu(Request request, Response response) { |
| 50 | + long currentTime = System.currentTimeMillis(); |
| 51 | + BigInteger computation = fib(10000); |
| 52 | + return "fib function took " + (System.currentTimeMillis() - currentTime) + " milliseconds"; |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + private static String ram(Request request, Response response) { |
| 57 | + try { |
| 58 | + String myBigString = new String(new char[10000000]); |
| 59 | + myBigList.add(myBigString); |
| 60 | + return ramInfo(request, response); |
| 61 | + } catch (OutOfMemoryError e){ |
| 62 | + return "OutOfMemory Exception!"; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static String ramInfo(Request request, Response response) { |
| 67 | + return getRamInfo(); |
| 68 | + } |
| 69 | + |
| 70 | + private static String ramClean(Request request, Response response) { |
| 71 | + myBigList.clear(); |
| 72 | + return ramInfo(request, response); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + private static String health(Request request, Response response) { |
| 77 | + if (isHealthy){ |
| 78 | + return "Healthy!"; |
| 79 | + } else { |
| 80 | + response.status(401); |
| 81 | + return "Not healthy"; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static String flipHealth(Request request, Response response) { |
| 86 | + isHealthy = !isHealthy; |
| 87 | + return health(request, response); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + private static String details(Request request, Response response) throws UnknownHostException { |
| 92 | + StringBuilder sb = new StringBuilder(); |
| 93 | + String myHostname = InetAddress.getLocalHost().getHostName(); |
| 94 | + sb.append("Hello World By: ").append(myHostname).append("<br/>"); |
| 95 | + String sourceIP = request.ip(); |
| 96 | + sb.append("Receive Request From: ").append(sourceIP).append("<br/>"); |
| 97 | + sb.append("Request Headers are: ").append("<br/>"); |
| 98 | + request.headers().forEach(header -> sb.append(header).append(": ").append(request.headers(header)).append("<br/>")); |
| 99 | + return sb.toString(); |
| 100 | + } |
| 101 | + |
| 102 | + // Fibonacci sequence to perform some long computations |
| 103 | + private static BigInteger fib(long nth){ |
| 104 | + nth = nth - 1; |
| 105 | + long count = 0; |
| 106 | + BigInteger first = BigInteger.ZERO; |
| 107 | + BigInteger second = BigInteger.ONE; |
| 108 | + |
| 109 | + BigInteger third = null; |
| 110 | + while(count < nth){ |
| 111 | + third = new BigInteger(first.add(second).toString()); |
| 112 | + first = new BigInteger(second.toString()); |
| 113 | + second = new BigInteger(third.toString()); |
| 114 | + count++; |
| 115 | + } |
| 116 | + |
| 117 | + return third; |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + // get some RAM statistics |
| 122 | + private static String getRamInfo() { |
| 123 | + |
| 124 | + Runtime runtime = Runtime.getRuntime(); |
| 125 | + NumberFormat format = NumberFormat.getInstance(); |
| 126 | + |
| 127 | + StringBuilder sb = new StringBuilder(); |
| 128 | + long maxMemory = runtime.maxMemory(); |
| 129 | + long allocatedMemory = runtime.totalMemory(); |
| 130 | + long freeMemory = runtime.freeMemory(); |
| 131 | + |
| 132 | + sb.append("free memory: ").append(format.format(freeMemory / 1024)).append("<br/>"); |
| 133 | + sb.append("allocated memory: ").append(format.format(allocatedMemory / 1024)).append("<br/>"); |
| 134 | + sb.append("max memory: ").append(format.format(maxMemory / 1024)).append("<br/>"); |
| 135 | + sb.append("total free memory: ").append(format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024)).append("<br/>"); |
| 136 | + return sb.toString(); |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments