package com.koenv.jsonapi.http;

import com.koenv.jsonapi.http.websocket.JSONAPIWebSocket;
import org.jetbrains.annotations.Nullable;

import static spark.Spark.*;

public class JSONAPIWebServer {

    /**
     * Starts the web server
     *
     * @param ipAddress The IP address to bind to or `null` if binding to all interfaces
     * @param port      The port to bind to or a value less than 1 if the port should be chosen automatically
     */
    public void start(@Nullable String ipAddress, int port) {
        if (ipAddress != null) {
            ipAddress(ipAddress);
        }
        if (port > 0) {
            port(port);
        }

        webSocket("/api/v1/websocket", JSONAPIWebSocket.class); // this needs to be first otherwise the web socket doesn't work

        before("/api/v1/*", (request, response) -> {
            // halt(401, "Unauthorized");
        });

        get("/api/v1/request", (req, res) -> "Request");

        init();
    }
}

@@ -781,28 +781,14 @@
     
-before(%22/api/v1/*%22, 
+after(
 (req
@@ -824,105 +824,552 @@
     
-// halt(401, %22Unauthorized%22);%0A        %7D);%0A%0A        get(%22/api/v1/request%22, (req, res) -%3E %22Request%22
+response.header(%22Access-Control-Allow-Origin%22, %22*%22);%0A            response.header(%22Access-Control-Allow-Methods%22, %22GET,PUT,POST,DELETE,OPTIONS%22);%0A            response.header(%22Access-Control-Allow-Headers%22, %22Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin,%22);%0A            response.header(%22Access-Control-Allow-Credentials%22, %22true%22);%0A        %7D);%0A%0A        get(%22/api/v1/request%22, (req, res) -%3E %22Request%22);%0A%0A        post(%22/api/v1/call%22, (req, res) -%3E %7B%0A            req.body();%0A            return %22Request received%22;%0A        %7D
 );%0A%0A

Add CORS headers