Skip to content

Commit

Permalink
Merge pull request #11 from Wizhi/develop
Browse files Browse the repository at this point in the history
Make Output providers configurable, fixes #10
  • Loading branch information
hmol committed May 3, 2016
2 parents 96ea1f6 + f923041 commit d94a2f7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
15 changes: 11 additions & 4 deletions LinkCrawler/LinkCrawler/App.config
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<configSections>
<section name="outputProviders" type="System.Configuration.DictionarySectionHandler" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<appSettings>
<add key="OnlyReportBrokenLinksToOutput" value="false" />
<add key="CheckImages" value="true" />
<add key="BaseUrl" value="http://www.github.com" />
<add key="SuccessHttpStatusCodes" value="1xx,2xx,302,303" />
<!--explanation of regex below: http://regexr.com/3cqt9 -->
<add key="ValidUrlRegex" value="(^http[s]?:\/{2})|(^www)|(^\/{1,2})" />
<add key="Csv.Enabled" value="false" />
<add key="Csv.FilePath" value="C:\Users\NIH\Desktop\output.csv" />
<add key="Csv.Overwrite" value="true" />
<add key="Csv.Delimiter" value="," />
Expand All @@ -22,4 +24,9 @@ Url: {0}
Statuscode: {1}
The link is placed on this page: {2}" />
</appSettings>
<outputProviders>
<add key="Console" value="LinkCrawler.Utils.Outputs.ConsoleOutput" />
<!--<add key="Csv" value="LinkCrawler.Utils.Outputs.CsvOutput" />-->
<!--<add key="Slack" value="LinkCrawler.Utils.Outputs.SlackOutput" />-->
</outputProviders>
</configuration>
5 changes: 0 additions & 5 deletions LinkCrawler/LinkCrawler/Utils/Outputs/CsvOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public class CsvOutput : IOutput, IDisposable

public CsvOutput(ISettings settings)
{
if (!settings.CsvEnabled)
{
return;
}

_settings = settings;
Setup();
}
Expand Down
2 changes: 1 addition & 1 deletion LinkCrawler/LinkCrawler/Utils/Settings/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public static class AppSettings
public const string SlackWebHookBotName = "Slack.WebHook.Bot.Name";
public const string SlackWebHookBotIconEmoji = "Slack.WebHook.Bot.IconEmoji";
public const string SlackWebHookBotMessageFormat = "Slack.WebHook.Bot.MessageFormat";
public const string CsvEnabled = "Csv.Enabled";
public const string CsvFilePath = "Csv.FilePath";
public const string CsvOverwrite = "Csv.Overwrite";
public const string CsvDelimiter = "Csv.Delimiter";
Expand All @@ -33,3 +32,4 @@ public static class Html
}
}
}

2 changes: 0 additions & 2 deletions LinkCrawler/LinkCrawler/Utils/Settings/ISettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public interface ISettings

string SlackWebHookBotMessageFormat { get; }

bool CsvEnabled { get; }

string CsvFilePath { get; }

bool CsvOverwrite { get; }
Expand Down
3 changes: 0 additions & 3 deletions LinkCrawler/LinkCrawler/Utils/Settings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ public class Settings : ISettings
public string SlackWebHookBotMessageFormat =>
ConfigurationManager.AppSettings[Constants.AppSettings.SlackWebHookBotMessageFormat];

public bool CsvEnabled =>
ConfigurationManager.AppSettings[Constants.AppSettings.CsvEnabled].ToBool();

public string CsvFilePath =>
ConfigurationManager.AppSettings[Constants.AppSettings.CsvFilePath];

Expand Down
41 changes: 39 additions & 2 deletions LinkCrawler/LinkCrawler/Utils/StructureMapRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using LinkCrawler.Utils.Outputs;
using System;
using System.Collections;
using System.Configuration;
using System.Linq;
using LinkCrawler.Utils.Outputs;
using StructureMap.Configuration.DSL;
using StructureMap.Graph;

Expand All @@ -12,8 +16,41 @@ public StructureMapRegistry()
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.AddAllTypesOf<IOutput>();
});

var providers = (ConfigurationManager.GetSection("outputProviders") as Hashtable)?
.Cast<DictionaryEntry>()
.ToDictionary(d => d.Key.ToString(), d => d.Value.ToString());

if (providers != null)
{
var pluginType = typeof(IOutput);

foreach (var provider in providers)
{
var concreteType = Type.GetType(provider.Value);

if (concreteType == null)
{
throw new ConfigurationErrorsException(string.Format(
"Output provider '{0}' not found: {1}",
provider.Key,
provider.Value
));
}

if (!concreteType.GetInterfaces().Contains(pluginType))
{
throw new ConfigurationErrorsException(string.Format(
"Output provider '{0}' does not implement IOutput: {1}",
provider.Key,
provider.Value
));
}

For(pluginType).Add(concreteType).Named(provider.Key);
}
}
}
}
}

0 comments on commit d94a2f7

Please sign in to comment.