Small tweaks to the web api

This commit is contained in:
runebaas 2018-05-14 00:41:05 +02:00
parent 4a11ce6207
commit 3004b19209
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
5 changed files with 25 additions and 10 deletions

View file

@ -63,7 +63,7 @@ namespace Geekbot.net.Lib
* WebApi * * 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; } public string ApiHost { get; set; }
[Option("api-port", Default = "12995", HelpText = "Port on which the WebApi listens")] [Option("api-port", Default = "12995", HelpText = "Port on which the WebApi listens")]

View file

@ -19,6 +19,7 @@ using Geekbot.net.Lib.Logger;
using Geekbot.net.Lib.Media; using Geekbot.net.Lib.Media;
using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.ReactionListener;
using Geekbot.net.Lib.UserRepository; using Geekbot.net.Lib.UserRepository;
using Geekbot.net.WebApi;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Nancy.Hosting.Self; using Nancy.Hosting.Self;
using StackExchange.Redis; using StackExchange.Redis;
@ -202,7 +203,8 @@ namespace Geekbot.net
{ {
_logger.Information(LogSource.Api, "Starting Webserver"); _logger.Information(LogSource.Api, "Starting Webserver");
var webApiUrl = new Uri($"http://{_runParameters.ApiHost}:{_runParameters.ApiPort}"); 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}"); _logger.Information(LogSource.Api, $"Webserver now running on {webApiUrl}");
} }
} }

View file

@ -2,19 +2,16 @@
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.Commands; using Discord.Commands;
using Geekbot.net.Lib;
using Nancy; using Nancy;
namespace Geekbot.net.WebApi.Help namespace Geekbot.net.WebApi.Help
{ {
public class HelpController : NancyModule public sealed class HelpController : NancyModule
{ {
public HelpController() public HelpController(CommandService commands)
{ {
Get("/v1/commands", args => Get("/v1/commands", args =>
{ {
var commands = GetCommands().Result;
var commandList = (from cmd in commands.Commands var commandList = (from cmd in commands.Commands
let cmdParamsObj = cmd.Parameters.Select(cmdParam => new CommandParamDto let cmdParamsObj = cmd.Parameters.Select(cmdParam => new CommandParamDto
{ {

View file

@ -3,7 +3,7 @@ using Nancy;
namespace Geekbot.net.WebApi.Status namespace Geekbot.net.WebApi.Status
{ {
public class StatusController : NancyModule public sealed class StatusController : NancyModule
{ {
public StatusController() public StatusController()
{ {

View file

@ -1,4 +1,6 @@
using System.Diagnostics; using System.Diagnostics;
using Discord.Commands;
using Geekbot.net.Lib.Logger;
using Nancy; using Nancy;
using Nancy.Bootstrapper; using Nancy.Bootstrapper;
using Nancy.TinyIoc; using Nancy.TinyIoc;
@ -7,12 +9,26 @@ namespace Geekbot.net.WebApi
{ {
public class WebConfig : DefaultNancyBootstrapper 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) protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
{ {
// Register Dependencies
//CORS Enable container.Register<IGeekbotLogger>(_logger);
container.Register<CommandService>(_commands);
// Enable CORS
pipelines.AfterRequest.AddItemToEndOfPipeline(ctx => pipelines.AfterRequest.AddItemToEndOfPipeline(ctx =>
{ {
_logger.Information(LogSource.Api, ctx.Request.Path.ToString());
ctx.Response.WithHeader("Access-Control-Allow-Origin", "*") ctx.Response.WithHeader("Access-Control-Allow-Origin", "*")
.WithHeader("Access-Control-Allow-Methods", "GET") .WithHeader("Access-Control-Allow-Methods", "GET")
.WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type") .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type")