-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
121 lines (107 loc) · 3.78 KB
/
Program.cs
File metadata and controls
121 lines (107 loc) · 3.78 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using GithubSponsorsWebhook.Database;
using GithubSponsorsWebhook.Services;
using GithubSponsorsWebhook.Jobs;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Quartz;
using Microsoft.AspNetCore.HttpOverrides;
using github_sponsors_webhook.Database;
using GithubSponsorsWebhook;
using GithubSponsorsWebhook.Database.Models;
//Cache it.
Configuration.GetConfiguration();
var builder = WebApplication.CreateBuilder(args);
#region Build Configuration
// Add services to the container.
// add LiteDb context to the container
builder.Services.Configure<LiteDbOptions>(builder.Configuration.GetSection(LiteDbOptions.LiteDb));
builder.Services.AddSingleton<ILiteDbContext, LiteDbContext>();
builder.Services.AddTransient<ILiteDbSponsorService, LiteDbSponsorService>();
builder.Services.AddScoped<ISponsorshipService, SponsorshipService>();
builder.Services.AddScoped<IGitHubService, GitHubService>();
builder.Services.AddScoped<IGitHubPaymentService, GitHubPaymentService>();
builder.Services.AddHttpClient<HttpClientGitHubGraphQLService>();
builder.Services.AddControllers();
#region CronJob
builder.Services.AddQuartz(q =>
{
q.SchedulerId = "Scheduler-Core";
q.SchedulerName = "Quartz Scheduler";
q.UseMicrosoftDependencyInjectionJobFactory();
q.UseSimpleTypeLoader();
q.UseDefaultThreadPool(tp =>
{
tp.MaxConcurrency = 3;
});
q.UseInMemoryStore();
q.UseTimeZoneConverter();
if (builder.Environment.IsProduction())
q.ScheduleJob<SponsoringJob>((trigger) => trigger
.WithIdentity("GithubSponsorsWebhook")
.WithCronSchedule("0 0 0/4 * * ?")
.StartNow());
else
q.ScheduleJob<SponsoringJob>((trigger) => trigger
.WithIdentity("GithubSponsorsWebhook")
.WithCronSchedule("0 0/1 * * * ?")
.StartNow());
});
builder.Services.AddScoped<SponsoringJob>();
// if you are using persistent job store, you might want to alter some options
builder.Services.Configure<QuartzOptions>(options =>
{
options.Scheduling.IgnoreDuplicates = true; // default: false
options.Scheduling.OverWriteExistingData = true; // default: true
});
builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
#endregion CronJob
if (builder.Environment.IsDevelopment())
{
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
}
#endregion Build Configuration
#region App Configuration
var app = builder.Build();
// Add Swagger if in development
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseHttpsRedirection();
}
else
{
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
}
app.UseCors(CorsPolicyBuilder =>
{
// we will handle that in nginx proxy
CorsPolicyBuilder.AllowAnyOrigin();
CorsPolicyBuilder.WithHeaders("Content-Type", "X-Hub-Signature-256", "X-GitHub-Event", "X-GitHub-Delivery");
CorsPolicyBuilder.WithMethods("POST", "OPTIONS", "GET");
});
app.UseHttpLogging();
app.MapControllers();
#endregion App Configuration
// One time job for newAccountIncentive
var dbService = app.Services.GetService<ILiteDbSponsorService>();
var allSpoonsors = dbService.FindAll();
foreach (var sponsor in allSpoonsors)
{
var incentive = Configuration.GetConfiguration().NewAccountIncentiveInCent;
sponsor.TotalSpendInCent += incentive;
sponsor.Payments ??= new List<OneTimePayment>();
sponsor.Payments.Add(new OneTimePayment
{
TotalInCent = (int)incentive,
CreatedAt = DateTime.Now,
Description = "New Account Incentive"
});
}
dbService.UpdateSponsors(allSpoonsors);
// end of one time job
app.Run();