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

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>
</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

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

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"