package com.skogsrud.halvard.sparkjava.spike;

import com.fasterxml.jackson.databind.ObjectMapper;

import static spark.Spark.before;
import static spark.SparkBase.port;

public class Launch {
    public static void main(String[] args) throws Exception {
        new Launch().run();
    }

    public void run() throws Exception {
        port(4567);

        ObjectMapper objectMapper = new ObjectMapper();

        new HelloWorld().run();
        new Books(objectMapper).run();
        new Image().run();

        disableCaching();
    }

    /**
     * Disables caching across HTTP 1.0 and 1.1 client and proxy caches
     */
    private void disableCaching() {
        before((request, response) -> {
            response.header("cache-control", "no-cache, no-store, must-revalidate"); // HTTP 1.1
            response.header("pragma", "no-cache"); // HTTP 1.0
            response.header("expires", "0"); // HTTP 1.0 proxies
        });
    }
}

@@ -91,16 +91,40 @@
 tMapper;
+%0Aimport spark.SparkBase;
 %0A%0Aimport
@@ -365,12 +365,17 @@
 ort(
-4567
+getPort()
 );%0A%0A
@@ -526,24 +526,24 @@
 e().run();%0A%0A
-
         disa
@@ -559,24 +559,542 @@
 g();%0A    %7D%0A%0A
+    /**%0A     * Use port from 'PORT' environment variable or 'port' system property, default is 4567.%0A     * Specify port 0 for a randomly assigned port.%0A     */%0A    private int getPort() %7B%0A        int port = SparkBase.SPARK_DEFAULT_PORT; // 4567%0A        if (System.getProperty(%22port%22) != null) %7B%0A            port = Integer.getInteger(System.getProperty(%22port%22));%0A        %7D%0A        if (System.getenv(%22PORT%22) != null) %7B%0A            port = Integer.getInteger(System.getenv(%22PORT%22));%0A        %7D%0A        return port;%0A    %7D%0A%0A
     /**%0A    

Read port from environment variable or system property