-
Notifications
You must be signed in to change notification settings - Fork 10
/
Function.cs
71 lines (67 loc) Β· 2.9 KB
/
Function.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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;
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace Trapdoor
{
public class Function
{
private readonly IMemoryCache memoryCache;
private List<ISender> _alerts;
private readonly Storage<SessionLog> _storage;
private Config config;
public Function()
{
memoryCache = new MemoryCache(new MemoryCacheOptions());
_storage = new Storage<SessionLog>(new AmazonDynamoDBClient());
}
public async Task<APIGatewayProxyResponse> 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<string, string> { { "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<ISender>();
config = JsonConvert.DeserializeObject<Config>(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.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p) && !p.IsInterface && !p.IsAbstract);
types.ToList().ForEach(type => {
ConstructorInfo ctor = type.GetConstructor(new[] { typeof(Storage<SessionLog>), typeof(Config), typeof(IMemoryCache) });
ISender instance = ctor.Invoke(new object[] { _storage, config, memoryCache }) as ISender;
_alerts.Add(instance);
});
}
}
}