/// <summary> /// Creates a VHOST form with the specified values /// </summary> /// <param name="defaultValues">Default values that will be placed into the appropriate controls</param> public VhostForm(VhostSettings defaultValues) { InitializeComponent(); // based on what default values we where passed go ahead and populate our controls as appropriate this.publicRootTextbox.Text = defaultValues.PublicRoot; this.serverNameTextbox.Text = defaultValues.ServerName; this.serverAdminTextBox.Text = defaultValues.ServerAdmin; this.logLocationTextBox.Text = defaultValues.LogLocation; }
/// <summary> /// Creates a VHOST entry based on supplied parameters /// </summary> /// <param name="settings">Setting configurations</param> /// <param name="documentRoot">Where are we storing it</param> public void CreateVHost(VhostSettings settings) { string basename = Directory.GetParent(settings.PublicRoot).FullName; StringBuilder entry = new StringBuilder(); entry.AppendLine("<VirtualHost *:80>"); entry.AppendLine("\tServerAdmin " + settings.ServerAdmin); entry.AppendLine("\tDocumentRoot \"" + settings.PublicRoot + "\""); entry.AppendLine("\tServerName " + settings.ServerName); entry.AppendLine("\tErrorLog \"" + settings.LogLocation + "\\error.log\""); entry.AppendLine("\tCustomLog \"" + settings.LogLocation + "\\access.log\" combined"); entry.AppendLine("</VirtualHost>"); File.AppendAllText(this.vhosts, entry.ToString()); }
/// <summary> /// Create a new vhost and hosts entry whenver this button is clicked /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void createVHost_Click(object sender, EventArgs e) { using (FolderBrowserDialog dialog = new FolderBrowserDialog()) { if(dialog.ShowDialog() == DialogResult.OK) { string[] directoryParts = Path.GetDirectoryName(dialog.SelectedPath).Split('\\'); string serverName = directoryParts[directoryParts.Length - 1].ToLower().Replace('-', '_').Replace(" ", "").Replace(".","_").Trim(); // populate our default values now VhostSettings defaultSettings = new VhostSettings(); defaultSettings.ServerName = serverName; defaultSettings.PublicRoot = dialog.SelectedPath; defaultSettings.LogLocation = dialog.SelectedPath; defaultSettings.ServerAdmin = "admin@localhost"; // pop a dialog open to configure vhost using (VhostForm vhostDialog = new VhostForm(defaultSettings)) { if (vhostDialog.ShowDialog() == DialogResult.OK) { VhostSettings settings = vhostDialog.Settings; // use our utility classes to add a HOSTS file entry and a VHOSTS entry at the same time WindowsHost.AddEntry("127.0.0.1", settings.ServerName); this.apacheUtility.CreateVHost(settings); // add items to respective list boxes hostsListBox.Items.Add("127.0.0.1 " + settings.ServerName); vhostListBox.Items.Add(serverName); } } } } }