Skip to content

Commit 5768cc0

Browse files
authored
Merge pull request #1 from Skyfall1235/db-addition
Add WebhookService and DatabaseService classes
2 parents 2e899e5 + c16c729 commit 5768cc0

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

src/RandomAPI/Program.cs

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,65 @@
2626

2727

2828
//TODO:
29+
// - good logging service. rabapp has an event table, i bet i could do something worse
30+
31+
// - add a db. sqlite will probably work here since its small
32+
//how tf will i get a db in here
33+
2934
// - AlertGatewayService
3035
// API Endpoint Goal: POST / alert / ingest
3136
// Brief Description(Project Scope): Centralized Notification Hub with Discord Integration.
3237
// Receives generic webhooks (from Sentry, CI/CD, etc.), standardizes the payload, applies personalized filtering rules, and routes the cleaned alert to your Discord channel using a webhook.
3338
// The service will format the alert into a visually appealing Discord Embed using a library like discord-webhook or requests.
34-
public class WebhookService { }
39+
public class WebhookPayload
40+
{
41+
//payload, timestamp, event enum probably
42+
}
43+
public interface IWebhookService
44+
{
45+
Task SendWebhookAsync(object payload);
46+
void AddListener(string url);
47+
IEnumerable<string> GetListeners();
48+
}
49+
50+
public class WebhookService : IWebhookService
51+
{
52+
private readonly HttpClient _httpClient;
53+
private readonly ConcurrentBag<string> _listeners = new ConcurrentBag<string>();
54+
55+
public WebhookService(HttpClient httpClient)
56+
{
57+
_httpClient = httpClient;
58+
}
59+
60+
public void AddListener(string url)
61+
{
62+
// validate url here
63+
_listeners.Add(url);
64+
}
65+
66+
public IEnumerable<string> GetListeners() => _listeners;
67+
68+
public async Task SendWebhookAsync(WebhookPayload payload)
69+
{
70+
string json = JsonConvert.SerializeObject(payload);
71+
var content = new StringContent(json, Encoding.UTF8, "application/json");
72+
//this can be wayyyyy better
73+
foreach (var url in _listeners)
74+
{
75+
try
76+
{
77+
var response = await _httpClient.PostAsync(url, content);
78+
Console.WriteLine($"Sent to {url}: {response.StatusCode}");
79+
}
80+
catch (Exception ex)
81+
{
82+
Console.WriteLine($"Error sending to {url}: {ex.Message}");
83+
}
84+
}
85+
}
86+
}
87+
3588

3689
// - HealthCheckAggregator
3790
// API Endpoint Goal: GET / health / summary
@@ -47,3 +100,26 @@ public class HealthCheckService { }
47100
// Supports robust searching by language (python, sql) and customizable tags (regex, lambda, auth).
48101
public class UniversalSnippetService { }
49102
// will require a db
103+
104+
105+
// - basic sqlite db
106+
public class DatabaseService
107+
{
108+
private readonly string _connectionString;
109+
110+
public DatabaseService(string dbPath = "Data/myapp.db")
111+
{
112+
//WILL NEED TO BE UPDATED
113+
_connectionString = $"Data Source={dbPath}";
114+
}
115+
116+
/// <summary>
117+
/// Opens a connection to SQLite, executes an action, then closes the connection.
118+
/// </summary>
119+
public void Execute(Action<SqliteConnection> action)
120+
{
121+
using var connection = new SqliteConnection(_connectionString);
122+
connection.Open();
123+
action(connection);
124+
}
125+
}

0 commit comments

Comments
 (0)