diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs index 7942b76..7f2dd3d 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/Geekbot.net/Lib/RunParameters.cs @@ -63,7 +63,7 @@ namespace Geekbot.net.Lib * WebApi * ************************************/ - [Option("api-host", Default = "127.0.0.1", HelpText = "Host on which the WebApi listens")] + [Option("api-host", Default = "localhost", HelpText = "Host on which the WebApi listens")] public string ApiHost { get; set; } [Option("api-port", Default = "12995", HelpText = "Port on which the WebApi listens")] diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 8084127..e5b3f59 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -19,6 +19,7 @@ using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.Media; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; +using Geekbot.net.WebApi; using Microsoft.Extensions.DependencyInjection; using Nancy.Hosting.Self; using StackExchange.Redis; @@ -202,7 +203,8 @@ namespace Geekbot.net { _logger.Information(LogSource.Api, "Starting Webserver"); var webApiUrl = new Uri($"http://{_runParameters.ApiHost}:{_runParameters.ApiPort}"); - new NancyHost(webApiUrl).Start(); + var webConfig = new WebConfig(_logger, _commands); + new NancyHost(webConfig, webApiUrl).Start(); _logger.Information(LogSource.Api, $"Webserver now running on {webApiUrl}"); } } diff --git a/Geekbot.net/WebApi/Help/HelpController.cs b/Geekbot.net/WebApi/Help/HelpController.cs index 015c7d8..8aaed12 100644 --- a/Geekbot.net/WebApi/Help/HelpController.cs +++ b/Geekbot.net/WebApi/Help/HelpController.cs @@ -2,19 +2,16 @@ using System.Reflection; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib; using Nancy; namespace Geekbot.net.WebApi.Help { - public class HelpController : NancyModule + public sealed class HelpController : NancyModule { - public HelpController() + public HelpController(CommandService commands) { Get("/v1/commands", args => { - var commands = GetCommands().Result; - var commandList = (from cmd in commands.Commands let cmdParamsObj = cmd.Parameters.Select(cmdParam => new CommandParamDto { diff --git a/Geekbot.net/WebApi/Status/StatusController.cs b/Geekbot.net/WebApi/Status/StatusController.cs index 05196a2..a2c692c 100644 --- a/Geekbot.net/WebApi/Status/StatusController.cs +++ b/Geekbot.net/WebApi/Status/StatusController.cs @@ -3,7 +3,7 @@ using Nancy; namespace Geekbot.net.WebApi.Status { - public class StatusController : NancyModule + public sealed class StatusController : NancyModule { public StatusController() { diff --git a/Geekbot.net/WebApi/WebConfig.cs b/Geekbot.net/WebApi/WebConfig.cs index f0b9ca3..6c5a70c 100644 --- a/Geekbot.net/WebApi/WebConfig.cs +++ b/Geekbot.net/WebApi/WebConfig.cs @@ -1,4 +1,6 @@ using System.Diagnostics; +using Discord.Commands; +using Geekbot.net.Lib.Logger; using Nancy; using Nancy.Bootstrapper; using Nancy.TinyIoc; @@ -7,12 +9,26 @@ namespace Geekbot.net.WebApi { public class WebConfig : DefaultNancyBootstrapper { + private readonly GeekbotLogger _logger; + private readonly CommandService _commands; + + public WebConfig(GeekbotLogger logger, CommandService commands) + { + _logger = logger; + _commands = commands; + } + protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context) { - - //CORS Enable + // Register Dependencies + container.Register(_logger); + container.Register(_commands); + + // Enable CORS pipelines.AfterRequest.AddItemToEndOfPipeline(ctx => { + _logger.Information(LogSource.Api, ctx.Request.Path.ToString()); + ctx.Response.WithHeader("Access-Control-Allow-Origin", "*") .WithHeader("Access-Control-Allow-Methods", "GET") .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type")