Rome refactoring of virtual hosts
This commit is contained in:
parent
813d48ca05
commit
70ad01568b
4 changed files with 123 additions and 98 deletions
|
|
@ -2,18 +2,23 @@ package wa.server.plugin.apache;
|
|||
|
||||
import wa.server.WAConstants;
|
||||
import wa.server.plugin.WAServiceConfig;
|
||||
import wa.server.util.ConfigFileUtil;
|
||||
import zutil.db.DBConnection;
|
||||
import zutil.db.bean.DBBean;
|
||||
import zutil.db.bean.DBBean.DBTable;
|
||||
import zutil.io.IOUtil;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class ApacheConfigVirtualHost implements WAServiceConfig{
|
||||
public static final String CONFIG_NAME = "Apache Virtual Host";
|
||||
private static final String CONFIG_NAME = "Apache Virtual Host";
|
||||
private static final String APACHE_MAIN_CONFIG_FILE = "apache2/apache2.conf";
|
||||
private static final String STATIC_PRE_CONF = "wa/server/plugin/apache/apache_default.config";
|
||||
|
||||
private List<VirtualHostData> vhosts = new ArrayList<>();
|
||||
private List<ApacheVirtualHostData> vhosts = new ArrayList<>();
|
||||
|
||||
|
||||
@Override
|
||||
|
|
@ -24,23 +29,37 @@ public class ApacheConfigVirtualHost implements WAServiceConfig{
|
|||
@Override
|
||||
public void read() throws Exception {
|
||||
DBConnection db = WAConstants.getDB();
|
||||
vhosts = DBBean.load(db, VirtualHostData.class);
|
||||
vhosts = DBBean.load(db, ApacheVirtualHostData.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() throws Exception {
|
||||
|
||||
DBConnection db = WAConstants.getDB();
|
||||
for(ApacheVirtualHostData vhost : vhosts){
|
||||
vhost.save(db);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure() throws Exception {
|
||||
// Update main configuration file
|
||||
ConfigFileUtil.writeBetweenBoundary(
|
||||
WAConstants.getConfigFile(APACHE_MAIN_CONFIG_FILE),
|
||||
"#",
|
||||
IOUtil.getContentString(
|
||||
new FileInputStream(STATIC_PRE_CONF)));
|
||||
|
||||
// Write Vhost configuration
|
||||
for(ApacheVirtualHostData vhost : vhosts){
|
||||
vhost.configure();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@DBTable(WAConstants.DB_TABLE_PREFIX + "_apache_vhost")
|
||||
public static class VirtualHostData extends DBBean{
|
||||
public static class ApacheVirtualHostData extends DBBean{
|
||||
protected String domain;
|
||||
protected String domain_old;
|
||||
protected String path;
|
||||
protected boolean ssl;
|
||||
|
||||
|
|
@ -64,5 +83,44 @@ public class ApacheConfigVirtualHost implements WAServiceConfig{
|
|||
public void setSSL(boolean ssl) {
|
||||
this.ssl = ssl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public final void configure() throws IOException {
|
||||
if(domain != domain_old){ // Configuration file has changed name
|
||||
WAConstants.getConfigFile("apache2/enabled/"+ domain_old).delete();
|
||||
}
|
||||
PrintStream out = new PrintStream(
|
||||
WAConstants.getConfigFile("apache2/enabled/"+ domain));
|
||||
|
||||
writeConfiguration(out);
|
||||
out.close();
|
||||
}
|
||||
public void writeConfiguration(PrintStream out){
|
||||
if(ssl){
|
||||
out.println("<VirtualHost *:80>");
|
||||
out.println(" ServerName "+ domain +":80");
|
||||
out.println(" RewriteEngine On");
|
||||
out.println(" RewriteCond %{SERVER_PORT} !^443$");
|
||||
out.println(" RewriteRule ^(.*)$ https://server$1 [L,R]");
|
||||
out.println("</VirtualHost>");
|
||||
out.println("<VirtualHost *:443>");
|
||||
out.println(" ServerName "+ domain +":443");
|
||||
out.println(" DocumentRoot "+ path);
|
||||
out.println();
|
||||
out.println(" SSLEngine on");
|
||||
out.println(" SSLCertificateFile "+ WAConstants.getConfigFile(WAConstants.WA_SSL_CERT));
|
||||
out.println(" SSLCertificateKeyFile "+ WAConstants.getConfigFile(WAConstants.WA_SSL_KEY));
|
||||
out.println("</VirtualHost>");
|
||||
out.println("");
|
||||
}
|
||||
else {
|
||||
out.println("<VirtualHost *:80>");
|
||||
out.println(" ServerName " + domain + ":80");
|
||||
out.println(" DocumentRoot " + path);
|
||||
out.println("</VirtualHost>");
|
||||
out.println("");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue