package net.kedare.iot.leds;

import com.qmetric.spark.authentication.AuthenticationDetails;
import com.qmetric.spark.authentication.BasicAuthenticationFilter;
import net.kedare.iot.particle.ParticleException;
import org.json.JSONObject;
import spark.servlet.SparkApplication;

import java.util.logging.Level;
import java.util.logging.Logger;

import static spark.Spark.*;

public class LedStripApplication implements SparkApplication {

    private LedStrip ledStrip;
    private String token;
    private String deviceId;
    private Logger logger;

    public LedStripApplication() {
        this.token = System.getenv("LED_CONTROLLER_TOKEN");
        this.deviceId = System.getenv("LED_CONTROLLER_DEVICE");

        this.logger = Logger.getLogger(LedStripApplication.class.getName());

        try {
            this.ledStrip = new LedStrip(token, deviceId, logger);
        } catch (ParticleException exception) {
            logger.log(Level.SEVERE, "Fail to initialize Particle", exception);
        }
    }

    public static void main(String[] args) throws Exception {
        int configuredPort;
        try {
            configuredPort = Integer.valueOf(System.getenv("PORT"));
        } catch (Exception e) {
            configuredPort = 80;
        }
        port(configuredPort);
        LedStripApplication controller = new LedStripApplication();
        controller.init();
    }

    public void init() {

        externalStaticFileLocation("target/static");

        before(new BasicAuthenticationFilter("/*", new AuthenticationDetails(System.getenv("LED_CONTROLLER_USER"), System.getenv("LED_CONTROLLER_PASS"))));

        // Modes list
        get("/api/modes", (req, res) -> {
            res.type("application/json");
            return new JSONObject(LedStrip.PROGRAMS);
        });

        // GET/SET mode
        get("/api/mode", (req, res) -> {
            res.type("application/json");
            return this.ledStrip.getMode();
        });

        post("/api/mode", (req, res) -> {
            res.type("application/json");
            return this.ledStrip.setMode(req.queryParams("mode"));
        });

        // GET/SET Power
        get("/api/power", (req, res) -> {
            res.type("application/json");
            return this.ledStrip.getPower();
        });

        post("/api/power", (req, res) -> {
            res.type("application/json");
            return this.ledStrip.setPower(Integer.parseInt(req.queryParams("power")));
        });

        // GET/SET Wait
        get("/api/wait", (req, res) -> {
            res.type("application/json");
            return this.ledStrip.getDelay();
        });

        post("/api/wait", (req, res) -> {
            res.type("application/json");
            return this.ledStrip.setDelay(Integer.parseInt(req.queryParams("wait")));
        });

        // GET/SET color
        get("/api/color/:index", (req, res) -> {
            res.type("application/json");
            return this.ledStrip.getColor(Integer.parseInt(req.params("index")));
        });

        post("/api/color/:index", (req, res) -> {
            res.type("application/json");
            return this.ledStrip.setColor(Integer.parseInt(req.params("index")), req.queryParams("color"));
        });
    }
}

@@ -517,16 +517,70 @@
 viceId;%0A
+    private String user;%0A    private String password;%0A
     priv
@@ -631,16 +631,33 @@
 cation()
+ throws Exception
  %7B%0A     
@@ -779,85 +779,629 @@
 %22);%0A
-%0A        this.logger = Logger.getLogger(LedStripApplication.class.getName());
+        this.user = System.getenv(%22LED_CONTROLLER_USER%22);%0A        this.password = System.getenv(%22LED_CONTROLLER_PASS%22);%0A%0A        this.logger = Logger.getLogger(LedStripApplication.class.getName());%0A%0A        if (this.token == null %7C%7C this.deviceId == null %7C%7C this.user == null %7C%7C this.password == null) %7B%0A            logger.log(Level.SEVERE, %22Please make sure all the required environment variables are configured : %22);%0A            logger.log(Level.SEVERE, %22LED_CONTROLLER_TOKEN, LED_CONTROLLER_DEVICE, LED_CONTROLLER_USER, LED_CONTROLLER_PASS%22);%0A            throw new Exception(%22Missing environment variables%22);%0A        %7D
 %0A%0A  
@@ -2029,17 +2029,16 @@
 nit() %7B%0A
-%0A
         
@@ -2078,25 +2078,24 @@
 t/static%22);%0A
-%0A
         befo
@@ -2163,82 +2163,32 @@
 ils(
-System.getenv(%22LED_CONTROLLER_USER%22), System.getenv(%22LED_CONTROLLER_PASS%22)
+this.user, this.password
 )));

Add environment variable check