-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from KishoreIthadi/develop
migrated to dotnet core
- Loading branch information
Showing
15 changed files
with
468 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.28307.852 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PingerAPI", "PingerAPI\PingerAPI.csproj", "{B27C07BB-8946-45A1-A66A-0553BA4FB9FF}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{B27C07BB-8946-45A1-A66A-0553BA4FB9FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{B27C07BB-8946-45A1-A66A-0553BA4FB9FF}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{B27C07BB-8946-45A1-A66A-0553BA4FB9FF}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{B27C07BB-8946-45A1-A66A-0553BA4FB9FF}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {982628F0-AB81-47B5-ABDB-703D159B0F38} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using PingerAPI.DTO; | ||
using PingerAPI.Utilities; | ||
using PingerAPI.Enums; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace PingerAPI.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class EmailController : ControllerBase | ||
{ | ||
private IConfiguration _configuration { get; } | ||
private readonly ILogger<EmailController> _logger; | ||
|
||
public EmailController(IConfiguration configuration, | ||
ILogger<EmailController> logger) | ||
{ | ||
_logger = logger; | ||
_configuration = configuration; | ||
} | ||
|
||
[HttpPost] | ||
[Route("SendEmail")] | ||
public void SendEmail([FromBody]List<TaskDTO> list) | ||
{ | ||
Task.Run(() => PrepareEmail(list)); | ||
} | ||
|
||
private void PrepareEmail(List<TaskDTO> emaillist) | ||
{ | ||
try | ||
{ | ||
foreach (var item in emaillist) | ||
{ | ||
if (item.ToEmail != string.Empty) | ||
{ | ||
string status = StringUtility.StatusAlive; | ||
|
||
if (item.PreviousState == (int)TaskStatusEnum.Dead) | ||
{ | ||
status = StringUtility.StatusDead; | ||
} | ||
|
||
string subject = string.Format(StringUtility.EmailSubject, item.Entity, status, DateTime.UtcNow.ToString()); | ||
|
||
Task.Run(() => | ||
{ | ||
EmailUtility.SendEmail(item.ToEmail, subject, StringUtility.EmailBody, _configuration); | ||
}); | ||
} | ||
} | ||
} | ||
catch(Exception ex) | ||
{ | ||
_logger.LogError(ex.Message + ex.StackTrace); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using PingerAPI.DTO; | ||
using PingerAPI.Enums; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace PingerAPI.Controllers | ||
{ | ||
[Route("api/Task")] | ||
[ApiController] | ||
public class TaskController : ControllerBase | ||
{ | ||
private readonly ILogger<TaskController> _logger; | ||
|
||
public TaskController(ILogger<TaskController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
|
||
[HttpGet] | ||
public string Get() | ||
{ | ||
return "Success"; | ||
} | ||
|
||
[HttpPost] | ||
[Route("CheckTaskStatus")] | ||
public async Task<List<TaskDTO>> CheckTaskStatus([FromBody]List<TaskDTO> list) | ||
{ | ||
foreach (var item in list) | ||
{ | ||
switch (item.TaskType) | ||
{ | ||
case (int)TaskTypeEnum.WebSite: | ||
await CheckWebsiteStatus(item); | ||
break; | ||
case (int)TaskTypeEnum.Server: | ||
await CheckServerNDBStatus(item); | ||
break; | ||
case (int)TaskTypeEnum.Database: | ||
await CheckServerNDBStatus(item); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
return list; | ||
} | ||
|
||
private async Task<bool> CheckWebsiteStatus(TaskDTO obj) | ||
{ | ||
try | ||
{ | ||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create((obj.Entity.IndexOf("://") == -1) ? | ||
"http://" + obj.Entity : obj.Entity); | ||
|
||
using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync()) | ||
{ | ||
if (response.StatusCode == HttpStatusCode.OK) | ||
{ | ||
if (obj.PreviousState != (int)TaskStatusEnum.Alive) | ||
{ | ||
obj.UpdatedState = (int)TaskStatusEnum.Alive; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
catch(Exception ex) | ||
{ | ||
_logger.LogError(ex.Message + ex.StackTrace); | ||
} | ||
|
||
if (obj.PreviousState != (int)TaskStatusEnum.Dead) | ||
{ | ||
obj.UpdatedState = (int)TaskStatusEnum.Dead; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private async Task<bool> CheckServerNDBStatus(TaskDTO obj) | ||
{ | ||
try | ||
{ | ||
string[] list = obj.Entity.Split(':'); | ||
|
||
TcpClient client = new TcpClient(); | ||
await client.ConnectAsync(list[0], Convert.ToInt32(list[1])); | ||
|
||
if (obj.PreviousState != (int)TaskStatusEnum.Alive) | ||
{ | ||
obj.UpdatedState = (int)TaskStatusEnum.Alive; | ||
} | ||
|
||
return true; | ||
} | ||
catch(Exception ex) | ||
{ | ||
_logger.LogError(ex.Message + ex.StackTrace); | ||
} | ||
|
||
if (obj.PreviousState != (int)TaskStatusEnum.Dead) | ||
{ | ||
obj.UpdatedState = (int)TaskStatusEnum.Dead; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace PingerAPI.DTO | ||
{ | ||
public class TaskDTO | ||
{ | ||
public int TaskType { get; set; } | ||
public string Entity { get; set; } | ||
public int PreviousState { get; set; } | ||
public int UpdatedState { get; set; } | ||
public string ToEmail { get; set; } | ||
public string Key { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace PingerAPI.Enums | ||
{ | ||
public enum TaskStatusEnum | ||
{ | ||
Alive = 1, | ||
Checking = 2, | ||
Dead = 3, | ||
UnableToFetch = 4 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace PingerAPI.Enums | ||
{ | ||
public enum TaskTypeEnum | ||
{ | ||
WebSite = 1, | ||
Server = 2, | ||
Database = 3, | ||
Settings = 4 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace PingerAPI.Enums | ||
{ | ||
public enum ValidationEnum | ||
{ | ||
Success = 1, | ||
Failure = 2, | ||
ValidationFailed = 3 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="wwwroot\" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.App" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" /> | ||
<PackageReference Include="NLog" Version="4.6.5" /> | ||
<PackageReference Include="NLog.Web.AspNetCore" Version="4.8.4" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
|
||
namespace PingerAPI | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateWebHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => | ||
WebHost.CreateDefaultBuilder(args) | ||
.UseStartup<Startup>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace PingerAPI | ||
{ | ||
public class Startup | ||
{ | ||
public Startup(IConfiguration configuration) | ||
{ | ||
Configuration = configuration; | ||
} | ||
|
||
public IConfiguration Configuration { get; } | ||
|
||
// This method gets called by the runtime. Use this method to add services to the container. | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); | ||
} | ||
|
||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | ||
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | ||
{ | ||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
|
||
app.UseMvc(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Mail; | ||
using Microsoft.Extensions.Configuration; | ||
using PingerAPI.Enums; | ||
|
||
namespace PingerAPI.Utilities | ||
{ | ||
public class EmailUtility | ||
{ | ||
public static void SendEmail(string toMail, string subject, string body, IConfiguration configuration) | ||
{ | ||
//try | ||
//{ | ||
using (SmtpClient smtp = new SmtpClient()) | ||
{ | ||
smtp.Port = configuration.GetValue<int>("Smtp:Port"); | ||
smtp.Host = configuration.GetValue<string>("Smtp:Host"); | ||
smtp.Credentials = new NetworkCredential(configuration.GetValue<string>("Smtp:UserName"), | ||
configuration.GetValue<string>("Smtp:Password")); | ||
smtp.EnableSsl = true; | ||
|
||
using (MailMessage message = new MailMessage()) | ||
{ | ||
if (configuration.GetValue<bool>("Settings:IsProduction")) | ||
{ | ||
foreach (var item in toMail.Replace(" ", "").Split(',')) | ||
{ | ||
if (item != string.Empty) | ||
{ | ||
message.To.Add(new MailAddress(item)); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
string tempMail = configuration.GetValue<string>("Settings:TestMail"); | ||
foreach (var item in tempMail.Replace(" ", "").Split(',')) | ||
{ | ||
if (item != string.Empty) | ||
{ | ||
message.To.Add(new MailAddress(item)); | ||
} | ||
} | ||
} | ||
|
||
message.From = new MailAddress(configuration.GetValue<string>("Smtp:UserName")); | ||
message.Subject = subject; | ||
message.Body = body; | ||
message.IsBodyHtml = true; | ||
|
||
smtp.Send(message); | ||
|
||
//return ValidationEnum.Success; | ||
} | ||
} | ||
//} | ||
//catch(Exception ex) | ||
//{ | ||
// return ValidationEnum.Failure; | ||
//} | ||
} | ||
} | ||
} |
Oops, something went wrong.