Replace nancy with kestrel
This commit is contained in:
parent
3004b19209
commit
8c107de92e
13 changed files with 222 additions and 117 deletions
|
@ -1,7 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Geekbot.net.WebApi.Help
|
||||
namespace Geekbot.net.WebApi.Controllers.Commands
|
||||
{
|
||||
public class CommandDto
|
||||
{
|
|
@ -1,4 +1,4 @@
|
|||
namespace Geekbot.net.WebApi.Help
|
||||
namespace Geekbot.net.WebApi.Controllers.Commands
|
||||
{
|
||||
public class CommandParamDto
|
||||
{
|
41
Geekbot.net/WebApi/Controllers/Commands/HelpController.cs
Normal file
41
Geekbot.net/WebApi/Controllers/Commands/HelpController.cs
Normal file
|
@ -0,0 +1,41 @@
|
|||
using System.Linq;
|
||||
using Discord.Commands;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Geekbot.net.WebApi.Controllers.Commands
|
||||
{
|
||||
[EnableCors("AllowSpecificOrigin")]
|
||||
public class HelpController : Controller
|
||||
{
|
||||
private readonly CommandService _commands;
|
||||
|
||||
public HelpController(CommandService commands)
|
||||
{
|
||||
_commands = commands;
|
||||
}
|
||||
|
||||
[Route("/v1/commands")]
|
||||
public IActionResult GetCommands()
|
||||
{
|
||||
var commandList = (from cmd in _commands.Commands
|
||||
let cmdParamsObj = cmd.Parameters.Select(cmdParam => new CommandParamDto
|
||||
{
|
||||
Summary = cmdParam.Summary,
|
||||
Default = cmdParam.DefaultValue?.ToString() ?? null,
|
||||
Type = cmdParam.Type?.ToString()
|
||||
})
|
||||
.ToList()
|
||||
let param = string.Join(", !", cmd.Aliases)
|
||||
select new CommandDto
|
||||
{
|
||||
Name = cmd.Name,
|
||||
Summary = cmd.Summary,
|
||||
IsAdminCommand = (param.Contains("admin")),
|
||||
Aliases = cmd.Aliases.ToArray(),
|
||||
Params = cmdParamsObj
|
||||
}).ToList();
|
||||
return Ok(commandList);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
namespace Geekbot.net.WebApi.Status
|
||||
namespace Geekbot.net.WebApi.Controllers.Status
|
||||
{
|
||||
public class ApiStatusDto
|
||||
{
|
22
Geekbot.net/WebApi/Controllers/Status/StatusController.cs
Normal file
22
Geekbot.net/WebApi/Controllers/Status/StatusController.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
using Geekbot.net.Lib;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Geekbot.net.WebApi.Controllers.Status
|
||||
{
|
||||
[EnableCors("AllowSpecificOrigin")]
|
||||
public class StatusController : Controller
|
||||
{
|
||||
[Route("/")]
|
||||
public IActionResult GetCommands()
|
||||
{
|
||||
var responseBody = new ApiStatusDto
|
||||
{
|
||||
GeekbotVersion = Constants.BotVersion(),
|
||||
ApiVersion = Constants.ApiVersion.ToString(),
|
||||
Status = "Online"
|
||||
};
|
||||
return Ok(responseBody);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.Commands;
|
||||
using Nancy;
|
||||
|
||||
namespace Geekbot.net.WebApi.Help
|
||||
{
|
||||
public sealed class HelpController : NancyModule
|
||||
{
|
||||
public HelpController(CommandService commands)
|
||||
{
|
||||
Get("/v1/commands", args =>
|
||||
{
|
||||
var commandList = (from cmd in commands.Commands
|
||||
let cmdParamsObj = cmd.Parameters.Select(cmdParam => new CommandParamDto
|
||||
{
|
||||
Summary = cmdParam.Summary,
|
||||
Default = cmdParam.DefaultValue?.ToString() ?? null,
|
||||
Type = cmdParam.Type?.ToString()
|
||||
})
|
||||
.ToList()
|
||||
let param = string.Join(", !", cmd.Aliases)
|
||||
select new CommandDto
|
||||
{
|
||||
Name = cmd.Name,
|
||||
Summary = cmd.Summary,
|
||||
IsAdminCommand = (param.Contains("admin")),
|
||||
Aliases = cmd.Aliases.ToArray(),
|
||||
Params = cmdParamsObj
|
||||
}).ToList();
|
||||
return Response.AsJson(commandList);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
private async Task<CommandService> GetCommands()
|
||||
{
|
||||
var commands = new CommandService();
|
||||
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
|
||||
return commands;
|
||||
}
|
||||
}
|
||||
}
|
29
Geekbot.net/WebApi/Logging/AspLogProvider.cs
Normal file
29
Geekbot.net/WebApi/Logging/AspLogProvider.cs
Normal file
|
@ -0,0 +1,29 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using Geekbot.net.Lib.Logger;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Geekbot.net.WebApi.Logging
|
||||
{
|
||||
public class AspLogProvider : ILoggerProvider
|
||||
{
|
||||
private readonly IGeekbotLogger _geekbotLogger;
|
||||
|
||||
private readonly ConcurrentDictionary<string, AspLogger> _loggers = new ConcurrentDictionary<string, AspLogger>();
|
||||
|
||||
public AspLogProvider(IGeekbotLogger geekbotLogger)
|
||||
{
|
||||
_geekbotLogger = geekbotLogger;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_loggers.Clear();
|
||||
}
|
||||
|
||||
public ILogger CreateLogger(string categoryName)
|
||||
{
|
||||
return _loggers.GetOrAdd(categoryName, name => new AspLogger(categoryName, _geekbotLogger));
|
||||
}
|
||||
}
|
||||
}
|
75
Geekbot.net/WebApi/Logging/AspLogger.cs
Normal file
75
Geekbot.net/WebApi/Logging/AspLogger.cs
Normal file
|
@ -0,0 +1,75 @@
|
|||
using System;
|
||||
using Geekbot.net.Lib.Logger;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Geekbot.net.WebApi.Logging
|
||||
{
|
||||
public class AspLogger : ILogger
|
||||
{
|
||||
private readonly string _categoryName;
|
||||
private readonly IGeekbotLogger _geekbotLogger;
|
||||
|
||||
public AspLogger(string categoryName, IGeekbotLogger geekbotLogger)
|
||||
{
|
||||
geekbotLogger.Trace(LogSource.Api, $"Adding {categoryName}");
|
||||
_categoryName = categoryName;
|
||||
_geekbotLogger = geekbotLogger;
|
||||
}
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
||||
{
|
||||
switch (logLevel)
|
||||
{
|
||||
case LogLevel.Trace:
|
||||
_geekbotLogger.Trace(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}");
|
||||
break;
|
||||
case LogLevel.Debug:
|
||||
_geekbotLogger.Debug(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}");
|
||||
break;
|
||||
case LogLevel.Information:
|
||||
_geekbotLogger.Information(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}");
|
||||
break;
|
||||
case LogLevel.Warning:
|
||||
_geekbotLogger.Warning(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}", exception);
|
||||
break;
|
||||
case LogLevel.Error:
|
||||
case LogLevel.Critical:
|
||||
_geekbotLogger.Error(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}", exception);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(logLevel));
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel)
|
||||
{
|
||||
return !_geekbotLogger.LogAsJson() && _geekbotLogger.GetNLogger().IsEnabled(ToGeekbotLogLevel(logLevel));
|
||||
}
|
||||
|
||||
public IDisposable BeginScope<TState>(TState state)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
private static NLog.LogLevel ToGeekbotLogLevel(LogLevel level)
|
||||
{
|
||||
switch (level)
|
||||
{
|
||||
case LogLevel.Trace:
|
||||
return NLog.LogLevel.Trace;
|
||||
case LogLevel.Debug:
|
||||
return NLog.LogLevel.Debug;
|
||||
case LogLevel.Information:
|
||||
return NLog.LogLevel.Info;
|
||||
case LogLevel.Warning:
|
||||
return NLog.LogLevel.Warn;
|
||||
case LogLevel.Error:
|
||||
return NLog.LogLevel.Error;
|
||||
case LogLevel.Critical:
|
||||
return NLog.LogLevel.Fatal;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(level));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
using Geekbot.net.Lib;
|
||||
using Nancy;
|
||||
|
||||
namespace Geekbot.net.WebApi.Status
|
||||
{
|
||||
public sealed class StatusController : NancyModule
|
||||
{
|
||||
public StatusController()
|
||||
{
|
||||
Get("/", args =>
|
||||
{
|
||||
var responseBody = new ApiStatusDto
|
||||
{
|
||||
GeekbotVersion = Constants.BotVersion(),
|
||||
ApiVersion = Constants.ApiVersion.ToString(),
|
||||
Status = "Online"
|
||||
};
|
||||
return Response.AsJson(responseBody);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
50
Geekbot.net/WebApi/WebApiStartup.cs
Normal file
50
Geekbot.net/WebApi/WebApiStartup.cs
Normal file
|
@ -0,0 +1,50 @@
|
|||
using System;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib;
|
||||
using Geekbot.net.Lib.Logger;
|
||||
using Geekbot.net.WebApi.Logging;
|
||||
using Microsoft.AspNetCore;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Geekbot.net.WebApi
|
||||
{
|
||||
public class WebApiStartup
|
||||
{
|
||||
public static void StartWebApi(IGeekbotLogger logger, RunParameters runParameters, CommandService commandService)
|
||||
{
|
||||
WebHost.CreateDefaultBuilder()
|
||||
.UseKestrel(options =>
|
||||
{
|
||||
options.Listen(IPAddress.Any, int.Parse(runParameters.ApiPort));
|
||||
})
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
services.AddMvc();
|
||||
services.AddSingleton<CommandService>(commandService);
|
||||
services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowSpecificOrigin",
|
||||
builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
|
||||
});
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseMvc();
|
||||
app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().Build());
|
||||
})
|
||||
.ConfigureLogging(logging =>
|
||||
{
|
||||
logging.ClearProviders();
|
||||
logging.SetMinimumLevel(LogLevel.Debug);
|
||||
logging.AddProvider(new AspLogProvider(logger));
|
||||
})
|
||||
.UseSetting(WebHostDefaults.ApplicationKey, typeof(Program).GetTypeInfo().Assembly.FullName)
|
||||
.Build().Run();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
using System.Diagnostics;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib.Logger;
|
||||
using Nancy;
|
||||
using Nancy.Bootstrapper;
|
||||
using Nancy.TinyIoc;
|
||||
|
||||
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)
|
||||
{
|
||||
// Register Dependencies
|
||||
container.Register<IGeekbotLogger>(_logger);
|
||||
container.Register<CommandService>(_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")
|
||||
.WithHeader("Last-Modified", Process.GetCurrentProcess().StartTime.ToString());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue