forked from NpgsqlRest/NpgsqlRest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlRestMiddlewareExtensions.cs
More file actions
85 lines (74 loc) · 3.88 KB
/
Copy pathNpgsqlRestMiddlewareExtensions.cs
File metadata and controls
85 lines (74 loc) · 3.88 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
using System.Net;
using static System.Net.Mime.MediaTypeNames;
namespace NpgsqlRest;
public static class NpgsqlRestMiddlewareExtensions
{
public static IApplicationBuilder UseNpgsqlRest(this IApplicationBuilder builder, NpgsqlRestOptions options)
{
if (options.ConnectionString is null && options.DataSource is null && options.ServiceProviderMode == ServiceProviderObject.None)
{
throw new ArgumentException("ConnectionString and DataSource are null and ServiceProviderMode is set to None. You must specify connection with connection string, DataSource object or with ServiceProvider");
}
if (options.ConnectionString is not null && options.DataSource is not null && options.ServiceProviderMode == ServiceProviderObject.None)
{
throw new ArgumentException("Both ConnectionString and DataSource are provided. Please specify only one.");
}
if (options.Logger is not null)
{
NpgsqlRestMiddleware.SetLogger(options.Logger);
}
else if (builder is WebApplication app)
{
var factory = app.Services.GetRequiredService<ILoggerFactory>();
NpgsqlRestMiddleware.SetLogger(factory is not null ? factory.CreateLogger(options.LoggerName ?? typeof(NpgsqlRestMiddlewareExtensions).Namespace ?? "NpgsqlRest") : app.Logger);
}
NpgsqlRestMiddleware.SetMetadata(NpgsqlRestMetadataBuilder.Build(options, NpgsqlRestMiddleware.Logger, builder));
if (NpgsqlRestMiddleware.Metadata.Entries.Count == 0)
{
return builder;
}
NpgsqlRestMiddleware.SetOptions(options);
NpgsqlRestMiddleware.SetServiceProvider(builder.ApplicationServices);
bool streamingEventsItialized = false;
if (NpgsqlRestMiddleware.metadata.HasStreamingEvents is true)
{
builder.UseMiddleware<NpgsqlRestNoticeEventSource>();
streamingEventsItialized = true;
}
if (options.RefreshEndpointEnabled)
{
var refreshMethodUpper = options.RefreshMethod.ToUpperInvariant();
var refreshPathUpper = options.RefreshPath.ToUpperInvariant();
builder.Use(async (context, next) =>
{
if (context.Request.Method.Equals(refreshMethodUpper, StringComparison.OrdinalIgnoreCase) &&
context.Request.Path.Equals(refreshPathUpper, StringComparison.OrdinalIgnoreCase))
{
try
{
Volatile.Write(ref NpgsqlRestMiddleware.metadata, NpgsqlRestMetadataBuilder.Build(options, options.Logger, builder));
NpgsqlRestMiddleware.lookup = NpgsqlRestMiddleware.metadata.Entries.GetAlternateLookup<ReadOnlySpan<char>>();
context.Response.StatusCode = (int)HttpStatusCode.OK;
await context.Response.CompleteAsync();
if (NpgsqlRestMiddleware.metadata.HasStreamingEvents is true && streamingEventsItialized is false)
{
builder.UseMiddleware<NpgsqlRestNoticeEventSource>();
streamingEventsItialized = true;
}
}
catch (Exception e)
{
options.Logger?.LogError(e, "Failed to refresh metadata");
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = Text.Plain;
await context.Response.WriteAsync($"Failed to refresh metadata: {e.Message}");
await context.Response.CompleteAsync();
}
return;
}
await next(context);
});
}
return builder.UseMiddleware<NpgsqlRestMiddleware>();
}
}