/
Program.cs
132 lines (112 loc) · 4.04 KB
/
Program.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System.Text.Json.Serialization;
using Helpdesk.Api;
using Helpdesk.Api.Core.Http.Middlewares.ExceptionHandling;
using Helpdesk.Api.Core.Kafka;
using Helpdesk.Api.Core.SignalR;
using Helpdesk.Api.Incidents;
using Helpdesk.Api.Incidents.GettingCustomerIncidents;
using Helpdesk.Api.Incidents.GettingCustomerIncidentsSummary;
using Helpdesk.Api.Incidents.GettingDetails;
using Helpdesk.Api.Incidents.GettingHistory;
using Helpdesk.Api.Incidents.ResolutionBatch;
using JasperFx.CodeGeneration;
using Marten;
using Marten.Events.Daemon.Resiliency;
using Marten.Events.Projections;
using Marten.Exceptions;
using Marten.Services.Json;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.AspNetCore.SignalR;
using Oakton;
using Oakton.Resources;
using Weasel.Core;
using Wolverine;
using Wolverine.Http;
using Wolverine.Marten;
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddDefaultExceptionHandler(
(exception, _) => exception switch
{
ConcurrencyException => exception.MapToProblemDetails(StatusCodes.Status412PreconditionFailed),
_ => null
})
.AddEndpointsApiExplorer()
.AddSwaggerGen()
.AddMarten(sp =>
{
var options = new StoreOptions();
var schemaName = Environment.GetEnvironmentVariable("SchemaName") ?? "WolverineHelpdesk";
options.Events.DatabaseSchemaName = schemaName;
options.DatabaseSchemaName = schemaName;
options.Connection(builder.Configuration.GetConnectionString("WolverineIncidents") ??
throw new InvalidOperationException());
options.UseSystemTextJsonForSerialization(
EnumStorage.AsString,
casing: Casing.CamelCase
);
options.Projections.Errors.SkipApplyErrors = false;
options.Projections.Errors.SkipSerializationErrors = false;
options.Projections.Errors.SkipUnknownEvents = false;
options.Projections.RebuildErrors.SkipApplyErrors = false;
options.Projections.RebuildErrors.SkipSerializationErrors = false;
options.Projections.RebuildErrors.SkipUnknownEvents = false;
options.Projections.Add(new KafkaProducer(builder.Configuration), ProjectionLifecycle.Async);
options.Projections.Add(
new SignalRProducer((IHubContext)sp.GetRequiredService<IHubContext<IncidentsHub>>()),
ProjectionLifecycle.Async
);
options.ConfigureIncidents();
options.DisableNpgsqlLogging = true;
return options;
})
.OptimizeArtifactWorkflow(TypeLoadMode.Static)
.UseLightweightSessions()
.AddAsyncDaemon(DaemonMode.Solo)
// Add Marten/PostgreSQL integration with Wolverine's outbox
.IntegrateWithWolverine()
// I also added this to opt into events being forward to
// the Wolverine outbox during SaveChangesAsync()
.EventForwardingToWolverine();
builder.Services.AddResourceSetupOnStartup();
builder.Services
.AddCors(options =>
options.AddPolicy("ClientPermission", policy =>
policy.WithOrigins("http://localhost:3000")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
)
)
.Configure<JsonOptions>(o => o.SerializerOptions.Converters.Add(new JsonStringEnumConverter()))
.Configure<Microsoft.AspNetCore.Mvc.JsonOptions>(o =>
o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()))
.AddSignalR();
builder.Host.ApplyOaktonExtensions();
// Configure Wolverine
builder.Host.UseWolverine(opts =>
{
opts.ConfigureIncidents();
opts.Policies.AutoApplyTransactions();
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger()
.UseSwaggerUI();
}
app.UseExceptionHandler()
.UseCors("ClientPermission");
app.MapHub<IncidentsHub>("/hubs/incidents");
// Let's add in Wolverine HTTP endpoints to the routing tree
app.MapWolverineEndpoints();
return await app.RunOaktonCommands(args);
namespace Helpdesk.Api
{
public class IncidentsHub: Hub
{
}
public partial class Program
{
}
}