2013-08-20 04:31:26 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
|
|
|
using Nancy;
|
|
|
|
using NzbDrone.Api.Frontend.Mappers;
|
|
|
|
|
|
|
|
namespace NzbDrone.Api.Frontend
|
|
|
|
{
|
|
|
|
public class StaticResourceModule : NancyModule
|
|
|
|
{
|
|
|
|
private readonly IEnumerable<IMapHttpRequestsToDisk> _requestMappers;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
2013-08-20 08:23:36 +02:00
|
|
|
public StaticResourceModule(IEnumerable<IMapHttpRequestsToDisk> requestMappers, Logger logger)
|
2013-08-20 04:31:26 +02:00
|
|
|
{
|
|
|
|
_requestMappers = requestMappers;
|
|
|
|
_logger = logger;
|
|
|
|
|
|
|
|
Get["/{resource*}"] = x => Index();
|
2013-08-20 04:39:58 +02:00
|
|
|
Get["/"] = x => Index();
|
2013-08-20 04:31:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private Response Index()
|
|
|
|
{
|
|
|
|
var path = Request.Url.Path;
|
|
|
|
|
|
|
|
if (
|
|
|
|
string.IsNullOrWhiteSpace(path) ||
|
|
|
|
path.StartsWith("/api", StringComparison.CurrentCultureIgnoreCase) ||
|
|
|
|
path.StartsWith("/signalr", StringComparison.CurrentCultureIgnoreCase))
|
|
|
|
{
|
2013-08-26 07:14:55 +02:00
|
|
|
return new NotFoundResponse();
|
2013-08-20 04:31:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var mapper = _requestMappers.SingleOrDefault(m => m.CanHandle(path));
|
|
|
|
|
|
|
|
|
|
|
|
if (mapper != null)
|
|
|
|
{
|
2013-08-20 08:23:36 +02:00
|
|
|
return mapper.GetResponse(path);
|
2013-08-20 04:31:26 +02:00
|
|
|
}
|
|
|
|
|
2013-08-20 08:23:36 +02:00
|
|
|
_logger.Warn("Couldn't find handler for {0}", path);
|
2013-08-20 04:31:26 +02:00
|
|
|
|
2013-08-26 07:14:55 +02:00
|
|
|
return new NotFoundResponse();
|
2013-08-20 04:31:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|