Skip to content

Commit c4c2ff4

Browse files
authored
Merge pull request #6 from shareitcode/develop
Generate v-host for Apache Debian, done!
2 parents dd65d2d + 862db9a commit c4c2ff4

2 files changed

Lines changed: 222 additions & 2 deletions

File tree

Lines changed: 210 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,221 @@
11
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using System.Reflection;
25

36
namespace VHostGenerator.ConsoleApp
47
{
58
internal static class Program
69
{
10+
private static string _vHost = @"<VirtualHost *:80>
11+
ServerName $domain_name
12+
ServerAlias www.$domain_name
13+
14+
DocumentRoot /var/www/$domain_name/
15+
16+
<Directory /var/www/$domain_name/>
17+
Options -Indexes +FollowSymLinks -MultiViews
18+
AllowOverride All
19+
Require all granted
20+
</Directory>
21+
22+
ErrorLog ${APACHE_LOG_DIR}/$domain_name.log
23+
CustomLog ${APACHE_LOG_DIR}/$domain_name.log combined
24+
25+
ServerSignature Off
26+
</VirtualHost>";
27+
private static string _html = @"<!DOCTYPE html>
28+
<html lang=""fr"">
29+
<head>
30+
<meta charset=""UTF-8"">
31+
<meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
32+
<title>Welcome to $domain_name!</title>
33+
</head>
34+
<body>
35+
<h1>$domain_name</h1>
36+
</body>
37+
</html>";
38+
private static string _domaineName = string.Empty;
39+
private static bool _isDomaineValid = false;
40+
741
private static void Main(string[] args)
842
{
9-
Console.WriteLine("Hello World!");
43+
Console.WriteLine("+----------------------------------------+");
44+
Console.WriteLine("+ V-HOST GENERATOR +");
45+
Console.WriteLine($"+ v{Assembly.GetEntryAssembly().GetName().Version.ToString(3)} +");
46+
Console.WriteLine("+ by Share IT Code +");
47+
Console.WriteLine("+ MIT Licence +");
48+
Console.WriteLine("+----------------------------------------+");
49+
50+
while (!_isDomaineValid)
51+
{
52+
Console.Write("\nEnter domaine name (ex: github.com): ");
53+
_domaineName = Console.ReadLine();
54+
CheckDomaineNameValidity(_domaineName);
55+
}
56+
ApacheDebian(_domaineName);
57+
}
58+
59+
private static void CheckDomaineNameValidity(string domaineName)
60+
{
61+
if (string.IsNullOrEmpty(domaineName))
62+
{
63+
Console.WriteLine("Invalide domaine!");
64+
return;
65+
}
66+
_isDomaineValid = true;
67+
}
68+
69+
/// <summary>
70+
/// Generate Apache v-host for Debian
71+
/// </summary>
72+
/// <param name="domaineName"></param>
73+
private static void ApacheDebian(string domaineName)
74+
{
75+
try
76+
{
77+
string webSitePath = $"/var/www/{domaineName}";
78+
Console.WriteLine("\n----- Generate Apache v-host");
79+
PrepareWelcomeHtml(domaineName);
80+
CreateWebSiteDirectory(webSitePath);
81+
82+
PrepareApacheVHost(domaineName);
83+
84+
CreateVHostFileIntoApacheDirectory(domaineName);
85+
86+
EnableApacheVHost(domaineName);
87+
88+
RestartApacheServer();
89+
}
90+
catch (Exception exception)
91+
{
92+
#if DEBUG
93+
Console.WriteLine("---------- EXCEPTION ----------");
94+
Console.WriteLine($"---------- Message: {exception.Message}");
95+
Console.WriteLine($"---------- StackTrace: {exception.StackTrace}");
96+
#else
97+
Console.WriteLine("---------- An error is occured! :(");
98+
#endif
99+
}
100+
}
101+
102+
/// <summary>
103+
///
104+
/// </summary>
105+
private static void RestartApacheServer()
106+
{
107+
Console.WriteLine("\n 5 > Restart Apache server");
108+
RestartServerApacheDebianCommande();
109+
}
110+
111+
/// <summary>
112+
///
113+
/// </summary>
114+
/// <param name="domaineName"></param>
115+
private static void EnableApacheVHost(string domaineName)
116+
{
117+
Console.WriteLine($"\n 4 > Enable v-host (a2ensite {domaineName}.conf)");
118+
ApacheDebianCommande(domaineName);
119+
}
120+
121+
/// <summary>
122+
///
123+
/// </summary>
124+
/// <param name="domaineName"></param>
125+
private static void CreateVHostFileIntoApacheDirectory(string domaineName)
126+
{
127+
string vHostFile = $"/etc/apache2/sites-available/{domaineName}.conf";
128+
Console.WriteLine($"\n 3 > Create v-host file in Apache directory ({vHostFile})");
129+
if (!File.Exists(vHostFile))
130+
{
131+
File.WriteAllText(vHostFile, _vHost);
132+
}
133+
else
134+
{
135+
Console.WriteLine($" --- V-host file in Apache directory ({vHostFile}) already existe!");
136+
}
137+
}
138+
139+
/// <summary>
140+
///
141+
/// </summary>
142+
/// <param name="domaineName"></param>
143+
private static void PrepareApacheVHost(string domaineName)
144+
{
145+
Console.WriteLine("\n 2 > Prepare Apache v-host");
146+
_vHost = _vHost.Replace("$domain_name", domaineName);
147+
}
148+
149+
/// <summary>
150+
///
151+
/// </summary>
152+
/// <param name="domaineName"></param>
153+
private static void PrepareWelcomeHtml(string domaineName)
154+
{
155+
_html = _html.Replace("$domain_name", domaineName);
156+
}
157+
158+
/// <summary>
159+
///
160+
/// </summary>
161+
/// <param name="webSitePath"></param>
162+
private static void CreateWebSiteDirectory(string webSitePath)
163+
{
164+
Console.WriteLine($" 1 > Create web site directory ({webSitePath})");
165+
if (!Directory.Exists(webSitePath))
166+
{
167+
Directory.CreateDirectory(webSitePath);
168+
File.WriteAllText($"{webSitePath}/index.html", _html);
169+
}
170+
else
171+
{
172+
Console.WriteLine($" --- Web site directory ({webSitePath}) already existe!");
173+
}
174+
}
175+
176+
/// <summary>
177+
/// Execute a2ensite Apache commande
178+
/// </summary>
179+
/// <param name="domaineName"></param>
180+
private static void ApacheDebianCommande(string domaineName)
181+
{
182+
Process process = new Process()
183+
{
184+
StartInfo = new ProcessStartInfo
185+
{
186+
FileName = "/bin/bash",
187+
Arguments = $"-c \"a2ensite {domaineName}.conf\"",
188+
RedirectStandardOutput = true,
189+
UseShellExecute = false,
190+
CreateNoWindow = true,
191+
}
192+
};
193+
process.Start();
194+
string result = process.StandardOutput.ReadToEnd();
195+
process.WaitForExit();
196+
Console.WriteLine($" --- a2ensite command result:\n{result}");
197+
}
198+
199+
/// <summary>
200+
/// Execute commande for restart Apache server
201+
/// </summary>
202+
private static void RestartServerApacheDebianCommande()
203+
{
204+
Process process = new Process()
205+
{
206+
StartInfo = new ProcessStartInfo
207+
{
208+
FileName = "/bin/bash",
209+
Arguments = "-c \"/etc/init.d/apache2 restart\"",
210+
RedirectStandardOutput = true,
211+
UseShellExecute = false,
212+
CreateNoWindow = true,
213+
}
214+
};
215+
process.Start();
216+
string result = process.StandardOutput.ReadToEnd();
217+
process.WaitForExit();
218+
Console.WriteLine($" --- apache2 restart command result:\n{result}");
10219
}
11220
}
12221
}
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>netcoreapp3.1</TargetFramework>
66
<Platforms>AnyCPU;x64</Platforms>
7+
<Authors>Share IT Code</Authors>
8+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
9+
<PackageProjectUrl>https://github.com/shareitcode/v-host-generator</PackageProjectUrl>
10+
<SignAssembly>false</SignAssembly>
11+
<AssemblyName>VHostGenerator</AssemblyName>
12+
</PropertyGroup>
13+
14+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
15+
<ErrorReport>none</ErrorReport>
16+
<DebugType>none</DebugType>
17+
<DebugSymbols>false</DebugSymbols>
718
</PropertyGroup>
819

920
</Project>

0 commit comments

Comments
 (0)