package com.ctrip.zeus.service.build.conf;

import com.ctrip.zeus.exceptions.ValidationException;
import com.ctrip.zeus.model.entity.Domain;
import com.ctrip.zeus.model.entity.Group;
import com.ctrip.zeus.model.entity.Slb;
import com.ctrip.zeus.model.entity.VirtualServer;
import com.ctrip.zeus.util.AssertUtils;
import com.ctrip.zeus.util.StringFormat;

import java.util.List;

/**
 * @author:xingchaowang
 * @date: 3/8/2015.
 */
public class ServerConf {

    public static String generate(Slb slb, VirtualServer vs, List<Group> groups) throws Exception{
        StringBuilder b = new StringBuilder(1024);

        AssertUtils.assertNotNull(vs.getPort(), "virtual server [" + vs.getId() + "] port is null! Please check the configuration!");
        try{
            Integer.parseInt(vs.getPort());
        }catch (Exception e){
            throw new ValidationException("virtual server ["+vs.getId()+"] port is illegal!");
        }


        b.append("server {").append("\n");
        b.append("listen    ").append(vs.getPort()).append(";\n");
        b.append("server_name    ").append(getServerNames(vs)).append(";\n");
        if (vs.getSsl())
        {
            b.append("ssl on;\n")
             .append("ssl_certificate /data/nginx/").append(vs.getId()).append("/ssl.crt;\n")
             .append("ssl_certificate_key /data/nginx/").append(vs.getId()).append("/ssl.key;\n");
        }
        NginxConf.appendServerCommand(b);
        //add locations
        for (Group group : groups) {
            b.append(LocationConf.generate(slb, vs, group, UpstreamsConf.buildUpstreamName(slb, vs, group)));
        }

        b.append("}").append("\n");

        return StringFormat.format(b.toString());
    }

    private static String getServerNames(VirtualServer vs) throws Exception{
        StringBuilder b = new StringBuilder(128);
        for (Domain domain : vs.getDomains()) {
            b.append(" ").append(domain.getName());
        }
        String res =  b.toString();

        AssertUtils.assertNotEquals("", res.trim(), "virtual server [" + vs.getId() + "] domain is null or illegal!");
        return res;
    }
}

@@ -351,110 +351,487 @@
 at;%0A
-%0Aimport java.util.List;%0A%0A/**%0A * @author:xingchaowang%0A * @date: 3/8/2015.%0A */%0Apublic class ServerConf %7B
+import com.netflix.config.DynamicPropertyFactory;%0Aimport com.netflix.config.DynamicStringProperty;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport java.util.List;%0A%0A/**%0A * @author:xingchaowang%0A * @date: 3/8/2015.%0A */%0Apublic class ServerConf %7B%0A    private static final Logger LOGGER = LoggerFactory.getLogger(ServerConf.class);%0A    private static DynamicStringProperty allowSSL = DynamicPropertyFactory.getInstance().getStringProperty(%22virtual-server-id.ssl%22, %22%22);
 %0A%0A  
@@ -1305,18 +1305,16 @@
       %7D%0A
-%0A%0A
         
@@ -1528,16 +1528,227 @@
       %7B%0A
+            String %5B%5DsslList = allowSSL.get().split(%22;%22);%0A            for (String vsid : sslList)%0A            %7B%0A                try %7B%0A                    if(vs.getId().equals(Long.parseLong(vsid)))%7B%0A            
         
@@ -1778,32 +1778,44 @@
 %22)%0A             
+           
+ 
 .append(%22ssl_cer
@@ -1875,24 +1875,36 @@
 sl.crt;%5Cn%22)%0A
+            
             
@@ -1990,33 +1990,192 @@
 y;%5Cn%22);%0A        
-%7D
+            %7D%0A                %7Dcatch (Exception e)%7B%0A                    LOGGER.warn(%22build ssl config Exception:%22,e);%0A                %7D%0A            %7D%0A        %7D%0A
 %0A        NginxCo

add ssl flag