| | | 1 | | using Syki.Back.Hubs; |
| | | 2 | | using Syki.Back.Converters; |
| | | 3 | | using Microsoft.AspNetCore.HttpOverrides; |
| | | 4 | | |
| | | 5 | | namespace Syki.Back.Configs; |
| | | 6 | | |
| | | 7 | | public static class HttpConfigs |
| | | 8 | | { |
| | | 9 | | public static void AddHttpConfigs(this WebApplicationBuilder builder) |
| | | 10 | | { |
| | 2 | 11 | | builder.Services.AddControllers().AddJsonOptions(options => |
| | 4 | 12 | | options.JsonSerializerOptions.Converters.Add(new SykiStringEnumConverter())); |
| | | 13 | | |
| | 2 | 14 | | builder.Services.Configure<MvcOptions>(options => |
| | 2 | 15 | | { |
| | 2 | 16 | | options.Filters.Add(new ProducesAttribute("application/json")); |
| | 2 | 17 | | options.Filters.Add(new ConsumesAttribute("application/json")); |
| | 4 | 18 | | }); |
| | | 19 | | |
| | 2 | 20 | | builder.Services.AddHttpContextAccessor(); |
| | 4 | 21 | | builder.Services.AddRouting(options => options.LowercaseUrls = true); |
| | | 22 | | |
| | 2 | 23 | | builder.Services.AddSignalR(); |
| | | 24 | | |
| | 2 | 25 | | builder.Services.AddHttpClient(); |
| | 2 | 26 | | } |
| | | 27 | | |
| | | 28 | | public static void UseForwardedHeaders(this WebApplication app) |
| | | 29 | | { |
| | 2 | 30 | | var fwd = new ForwardedHeadersOptions |
| | 2 | 31 | | { |
| | 2 | 32 | | ForwardLimit = null, |
| | 2 | 33 | | RequireHeaderSymmetry = false, |
| | 2 | 34 | | ForwardedHeaders = ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost | ForwardedHeaders.XFo |
| | 2 | 35 | | }; |
| | | 36 | | |
| | 2 | 37 | | fwd.KnownProxies.Clear(); |
| | 2 | 38 | | fwd.KnownIPNetworks.Clear(); |
| | | 39 | | |
| | 2 | 40 | | app.UseForwardedHeaders(fwd); |
| | 2 | 41 | | } |
| | | 42 | | |
| | | 43 | | public static void UseHttpsConfigs(this WebApplication app) |
| | | 44 | | { |
| | 2 | 45 | | app.UseHsts(); |
| | 2 | 46 | | app.UseHttpsRedirection(); |
| | 2 | 47 | | } |
| | | 48 | | |
| | | 49 | | public static void UseExceptions(this IApplicationBuilder app) |
| | | 50 | | { |
| | 2 | 51 | | app.UseMiddleware<ExceptionsMiddleware>(); |
| | 2 | 52 | | } |
| | | 53 | | |
| | | 54 | | public static void UseControllers(this IApplicationBuilder app) |
| | | 55 | | { |
| | 2 | 56 | | app.UseEndpoints(options => |
| | 2 | 57 | | { |
| | 2 | 58 | | options.MapControllers(); |
| | 2 | 59 | | |
| | 2 | 60 | | options.MapHub<SykiHub>("/syki-hub"); |
| | 2 | 61 | | |
| | 2 | 62 | | options.MapOpenApi(); |
| | 2 | 63 | | options.MapScalarDocs(); |
| | 4 | 64 | | }); |
| | 2 | 65 | | } |
| | | 66 | | |
| | | 67 | | public static void UseLogs(this IApplicationBuilder app) |
| | | 68 | | { |
| | 2 | 69 | | app.UseSerilogRequestLogging(); |
| | 2 | 70 | | } |
| | | 71 | | } |