From 16f9fa51ee05b888c59cd0201bcf634b54cd8f14 Mon Sep 17 00:00:00 2001 From: Tommy Parnell Date: Wed, 16 Mar 2016 21:33:52 -0400 Subject: [PATCH] init first version --- LICENSE | 21 ++++++++ Readme.md | 49 ++++++++++++++++++ src/Alive.Net.Example/Startup.cs | 27 +++++++--- src/Alive.Net.Example/wwwroot/_references.js | 8 +-- .../wwwroot/css/site.min.css | 1 - src/Alive.Net.Example/wwwroot/js/site.min.js | 0 .../Alive.Net.UnitTests.xproj | 7 +-- src/Alive.Net.UnitTests/Class1.cs | 16 ------ src/Alive.Net.UnitTests/MainUnitTests.cs | 45 +++++++++++++++++ src/Alive.Net.UnitTests/project.json | 19 +++---- src/Alive.Net/Alive.cs | 50 ++++++++++--------- src/Alive.Net/AliveOptions.cs | 8 +-- src/Alive.Net/AliveResponse.cs | 15 ++++++ src/Alive.Net/Properties/AssemblyInfo.cs | 2 +- src/Alive.Net/project.json | 8 +-- 15 files changed, 204 insertions(+), 72 deletions(-) create mode 100644 LICENSE create mode 100644 Readme.md delete mode 100644 src/Alive.Net.Example/wwwroot/css/site.min.css delete mode 100644 src/Alive.Net.Example/wwwroot/js/site.min.js delete mode 100644 src/Alive.Net.UnitTests/Class1.cs create mode 100644 src/Alive.Net.UnitTests/MainUnitTests.cs create mode 100644 src/Alive.Net/AliveResponse.cs diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e41b5e7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Tommy Parnel + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..c90230e --- /dev/null +++ b/Readme.md @@ -0,0 +1,49 @@ +This is a simple asp.net core 1.0 middlewear to add a livecheck to your app without having to make controllers, and what not. This is useful to keep your livecheck requests out of your MVC action filter lifecycle + + + + +## Usage + +`Install-Package Alive.Net` + +#### Ultra-Simple: +```c# +//this will default to /livecheck with 200 +app.UseAlive(a => + { + + }); + +``` +#### Simple +```c# + +app.UseAlive(a => + { + a.BodyText = "Im awesome"; + a.StatusCode = System.Net.HttpStatusCode.OK; + a.LivecheckPath = new Microsoft.AspNet.Http.PathString("/CustomLivecheck"); + }); + +``` + +#### Complex +```c# + +app.UseAlive(a => + a.OnLivecheckResponse = (response) => + { + if(ThingsThatCouldMakeOurAppDown) + { + response.BodyText = "awesome"; + response.StatusCode = System.Net.HttpStatusCode.BadGateway; + } + else + { + response.BodyText = "awesome"; + response.StatusCode = System.Net.HttpStatusCode.OK; + } + }); + +``` diff --git a/src/Alive.Net.Example/Startup.cs b/src/Alive.Net.Example/Startup.cs index 201d7a7..91e19dc 100644 --- a/src/Alive.Net.Example/Startup.cs +++ b/src/Alive.Net.Example/Startup.cs @@ -1,12 +1,12 @@ -using Microsoft.AspNet.Builder; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace Alive.Net.Example { @@ -36,7 +36,7 @@ namespace Alive.Net.Example loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); - if (env.IsDevelopment()) + if(env.IsDevelopment()) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(); @@ -48,6 +48,21 @@ namespace Alive.Net.Example app.UseAlive(a => { a.BodyText = "Im awesome"; + a.StatusCode = System.Net.HttpStatusCode.OK; + a.LivecheckPath = new Microsoft.AspNet.Http.PathString("/CustomLivecheck"); + }); + app.UseAlive(a => a.OnLivecheckResponse = (response) => + { + if(true) + { + response.BodyText = "awesome"; + response.StatusCode = System.Net.HttpStatusCode.BadGateway; + } + else + { + response.BodyText = "awesome"; + response.StatusCode = System.Net.HttpStatusCode.OK; + } }); app.UseIISPlatformHandler(); diff --git a/src/Alive.Net.Example/wwwroot/_references.js b/src/Alive.Net.Example/wwwroot/_references.js index a48b29e..9b2a791 100644 --- a/src/Alive.Net.Example/wwwroot/_references.js +++ b/src/Alive.Net.Example/wwwroot/_references.js @@ -1,7 +1,7 @@ /// /// -/// -/// -/// -/// /// +/// +/// +/// +/// diff --git a/src/Alive.Net.Example/wwwroot/css/site.min.css b/src/Alive.Net.Example/wwwroot/css/site.min.css deleted file mode 100644 index c12e1a1..0000000 --- a/src/Alive.Net.Example/wwwroot/css/site.min.css +++ /dev/null @@ -1 +0,0 @@ -body{padding-top:50px;padding-bottom:20px}.body-content{padding-left:15px;padding-right:15px}input,select,textarea{max-width:280px}.carousel-caption{z-index:10!important}.carousel-caption p{font-size:20px;line-height:1.4}@media (min-width:768px){.carousel-caption{z-index:10!important}} \ No newline at end of file diff --git a/src/Alive.Net.Example/wwwroot/js/site.min.js b/src/Alive.Net.Example/wwwroot/js/site.min.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/Alive.Net.UnitTests/Alive.Net.UnitTests.xproj b/src/Alive.Net.UnitTests/Alive.Net.UnitTests.xproj index 5f780f1..4362708 100644 --- a/src/Alive.Net.UnitTests/Alive.Net.UnitTests.xproj +++ b/src/Alive.Net.UnitTests/Alive.Net.UnitTests.xproj @@ -4,7 +4,6 @@ 14.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - 4ca8001b-27c7-4671-b451-2c2da00a3827 @@ -12,9 +11,11 @@ ..\..\artifacts\obj\$(MSBuildProjectName) ..\..\artifacts\bin\$(MSBuildProjectName)\ - 2.0 + + + - + \ No newline at end of file diff --git a/src/Alive.Net.UnitTests/Class1.cs b/src/Alive.Net.UnitTests/Class1.cs deleted file mode 100644 index c14b995..0000000 --- a/src/Alive.Net.UnitTests/Class1.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Alive.Net.UnitTests -{ - // This project can output the Class library as a NuGet Package. - // To enable this option, right-click on the project and select the Properties menu item. In the Build tab select "Produce outputs on build". - public class Class1 - { - public Class1() - { - } - } -} diff --git a/src/Alive.Net.UnitTests/MainUnitTests.cs b/src/Alive.Net.UnitTests/MainUnitTests.cs new file mode 100644 index 0000000..88f6f7b --- /dev/null +++ b/src/Alive.Net.UnitTests/MainUnitTests.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNet.Http; +using Moq; +using Xunit; + +namespace Alive.Net.UnitTests +{ + public class MainUnitTests + { + [Fact] + public void ShouldThrowIfNoNext() + { + Assert.Throws(() => new Alive(null, new AliveOptions())); + } + + [Fact] + public void ShouldConstructIfOptionsIsNull() + { + var mockedContext = new Mock(); + var al = new Alive((context) => Task.Delay(0), null); + } + + [Fact] + public void CalculateResponseThrowsOnNullOptions() + { + Assert.Throws(() => Alive.CalculateResponse(null)); + } + + [Fact] + public void EnsureLivecheckFuncOverridesReturnData() + { + var t = new AliveOptions + { + BodyText = "awesome", + StatusCode = System.Net.HttpStatusCode.MovedPermanently, + OnLivecheckResponse = (d) => { d.StatusCode = System.Net.HttpStatusCode.Moved; } + }; + var calculatedResponse = Alive.CalculateResponse(t); + Assert.True(calculatedResponse.StatusCode == System.Net.HttpStatusCode.Moved); + } + } +} \ No newline at end of file diff --git a/src/Alive.Net.UnitTests/project.json b/src/Alive.Net.UnitTests/project.json index 01fd7c9..8e613b4 100644 --- a/src/Alive.Net.UnitTests/project.json +++ b/src/Alive.Net.UnitTests/project.json @@ -6,18 +6,15 @@ "projectUrl": "", "licenseUrl": "", "dependencies": { - "Alive.Net": "" + "Alive.Net": "", + "xunit": "2.1.0", + "xunit.runner.dnx": "2.1.0-rc1-build204", + "Moq": "4.2.1507.118" }, "frameworks": { - "net451": { }, - "dotnet5.4": { - "dependencies": { - "Microsoft.CSharp": "4.0.1-beta-23516", - "System.Collections": "4.0.11-beta-23516", - "System.Linq": "4.0.1-beta-23516", - "System.Runtime": "4.0.21-beta-23516", - "System.Threading": "4.0.11-beta-23516" - } - } + "dnx451": { } + }, + "commands": { + "test": "xunit.runner.dnx" } } \ No newline at end of file diff --git a/src/Alive.Net/Alive.cs b/src/Alive.Net/Alive.cs index 611ec2f..cf246c7 100644 --- a/src/Alive.Net/Alive.cs +++ b/src/Alive.Net/Alive.cs @@ -1,8 +1,8 @@ -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Http; -using System; +using System; using System.IO; using System.Threading.Tasks; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Http; namespace Alive.Net { @@ -10,12 +10,12 @@ namespace Alive.Net { private RequestDelegate _next; - private AliveOptions Options { get; set; } + internal AliveOptions Options { get; set; } public Alive(RequestDelegate next, AliveOptions options) { Options = options ?? new AliveOptions(); - if (next == null) + if(next == null) { throw new ArgumentNullException(nameof(next), "RequestDelegate not passed in"); } @@ -24,19 +24,35 @@ namespace Alive.Net public async Task Invoke(HttpContext context) { - if (context.Request.Path.Equals(this.Options.LivecheckPath, StringComparison.CurrentCultureIgnoreCase)) + if(context.Request.Path.Equals(this.Options.LivecheckPath, StringComparison.CurrentCultureIgnoreCase)) { - context.Response.StatusCode = (int)this.Options.ReturnStatusCode; - if (!string.IsNullOrWhiteSpace(Options.BodyText)) + var response = CalculateResponse(this.Options); + context.Response.StatusCode = (int)response.StatusCode; + if(!string.IsNullOrWhiteSpace(response.BodyText)) { - await context.Response.WriteAsync(Options.BodyText); + await context.Response.WriteAsync(response.BodyText); } } else { - await _next?.Invoke(context); + _next?.Invoke(context); } - //context.Request.Path. + } + + public static AliveResponse CalculateResponse(AliveOptions options) + { + if(options == null) + { + throw new ArgumentNullException("options"); + } + var response = new AliveResponse(); + if(!string.IsNullOrWhiteSpace(options.BodyText)) + { + response.BodyText = options.BodyText; + } + response.StatusCode = options.StatusCode; + options.OnLivecheckResponse?.Invoke(response); + return response; } } @@ -53,17 +69,5 @@ namespace Alive.Net options?.Invoke(userOptions); app.UseMiddleware(userOptions); } - - /// - /// Automatic livecheck - /// - /// - /// Setup your options - public static void UseHeartBeat(this IApplicationBuilder app, Action options) - { - var userOptions = new AliveOptions(); - options?.Invoke(userOptions); - app.UseMiddleware(userOptions); - } } } \ No newline at end of file diff --git a/src/Alive.Net/AliveOptions.cs b/src/Alive.Net/AliveOptions.cs index 328f30f..acc64be 100644 --- a/src/Alive.Net/AliveOptions.cs +++ b/src/Alive.Net/AliveOptions.cs @@ -1,16 +1,18 @@ -using Microsoft.AspNet.Http; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Threading.Tasks; +using Microsoft.AspNet.Http; namespace Alive.Net { public class AliveOptions { public PathString LivecheckPath { get; set; } = new PathString("/livecheck"); - public HttpStatusCode ReturnStatusCode { get; set; } = HttpStatusCode.OK; + public HttpStatusCode StatusCode { get; set; } = HttpStatusCode.OK; public string BodyText { get; set; } = string.Empty; + + public Action OnLivecheckResponse { get; set; } = null; } } \ No newline at end of file diff --git a/src/Alive.Net/AliveResponse.cs b/src/Alive.Net/AliveResponse.cs new file mode 100644 index 0000000..fdf0ff5 --- /dev/null +++ b/src/Alive.Net/AliveResponse.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Threading.Tasks; + +namespace Alive.Net +{ + public class AliveResponse + { + public string BodyText { get; set; } = String.Empty; + + public HttpStatusCode StatusCode { get; set; } = HttpStatusCode.OK; + } +} \ No newline at end of file diff --git a/src/Alive.Net/Properties/AssemblyInfo.cs b/src/Alive.Net/Properties/AssemblyInfo.cs index ec0ee03..f45c79f 100644 --- a/src/Alive.Net/Properties/AssemblyInfo.cs +++ b/src/Alive.Net/Properties/AssemblyInfo.cs @@ -20,4 +20,4 @@ using System.Runtime.InteropServices; [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("1628a48e-dd1d-45c7-a316-6cfd8498845c")] +[assembly: Guid("1628a48e-dd1d-45c7-a316-6cfd8498845c")] \ No newline at end of file diff --git a/src/Alive.Net/project.json b/src/Alive.Net/project.json index 79e8128..3aec2f7 100644 --- a/src/Alive.Net/project.json +++ b/src/Alive.Net/project.json @@ -1,9 +1,9 @@ { - "version": "1.0.0-*", - "description": "Alive.Net Class Library", + "version": "1.0.1", + "description": "Livecheck middlewear", "authors": [ "Tommy Parnell" ], - "tags": [ "" ], - "projectUrl": "", + "tags": [ "aspcore", "livecheck", "middlewear" ], + "projectUrl": "https://github.com/tparnell8/Alive.Net", "licenseUrl": "", "dependencies": { "Microsoft.AspNet.Http.Abstractions": "1.0.0-rc1-final"