Trapdoor
Function.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Amazon.DynamoDBv2;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using System.Text.Json;
[astembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace Trapdoor
{
public clast Function
{
private readonly IMemoryCache memoryCache;
private List _alerts;
private readonly Storage _storage;
private Config config;
public Function()
{
memoryCache = new MemoryCache(new MemoryCacheOptions());
_storage = new Storage(new AmazonDynamoDBClient());
}
public async Task FunctionHandler(APIGatewayProxyRequest request)
{
initConfig();
var sender = new Handler(config, memoryCache, _alerts);
var guid = Guid.NewGuid().ToString();
if (await sender.SendAlerts(request, guid))
return new APIGatewayProxyResponse
{
StatusCode = 200,
Headers = new Dictionary { { "Content-type", "text/html" } },
Body = (await File.ReadAllTextAsync("handle.html")).Replace("{REQUEST_ID}", guid)
};
return new APIGatewayProxyResponse()
{
StatusCode = 200
};
}
private void initConfig()
{
_alerts = new List();
config = JsonConvert.DeserializeObject(File.ReadAllText("config.json"));
if (string.IsNullOrEmpty(config.SlackPath))
config.SlackPath = Environment.GetEnvironmentVariable("SLACKPATH");
if (string.IsNullOrEmpty(config.WebhookChannel))
config.WebhookChannel = Environment.GetEnvironmentVariable("WEBHOOKCHANNEL");
if (string.IsNullOrEmpty(config.WebHookToken))
config.WebHookToken = Environment.GetEnvironmentVariable("WEBHOOKTOKEN");
if (string.IsNullOrEmpty(config.PostUrl))
config.PostUrl = Environment.GetEnvironmentVariable("POSTURL");
var type = typeof(ISender);
var types = AppDomain.CurrentDomain.Getastemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsastignableFrom(p) && !p.IsInterface && !p.IsAbstract);
types.ToList().ForEach(type => {
ConstructorInfo ctor = type.GetConstructor(new[] { typeof(Storage), typeof(Config), typeof(IMemoryCache) });
ISender instance = ctor.Invoke(new object[] { _storage, config, memoryCache }) as ISender;
_alerts.Add(instance);
});
}
}
}