ln.skyscanner/http/SkyScannerHttpApplication.cs

76 lines
3.0 KiB
C#

using System;
using ln.http.resources;
using System.IO;
using ln.skyscanner.entities;
using ln.skyscanner.checks;
using ln.types.net;
using System.Linq;
using ln.skyscanner.perfvalue;
namespace ln.skyscanner.http
{
public class SkyScannerHttpApplication : ResourceApplication
{
public SkyScanner SkyScanner { get; }
public String BasePath { get; private set; }
public String TemplatesBasePath { get; private set; }
ReflectiveResource refChecker;
ReflectiveResource refEntities;
public SkyScannerHttpApplication(SkyScanner skyScanner)
{
SkyScanner = skyScanner;
BasePath = Path.GetFullPath(".");
TemplatesBasePath = Path.Combine(BasePath, "templates", "static");
/* Development hint */
if (Directory.Exists("../../templates/static"))
TemplatesBasePath = Path.Combine(BasePath, "..", "..", "templates", "static");
DirectoryResource staticTemplates = new DirectoryResource(RootResource, TemplatesBasePath);
staticTemplates.ResourceTypeHook = ResourceTypeHook;
staticTemplates.DefaultResource = staticTemplates.GetResource("index.html");
RootResource.DefaultResource = staticTemplates;
refEntities = new ReflectiveResource(RootResource, "entities", SkyScanner.Entities);
refChecker = new ReflectiveResource(RootResource, "checker", SkyScanner.Checker);
SkyScannerHttpApi api = new SkyScannerHttpApi(this);
BaseResource collections = new BaseResource(RootResource, "collections");
new CollectionResource<Node>(collections, skyScanner.Entities.NodeCollection);
new CollectionResource<Subnet>(collections, skyScanner.Entities.SubnetCollection);
new CollectionResource<PointOfPresence>(collections, skyScanner.Entities.PointOfPresenceCollection);
new CollectionResource<CrawledHost>(collections, skyScanner.Entities.CrawledHosts);
new CollectionResource<CrawledSubnet>(collections, skyScanner.Entities.CrawledSubnets);
new CollectionResource<SkyCheckState>(collections, skyScanner.Entities.SkyCheckStates);
new CollectionResource<Network4>(collections, skyScanner.Entities.BlockedNetworks);
ObjectContainerResource<PerformanceValue> resPerformanceValues = new ObjectContainerResource<PerformanceValue>(collections);
resPerformanceValues.GetResourceName = (pv) => pv.PerfName;
resPerformanceValues.Enumeration = () => skyScanner.Entities.SkyCheckStates.SelectMany((checkState) => checkState.PerformanceValues);
resPerformanceValues.Lookup = skyScanner.Entities.LookupPerformanceValue;
}
private Resource ResourceTypeHook(DirectoryResource directoryResource, FileInfo fileInfo)
{
if (fileInfo.Extension.Equals(".html"))
return new TemplateResource(directoryResource, fileInfo.FullName);
return null;
}
}
}