init first version

This commit is contained in:
Tommy Parnell
2016-03-16 21:33:52 -04:00
parent ef47df3cfa
commit 16f9fa51ee
15 changed files with 204 additions and 72 deletions

21
LICENSE Normal file
View File

@@ -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.

49
Readme.md Normal file
View File

@@ -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;
}
});
```

View File

@@ -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();

View File

@@ -1,7 +1,7 @@
/// <autosync enabled="true" />
/// <reference path="../gulpfile.js" />
/// <reference path="lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js" />
/// <reference path="lib/jquery-validation/jquery.validate.js" />
/// <reference path="lib/jquery/dist/jquery.js" />
/// <reference path="lib/bootstrap/dist/js/bootstrap.js" />
/// <reference path="js/site.js" />
/// <reference path="lib/bootstrap/dist/js/bootstrap.js" />
/// <reference path="lib/jquery/dist/jquery.js" />
/// <reference path="lib/jquery-validation/dist/jquery.validate.js" />
/// <reference path="lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js" />

View File

@@ -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}}

View File

@@ -4,7 +4,6 @@
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>4ca8001b-27c7-4671-b451-2c2da00a3827</ProjectGuid>
@@ -12,9 +11,11 @@
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@@ -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()
{
}
}
}

View File

@@ -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<ArgumentNullException>(() => new Alive(null, new AliveOptions()));
}
[Fact]
public void ShouldConstructIfOptionsIsNull()
{
var mockedContext = new Mock<HttpContext>();
var al = new Alive((context) => Task.Delay(0), null);
}
[Fact]
public void CalculateResponseThrowsOnNullOptions()
{
Assert.Throws<ArgumentNullException>(() => 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);
}
}
}

View File

@@ -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"
}
}

View File

@@ -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<Alive>(userOptions);
}
/// <summary>
/// Automatic livecheck
/// </summary>
/// <param name="app"></param>
/// <param name="options">Setup your options</param>
public static void UseHeartBeat(this IApplicationBuilder app, Action<AliveOptions> options)
{
var userOptions = new AliveOptions();
options?.Invoke(userOptions);
app.UseMiddleware<Alive>(userOptions);
}
}
}

View File

@@ -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<AliveResponse> OnLivecheckResponse { get; set; } = null;
}
}

View File

@@ -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;
}
}

View File

@@ -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"