package core.log;

import core.framework.http.HTTPMethod;
import core.framework.internal.log.message.EventMessage;
import core.framework.internal.log.message.LogTopics;
import core.framework.module.App;
import core.framework.module.SystemModule;
import core.framework.util.Strings;
import core.log.web.CollectEventRequest;
import core.log.web.CollectEventRequestValidator;
import core.log.web.EventController;

import java.util.HashSet;
import java.util.Set;

/**
 * @author neo
 */
public class LogCollectorApp extends App {
    @Override
    protected void initialize() {
        load(new SystemModule("sys.properties"));
        loadProperties("app.properties");
        http().httpsPort(8444);

        site().security();
        site().staticContent("/robots.txt");

        kafka().publish(LogTopics.TOPIC_EVENT, EventMessage.class);

        bind(CollectEventRequestValidator.class);

        Set<String> allowedOrigins = allowedOrigins(requiredProperty("app.allowedOrigins"));
        EventController controller = bind(new EventController(allowedOrigins));
        http().route(HTTPMethod.OPTIONS, "/event/:app", controller::options);
        http().route(HTTPMethod.PUT, "/event/:app", controller::put);
        http().bean(CollectEventRequest.class);
    }

    Set<String> allowedOrigins(String value) {
        String[] origins = Strings.split(value, ',');
        Set<String> result = new HashSet<>();
        for (String origin : origins) {
            result.add(origin.strip());
        }
        return result;
    }
}

@@ -239,16 +239,49 @@
 Module;%0A
+import core.framework.util.Sets;%0A
 import c
@@ -441,34 +441,8 @@
 r;%0A%0A
-import java.util.HashSet;%0A
 impo
@@ -1402,22 +1402,54 @@
 t = 
+Sets.
 new
- 
 HashSet
-%3C%3E(
+WithExpectedSize(origins.length
 );%0A 

fix spotbugs check