|
|
|
@ -14,27 +14,26 @@ namespace Seenginx.Services
|
|
|
|
|
{
|
|
|
|
|
public class NginxService : INginxService
|
|
|
|
|
{
|
|
|
|
|
private readonly ConfigPaths ConfigPaths;
|
|
|
|
|
|
|
|
|
|
private readonly SeenginxSettings Settings;
|
|
|
|
|
private readonly CommandService CommandService;
|
|
|
|
|
|
|
|
|
|
public NginxService(ConfigPaths configPaths, CommandService commandService)
|
|
|
|
|
public NginxService(SeenginxSettings configPaths, CommandService commandService)
|
|
|
|
|
{
|
|
|
|
|
ConfigPaths = configPaths;
|
|
|
|
|
Settings = configPaths;
|
|
|
|
|
CommandService = commandService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<ConfigFile>> GetFilesAsync()
|
|
|
|
|
{
|
|
|
|
|
await Task.Run(() => { });
|
|
|
|
|
var rootConfigs = Directory.GetFiles(ConfigPaths.NginxPath, "*.conf");
|
|
|
|
|
var rootDraftConfigs = Directory.GetFiles(ConfigPaths.NginxPath, "*.conf.draft");
|
|
|
|
|
var rootConfigs = Directory.GetFiles(Settings.nginx.rootPath, "*.conf");
|
|
|
|
|
var rootDraftConfigs = Directory.GetFiles(Settings.nginx.rootPath, "*.conf.draft");
|
|
|
|
|
|
|
|
|
|
var confdConfigs = Directory.GetFiles(Path.Combine(ConfigPaths.NginxPath, "conf.d"), "*.conf");
|
|
|
|
|
var confDraftConfigs = Directory.GetFiles(Path.Combine(ConfigPaths.NginxPath, "conf.d"), "*.conf.draft");
|
|
|
|
|
var confdConfigs = Directory.GetFiles(Path.Combine(Settings.nginx.rootPath, "conf.d"), "*.conf");
|
|
|
|
|
var confDraftConfigs = Directory.GetFiles(Path.Combine(Settings.nginx.rootPath, "conf.d"), "*.conf.draft");
|
|
|
|
|
|
|
|
|
|
var sitesAvailableConfigs = Directory.GetFiles(Path.Combine(ConfigPaths.NginxPath, "sites-available"), "*.conf");
|
|
|
|
|
var sitesAvailableDraftConfigs = Directory.GetFiles(Path.Combine(ConfigPaths.NginxPath, "sites-available"), "*.conf.draft");
|
|
|
|
|
var sitesAvailableConfigs = Directory.GetFiles(Path.Combine(Settings.nginx.rootPath, "sites-available"), "*.conf");
|
|
|
|
|
var sitesAvailableDraftConfigs = Directory.GetFiles(Path.Combine(Settings.nginx.rootPath, "sites-available"), "*.conf.draft");
|
|
|
|
|
|
|
|
|
|
var rootConfigFiles = rootConfigs.Select(fp =>
|
|
|
|
|
{
|
|
|
|
@ -44,7 +43,7 @@ namespace Seenginx.Services
|
|
|
|
|
configFile.Folder = string.Empty;
|
|
|
|
|
configFile.LastUpdated = File.GetLastWriteTime(fp);
|
|
|
|
|
configFile.Name = name;
|
|
|
|
|
configFile.FullPath = Path.Combine(ConfigPaths.NginxPath, $"{configFile.Name}.conf");
|
|
|
|
|
configFile.FullPath = Path.Combine(Settings.nginx.rootPath, $"{configFile.Name}.conf");
|
|
|
|
|
configFile.Body = File.ReadAllText(fp);
|
|
|
|
|
if (rootDraftConfigs.Any(cfp => cfp.Contains(name)))
|
|
|
|
|
configFile.DraftBody = File.ReadAllText(rootDraftConfigs.First(cfp => cfp.Contains(name)));
|
|
|
|
@ -59,7 +58,7 @@ namespace Seenginx.Services
|
|
|
|
|
configFile.Folder = "conf.d";
|
|
|
|
|
configFile.LastUpdated = File.GetLastWriteTime(fp);
|
|
|
|
|
configFile.Name = fileName;
|
|
|
|
|
configFile.FullPath = Path.Combine(ConfigPaths.NginxPath, configFile.Folder, $"{configFile.Name}.conf");
|
|
|
|
|
configFile.FullPath = Path.Combine(Settings.nginx.rootPath, configFile.Folder, $"{configFile.Name}.conf");
|
|
|
|
|
configFile.Body = File.ReadAllText(fp);
|
|
|
|
|
if (confDraftConfigs.Any(cfp => cfp.Contains(fileName)))
|
|
|
|
|
configFile.DraftBody = File.ReadAllText(confDraftConfigs.First(cfp => cfp.Contains(fileName)));
|
|
|
|
@ -74,37 +73,18 @@ namespace Seenginx.Services
|
|
|
|
|
configFile.Folder = "sites-available";
|
|
|
|
|
configFile.LastUpdated = File.GetLastWriteTime(fp);
|
|
|
|
|
configFile.Name = fileName;
|
|
|
|
|
configFile.FullPath = Path.Combine(ConfigPaths.NginxPath, configFile.Folder, $"{configFile.Name}.conf");
|
|
|
|
|
configFile.FullPath = Path.Combine(Settings.nginx.rootPath, configFile.Folder, $"{configFile.Name}.conf");
|
|
|
|
|
configFile.Body = File.ReadAllText(fp);
|
|
|
|
|
if (sitesAvailableDraftConfigs.Any(cfp => cfp.Contains(fileName)))
|
|
|
|
|
configFile.DraftBody = File.ReadAllText(sitesAvailableDraftConfigs.First(cfp => cfp.Contains(fileName)));
|
|
|
|
|
|
|
|
|
|
return configFile;
|
|
|
|
|
});
|
|
|
|
|
//var sitesEnabledConfigFiles = sitesEnabledConfigs.Select(fp =>
|
|
|
|
|
//{
|
|
|
|
|
// var fileName = Path.GetFileName(fp);
|
|
|
|
|
// var configFile = new ConfigFile();
|
|
|
|
|
// configFile.CanBeDeleted = true;
|
|
|
|
|
// configFile.Folder = "/sites-enabled";
|
|
|
|
|
// configFile.LastUpdated = File.GetLastWriteTime(fp);
|
|
|
|
|
// configFile.Name = fileName;
|
|
|
|
|
// configFile.FullPath = Path.Combine(ConfigPaths.NginxPath, configFile.Folder.Replace("/", string.Empty), configFile.Name);
|
|
|
|
|
// configFile.Body = File.ReadAllText(fp);
|
|
|
|
|
// configFile.DraftName = fileName;
|
|
|
|
|
// configFile.DraftBody = configFile.Body;
|
|
|
|
|
// if (confDraftConfigs.Any(cfp => cfp.Contains(fileName)))
|
|
|
|
|
// {
|
|
|
|
|
// configFile.DraftName = Path.GetFileName(confDraftConfigs.First(cfp => cfp.Contains(fileName)));
|
|
|
|
|
// configFile.DraftBody = File.ReadAllText(confDraftConfigs.First(cfp => cfp.Contains(fileName)));
|
|
|
|
|
// }
|
|
|
|
|
// return configFile;
|
|
|
|
|
//});
|
|
|
|
|
|
|
|
|
|
var finalList = new List<ConfigFile>();
|
|
|
|
|
finalList.AddRange(rootConfigFiles);
|
|
|
|
|
finalList.AddRange(confdConfigFiles);
|
|
|
|
|
finalList.AddRange(sitesAvailableConfigFiles);
|
|
|
|
|
//finalList.AddRange(sitesEnabledConfigFiles);
|
|
|
|
|
|
|
|
|
|
finalList = finalList.OrderBy(cf => cf.Name).ToList();
|
|
|
|
|
|
|
|
|
@ -142,7 +122,7 @@ namespace Seenginx.Services
|
|
|
|
|
var validationResult = new Result();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var filePath = Path.Combine(ConfigPaths.NginxPath, "conf.d", $"{newFileForm.Name}.conf");
|
|
|
|
|
var filePath = Path.Combine(Settings.nginx.rootPath, "conf.d", $"{newFileForm.Name}.conf");
|
|
|
|
|
|
|
|
|
|
if (File.Exists(filePath))
|
|
|
|
|
return validationResult.Invalidate($"There's already a file with the '{newFileForm.Name}.conf' name.");
|
|
|
|
@ -161,9 +141,9 @@ namespace Seenginx.Services
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var newFile = new ConfigFile();
|
|
|
|
|
newFile.Name = $"{newFileForm.Name}.conf";
|
|
|
|
|
newFile.Name = newFileForm.Name;
|
|
|
|
|
newFile.Folder = "conf.d";
|
|
|
|
|
newFile.FullPath = Path.Combine(ConfigPaths.NginxPath, newFile.Folder, newFile.Name);
|
|
|
|
|
newFile.FullPath = Path.Combine(Settings.nginx.rootPath, newFile.Folder, $"{newFileForm.Name}.conf");
|
|
|
|
|
newFile.Body = newFileForm.SelectedTemplate == 0.ToString() ? string.Empty : (await GetTemplates()).SingleOrDefault(t => t.Name == newFileForm.SelectedTemplate)?.Code;
|
|
|
|
|
newFile.LastUpdated = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
@ -205,6 +185,8 @@ namespace Seenginx.Services
|
|
|
|
|
return result.Invalidate($"File '{configFile.FullPath}' not found.");
|
|
|
|
|
|
|
|
|
|
File.Delete(configFile.FullPath);
|
|
|
|
|
if (configFile.DraftBody != null)
|
|
|
|
|
File.Delete($"{configFile.FullPath}.draft");
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
@ -236,7 +218,7 @@ namespace Seenginx.Services
|
|
|
|
|
var saveResult = new Result<ConfigFile>();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await File.WriteAllTextAsync(Path.Combine(ConfigPaths.NginxPath, configFile.Folder, $"{configFile.Name}.conf"), configFile.Body, Encoding.UTF8);
|
|
|
|
|
await File.WriteAllTextAsync(Path.Combine(Settings.nginx.rootPath, configFile.Folder, $"{configFile.Name}.conf"), configFile.Body, Encoding.UTF8);
|
|
|
|
|
|
|
|
|
|
return saveResult;
|
|
|
|
|
}
|
|
|
|
@ -270,7 +252,7 @@ namespace Seenginx.Services
|
|
|
|
|
var saveDraftResult = new Result<ConfigFile>();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await File.WriteAllTextAsync(Path.Combine(ConfigPaths.NginxPath, configFile.Folder, $"{configFile.Name}.conf.draft"), configFile.DraftBody, Encoding.UTF8);
|
|
|
|
|
await File.WriteAllTextAsync(Path.Combine(Settings.nginx.rootPath, configFile.Folder, $"{configFile.Name}.conf.draft"), configFile.DraftBody, Encoding.UTF8);
|
|
|
|
|
|
|
|
|
|
return saveDraftResult;
|
|
|
|
|
}
|
|
|
|
@ -288,7 +270,7 @@ namespace Seenginx.Services
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(configFiles.Count(cf => cf.Name == newName) > 0)
|
|
|
|
|
if (configFiles.Count(cf => cf.Name == newName) > 0)
|
|
|
|
|
return renameResult.Invalidate($"File '{selectedConfigFile.FullPath}' already exists.");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -297,7 +279,7 @@ namespace Seenginx.Services
|
|
|
|
|
if (!File.Exists(selectedConfigFile.FullPath))
|
|
|
|
|
return renameResult.Invalidate($"Original file '{selectedConfigFile.FullPath}' not found.");
|
|
|
|
|
|
|
|
|
|
var newPathName = Path.Combine(ConfigPaths.NginxPath, selectedConfigFile.Folder, $"{newName}.conf");
|
|
|
|
|
var newPathName = Path.Combine(Settings.nginx.rootPath, selectedConfigFile.Folder, $"{newName}.conf");
|
|
|
|
|
if (File.Exists(newPathName))
|
|
|
|
|
return renameResult.Invalidate($"The file '{newPathName}' already exists.");
|
|
|
|
|
|
|
|
|
@ -309,15 +291,20 @@ namespace Seenginx.Services
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Result> RenameFileAsync(ConfigFile configFile, string newName)
|
|
|
|
|
public async Task<Result<ConfigFile>> RenameFileAsync(ConfigFile configFile, string newName)
|
|
|
|
|
{
|
|
|
|
|
var renameResult = new Result();
|
|
|
|
|
var renameResult = new Result<ConfigFile>();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var originalPathName = Path.Combine(ConfigPaths.NginxPath, configFile.Folder, $"{configFile.Name}.conf");
|
|
|
|
|
var newPathName = Path.Combine(ConfigPaths.NginxPath, configFile.Folder, newName);
|
|
|
|
|
var originalPathName = Path.Combine(Settings.nginx.rootPath, configFile.Folder, $"{configFile.Name}.conf");
|
|
|
|
|
var newPathName = Path.Combine(Settings.nginx.rootPath, configFile.Folder, newName);
|
|
|
|
|
|
|
|
|
|
File.Move(originalPathName, newPathName, overwrite: false);
|
|
|
|
|
if (configFile.DraftBody != null)
|
|
|
|
|
File.Move($"{originalPathName}.draft", $"{newPathName}.draft", overwrite: false);
|
|
|
|
|
|
|
|
|
|
configFile.FullPath = newPathName;
|
|
|
|
|
renameResult.SetData(configFile);
|
|
|
|
|
|
|
|
|
|
return renameResult;
|
|
|
|
|
}
|
|
|
|
@ -334,7 +321,7 @@ namespace Seenginx.Services
|
|
|
|
|
var command1 = string.Empty;
|
|
|
|
|
var command2 = string.Empty;
|
|
|
|
|
var runResult = new Result<string>();
|
|
|
|
|
var originalPathName = Path.Combine(ConfigPaths.NginxPath, configFile.Folder, $"{configFile.Name}.conf");
|
|
|
|
|
var originalPathName = Path.Combine(Settings.nginx.rootPath, configFile.Folder, $"{configFile.Name}.conf");
|
|
|
|
|
switch (Environment.OSVersion.Platform)
|
|
|
|
|
{
|
|
|
|
|
case PlatformID.Win32S:
|
|
|
|
@ -357,17 +344,5 @@ namespace Seenginx.Services
|
|
|
|
|
return runResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//public async Task<Result> DeleteFileAsync(ConfigFile configFile)
|
|
|
|
|
//{
|
|
|
|
|
// var result = new Result(true);
|
|
|
|
|
// try
|
|
|
|
|
// {
|
|
|
|
|
// return result;
|
|
|
|
|
// }
|
|
|
|
|
// catch (Exception ex)
|
|
|
|
|
// {
|
|
|
|
|
// return result.Invalidate(ex.Message, ex);
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|