Add CORS support to caching server, add heartbeat endpoint

This commit is contained in:
Timothy Baldridge 2019-12-24 08:01:06 -07:00
parent 271fe56f6b
commit 32b1a27e34
4 changed files with 46 additions and 4 deletions

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nancy;
namespace Wabbajack.CacheServer
{
public class Heartbeat : NancyModule
{
private static DateTime startTime = DateTime.Now;
public Heartbeat() : base("/")
{
Get("/heartbeat", HandleHeartbeat);
}
private object HandleHeartbeat(object arg)
{
return $"Service is live for: {DateTime.Now - startTime}";
}
}
}

View File

@ -15,7 +15,7 @@ namespace Wabbajack.CacheServer
Utils.LogMessages.Subscribe(Console.WriteLine);
using (var server = new Server("http://localhost:8080"))
{
ListValidationService.Start();
//ListValidationService.Start();
server.Start();
Console.ReadLine();
}

View File

@ -5,7 +5,10 @@ using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Nancy;
using Nancy.Bootstrapper;
using Nancy.Hosting.Self;
using Nancy.TinyIoc;
namespace Wabbajack.CacheServer
{
@ -17,11 +20,12 @@ namespace Wabbajack.CacheServer
public Server(string address)
{
Address = address;
_config = new HostConfiguration();
_config.MaximumConnectionCount = 24;
_config = new HostConfiguration {MaximumConnectionCount = 24, RewriteLocalhost = true};
//_config.UrlReservations.CreateAutomatically = true;
_config.RewriteLocalhost = true;
_server = new NancyHost(_config, new Uri(address));
}
public string Address { get; }
@ -36,4 +40,17 @@ namespace Wabbajack.CacheServer
_server?.Dispose();
}
}
public class CachingBootstrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
pipelines.AfterRequest.AddItemToEndOfPipeline(ctx =>
{
ctx.Response.WithHeader("Access-Control-Allow-Origin", "*")
.WithHeader("Access-Control-Allow-Methods", "POST, GET")
.WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type");
});
}
}
}

View File

@ -79,6 +79,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Server.cs" />
<Compile Include="Heartbeat.cs" />
<Compile Include="TestingEndpoints.cs" />
</ItemGroup>
<ItemGroup>