package core.framework.module;

import core.framework.util.Strings;

/**
 * @author neo
 */
public final class SystemModule extends Module {
    private final String propertyFileClasspath;

    public SystemModule(String propertyFileClasspath) {
        this.propertyFileClasspath = propertyFileClasspath;
    }

    @Override
    protected void initialize() {
        loadProperties(propertyFileClasspath);

        configureHTTP();

        property("sys.cache.host").ifPresent(host -> {
            if ("local".equals(host)) {
                cache().local();
            } else {
                cache().redis(host);
            }
        });

        property("sys.session.host").ifPresent(host -> {
            if ("local".equals(host)) {
                site().session().local();
            } else {
                site().session().redis(host);
            }
        });

        property("sys.cdn.host").ifPresent(host -> site().cdn().host(host));

        configureLog();

        property("sys.kafka.uri").ifPresent(uri -> kafka().uri(uri));

        configureDB();

        property("sys.redis.host").ifPresent(host -> redis().host(host));

        property("sys.site.publishAPI.allowCIDR").ifPresent(cidrs -> site().publishAPI(Strings.split(cidrs, ',')));
    }

    void configureHTTP() {
        property("sys.http.port").ifPresent(port -> http().httpPort(Integer.parseInt(port)));

        property("sys.https.port").ifPresent(port -> http().httpsPort(Integer.parseInt(port)));

        property("sys.http.allowCIDR").ifPresent(cidrs -> http().allowCIDR(Strings.split(cidrs, ',')));
    }

    private void configureLog() {
        property("sys.log.appender").ifPresent(appender -> {
            if ("console".equals(appender)) {
                log().writeToConsole();
            } else {
                log().writeToKafka(appender);
            }
        });
    }

    private void configureDB() {
        property("sys.jdbc.url").ifPresent(url -> db().url(url));
        property("sys.jdbc.user").ifPresent(user -> db().user(user));
        property("sys.jdbc.password").ifPresent(password -> db().password(password));
    }
}

@@ -423,24 +423,308 @@
 gureHTTP();%0A
+        configureCache();%0A        configureLog();%0A        property(%22sys.kafka.uri%22).ifPresent(uri -%3E kafka().uri(uri));%0A        configureDB();%0A        property(%22sys.redis.host%22).ifPresent(host -%3E redis().host(host));%0A        configureSite();%0A    %7D%0A%0A    private void configureCache() %7B
 %0A        pro
@@ -916,32 +916,73 @@
   %7D%0A        %7D);%0A
+    %7D%0A%0A    private void configureSite() %7B
 %0A        propert
@@ -1198,17 +1198,16 @@
     %7D);%0A
-%0A
         
@@ -1279,204 +1279,119 @@
 ));%0A
-%0A        configureLog();%0A%0A        property(%22sys.kafka.uri%22).ifPresent(uri -%3E kafka().uri(uri));%0A%0A        configureDB();%0A%0A        property(%22sys.redis.host%22).ifPresent(host -%3E redis().host(host
+        property(%22sys.publishAPI.allowCIDR%22).ifPresent(cidrs -%3E site().publishAPI(Strings.split(cidrs, ',')
 ));%0A
-%0A
     
@@ -1412,72 +1412,89 @@
 sys.
-site.publishAPI.allowCIDR%22).ifPresent(cidrs -%3E site().publishAPI
+webSecurity.trustedSources%22).ifPresent(trustedSources -%3E site().enableWebSecurity
 (Str
@@ -1496,36 +1496,45 @@
 y(Strings.split(
-cidr
+trustedSource
 s, ',')));%0A    %7D
@@ -1648,33 +1648,32 @@
 rseInt(port)));%0A
-%0A
         property
@@ -1752,17 +1752,16 @@
 ort)));%0A
-%0A
         

tweak sys module