package server;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.javalin.Javalin;
import io.javalin.plugin.json.JavalinJackson;
import sudoku.Candidate;
import sudoku.Sudoku;

import java.util.Set;

public class WebApplication {

    public static void main(String... args) {
        Javalin app = Javalin.create(config -> {
            config.defaultContentType = "application/json";
            config.addStaticFiles("/webapp/build");
        }).start(7000);

        app.post("/solve", ctx -> {
            Sudoku puzzle = JavalinJackson.defaultObjectMapper().readValue(ctx.body(), Sudoku.class);

            Set<Candidate> solution = puzzle.solve();

            ObjectWriter jsonWriter = new ObjectMapper()
                    .enable(SerializationFeature.WRAP_ROOT_VALUE)
                    .writer()
                    .withRootName("solution");

            ctx.result(jsonWriter.writeValueAsString(solution)).contentType("application/json");
            ctx.status(200);
        });
    }
}

@@ -371,35 +371,178 @@
 tic 
-void main(String... args) %7B
+final String DEFAULT_PORT = %227000%22;%0A%0A    public static void main(String... args) %7B%0A        int port = Integer.valueOf(System.getenv().getOrDefault(%22PORT%22, DEFAULT_PORT));
 %0A   
@@ -716,20 +716,20 @@
 ).start(
-7000
+port
 );%0A%0A    

read server-side port from environment