2024-12-09 21:11:46 +01:00
|
|
|
// Copyright (C) 2023-present sam/u1f320 (vulpine.solutions)
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2024-12-10 15:28:44 +01:00
|
|
|
using System.Text.Json;
|
|
|
|
using System.Text.Json.Serialization;
|
2024-05-28 15:29:18 +02:00
|
|
|
using Foxnouns.Backend;
|
2024-05-27 15:53:54 +02:00
|
|
|
using Foxnouns.Backend.Extensions;
|
2024-06-12 03:47:20 +02:00
|
|
|
using Foxnouns.Backend.Services;
|
2024-07-12 17:12:24 +02:00
|
|
|
using Foxnouns.Backend.Utils;
|
2024-12-10 15:28:44 +01:00
|
|
|
using Foxnouns.Backend.Utils.OpenApi;
|
2024-05-28 15:29:18 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-05-28 17:09:50 +02:00
|
|
|
using Newtonsoft.Json;
|
2024-05-27 15:53:54 +02:00
|
|
|
using Newtonsoft.Json.Serialization;
|
2024-09-03 17:00:14 +02:00
|
|
|
using Prometheus;
|
2024-12-10 15:28:44 +01:00
|
|
|
using Scalar.AspNetCore;
|
2024-07-08 19:03:04 +02:00
|
|
|
using Sentry.Extensibility;
|
2024-10-02 00:28:07 +02:00
|
|
|
using Serilog;
|
2024-05-27 15:53:54 +02:00
|
|
|
|
2024-12-08 15:07:25 +01:00
|
|
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
2024-05-27 15:53:54 +02:00
|
|
|
|
2024-12-08 15:07:25 +01:00
|
|
|
Config config = builder.AddConfiguration();
|
2024-05-27 15:53:54 +02:00
|
|
|
|
2024-09-04 01:46:39 +02:00
|
|
|
builder.AddSerilog();
|
2024-05-27 15:53:54 +02:00
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
builder
|
|
|
|
.WebHost.UseSentry(opts =>
|
2024-07-12 17:12:24 +02:00
|
|
|
{
|
2024-11-23 20:43:43 +01:00
|
|
|
opts.Dsn = config.Logging.SentryUrl ?? "";
|
2024-07-12 17:12:24 +02:00
|
|
|
opts.TracesSampleRate = config.Logging.SentryTracesSampleRate;
|
|
|
|
opts.MaxRequestBodySize = RequestSize.Small;
|
|
|
|
})
|
|
|
|
.ConfigureKestrel(opts =>
|
|
|
|
{
|
|
|
|
// Requests are limited to a maximum of 2 MB.
|
|
|
|
// No valid request body will ever come close to this limit,
|
|
|
|
// but the limit is slightly higher to prevent valid requests from being rejected.
|
|
|
|
opts.Limits.MaxRequestBodySize = 2 * 1024 * 1024;
|
|
|
|
});
|
2024-07-08 19:03:04 +02:00
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
builder
|
|
|
|
.Services.AddControllers()
|
2024-12-10 15:28:44 +01:00
|
|
|
.AddJsonOptions(options =>
|
|
|
|
{
|
|
|
|
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;
|
|
|
|
options.JsonSerializerOptions.Converters.Add(
|
|
|
|
new JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseUpper)
|
|
|
|
);
|
|
|
|
})
|
2024-05-27 15:53:54 +02:00
|
|
|
.AddNewtonsoftJson(options =>
|
2024-07-12 17:12:24 +02:00
|
|
|
{
|
|
|
|
options.SerializerSettings.ContractResolver = new PatchRequestContractResolver
|
2024-05-27 15:53:54 +02:00
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
NamingStrategy = new SnakeCaseNamingStrategy(),
|
2024-07-12 17:12:24 +02:00
|
|
|
};
|
|
|
|
})
|
2024-05-28 15:29:18 +02:00
|
|
|
.ConfigureApiBehaviorOptions(options =>
|
|
|
|
{
|
2024-12-11 16:54:06 +01:00
|
|
|
// the type isn't needed but without it, rider keeps complaining for no reason (it compiles just fine)
|
|
|
|
options.InvalidModelStateResponseFactory = (ActionContext actionContext) =>
|
|
|
|
new BadRequestObjectResult(
|
|
|
|
new ApiError.AspBadRequest("Bad request", actionContext.ModelState).ToJson()
|
|
|
|
);
|
2024-05-28 15:29:18 +02:00
|
|
|
});
|
2024-05-27 15:53:54 +02:00
|
|
|
|
2024-12-10 15:28:44 +01:00
|
|
|
builder.Services.AddOpenApi(
|
|
|
|
"v2",
|
|
|
|
options =>
|
|
|
|
{
|
|
|
|
options.AddSchemaTransformer<PropertyKeySchemaTransformer>();
|
|
|
|
options.AddSchemaTransformer<ExampleFixingSchemaTransformer>();
|
|
|
|
options.AddDocumentTransformer(new DocumentTransformer(config));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-05-28 17:09:50 +02:00
|
|
|
// Set the default converter to snake case as we use it in a couple places.
|
2024-10-02 00:28:07 +02:00
|
|
|
JsonConvert.DefaultSettings = () =>
|
|
|
|
new JsonSerializerSettings
|
2024-05-28 17:09:50 +02:00
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
ContractResolver = new DefaultContractResolver
|
|
|
|
{
|
|
|
|
NamingStrategy = new SnakeCaseNamingStrategy(),
|
|
|
|
},
|
|
|
|
};
|
2024-05-28 17:09:50 +02:00
|
|
|
|
2024-12-10 15:28:44 +01:00
|
|
|
builder.AddServices(config).AddCustomMiddleware();
|
2024-07-08 19:03:04 +02:00
|
|
|
|
2024-12-08 15:07:25 +01:00
|
|
|
WebApplication app = builder.Build();
|
2024-05-27 15:53:54 +02:00
|
|
|
|
2024-05-28 15:29:18 +02:00
|
|
|
await app.Initialize(args);
|
|
|
|
|
2024-05-27 15:53:54 +02:00
|
|
|
app.UseSerilogRequestLogging();
|
|
|
|
app.UseRouting();
|
2024-10-02 00:28:07 +02:00
|
|
|
|
2024-07-08 19:03:04 +02:00
|
|
|
// Not all environments will want tracing (from experience, it's expensive to use in production, even with a low sample rate),
|
|
|
|
// so it's locked behind a config option.
|
2024-10-02 00:28:07 +02:00
|
|
|
if (config.Logging.SentryTracing)
|
|
|
|
app.UseSentryTracing();
|
2024-05-27 15:53:54 +02:00
|
|
|
app.UseCors();
|
2024-05-28 15:29:18 +02:00
|
|
|
app.UseCustomMiddleware();
|
2024-05-27 15:53:54 +02:00
|
|
|
app.MapControllers();
|
2024-12-10 15:28:44 +01:00
|
|
|
app.MapOpenApi("/api-docs/openapi/{documentName}.json");
|
|
|
|
app.MapScalarApiReference(options =>
|
|
|
|
{
|
|
|
|
options.Title = "pronouns.cc API";
|
|
|
|
options.OpenApiRoutePattern = "/api-docs/openapi/{documentName}.json";
|
|
|
|
options.EndpointPathPrefix = "/api-docs/{documentName}";
|
|
|
|
});
|
2024-05-27 15:53:54 +02:00
|
|
|
|
|
|
|
app.Urls.Clear();
|
|
|
|
app.Urls.Add(config.Address);
|
2024-09-03 17:00:14 +02:00
|
|
|
|
|
|
|
// Make sure metrics are updated whenever Prometheus scrapes them
|
|
|
|
Metrics.DefaultRegistry.AddBeforeCollectCallback(async ct =>
|
2024-10-02 00:28:07 +02:00
|
|
|
await app.Services.GetRequiredService<MetricsCollectionService>().CollectMetricsAsync(ct)
|
|
|
|
);
|
2024-05-27 15:53:54 +02:00
|
|
|
|
|
|
|
app.Run();
|
2024-10-02 00:28:07 +02:00
|
|
|
Log.CloseAndFlush();
|