// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschränkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Net.Http.Headers; using Squidex.Infrastructure.Json; using Squidex.Pipeline.Robots; using Squidex.Web; using Squidex.Web.Pipeline; namespace Squidex.Config.Web { public static class WebExtensions { public static IApplicationBuilder UseSquidexLocalCache(this IApplicationBuilder app) { app.UseMiddleware(); return app; } public static IApplicationBuilder UseSquidexLocalization(this IApplicationBuilder app) { var supportedCultures = new[] { "en" }; var localizationOptions = new RequestLocalizationOptions() .SetDefaultCulture(supportedCultures[0]) .AddSupportedCultures(supportedCultures) .AddSupportedUICultures(supportedCultures); app.UseRequestLocalization(localizationOptions); return app; } public static IApplicationBuilder UseSquidexTracking(this IApplicationBuilder app) { app.UseMiddleware(); app.UseMiddleware(); app.UseMiddleware(); return app; } public static IApplicationBuilder UseSquidexHealthCheck(this IApplicationBuilder app) { var serializer = app.ApplicationServices.GetRequiredService(); var writer = new Func((httpContext, report) => { var response = new { Entries = report.Entries.ToDictionary(x => x.Key, x => { var value = x.Value; return new { Data = value.Data.Count > 0 ? new Dictionary(value.Data) : null, value.Description, value.Duration, value.Status }; }), report.Status, report.TotalDuration }; var json = serializer.Serialize(response); httpContext.Response.Headers[HeaderNames.ContentType] = "text/json"; return httpContext.Response.WriteAsync(json); }); app.UseHealthChecks("/readiness", new HealthCheckOptions { Predicate = check => !check.Tags.Contains("background"), ResponseWriter = writer }); app.UseHealthChecks("/healthz", new HealthCheckOptions { Predicate = check => check.Tags.Contains("node"), ResponseWriter = writer }); app.UseHealthChecks("/cluster-healthz", new HealthCheckOptions { Predicate = check => check.Tags.Contains("cluster"), ResponseWriter = writer }); app.UseHealthChecks("/background-healthz", new HealthCheckOptions { Predicate = check => check.Tags.Contains("background"), ResponseWriter = writer }); return app; } public static IApplicationBuilder UseSquidexRobotsTxt(this IApplicationBuilder app) { app.Map("/robots.txt", builder => builder.UseMiddleware()); return app; } public static void UseSquidexCors(this IApplicationBuilder app) { app.UseCors(builder => builder .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader()); } public static void UseSquidexForwardingRules(this IApplicationBuilder app, IConfiguration config) { app.UseForwardedHeaders(GetForwardingOptions(config)); app.UseMiddleware(); app.UseMiddleware(); } private static ForwardedHeadersOptions GetForwardingOptions(IConfiguration config) { var urlsOptions = config.GetSection("urls").Get(); if (!string.IsNullOrWhiteSpace(urlsOptions.BaseUrl) && urlsOptions.EnableXForwardedHost) { return new ForwardedHeadersOptions { AllowedHosts = new List { new Uri(urlsOptions.BaseUrl).Host }, ForwardedHeaders = ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost, ForwardLimit = null, RequireHeaderSymmetry = false }; } else { return new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedProto, ForwardLimit = null, RequireHeaderSymmetry = false }; } } } }