-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathProgram.cs
More file actions
185 lines (162 loc) · 6.69 KB
/
Program.cs
File metadata and controls
185 lines (162 loc) · 6.69 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Options;
using Microsoft.PWABuilder.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using PWABuilder.Services;
using PWABuilder.Common;
using PWABuilder.IOS.Services;
using PWABuilder.Models;
using PWABuilder.Utils;
using PWABuilder.Validations.Services;
using StackExchange.Redis;
var builder = WebApplication.CreateBuilder(args);
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) // load base settings
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: true) // load environment-specific settings
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) // load local settings
.AddEnvironmentVariables();
builder.Configuration.AddConfiguration(configuration.Build());
// Remove duplicate logging.
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
// App settings
var appSettings = builder.Configuration.GetSection("AppSettings");
var aiOptions = AppInsights.setUpAppInsights(appSettings);
builder.Services.Configure<AppSettings>(appSettings);
builder.Services.AddApplicationInsightsTelemetry(aiOptions);
JsonConvert.DefaultSettings = () =>
new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
// Add services to the container.
builder.Services.AddSingleton<ViteEntryPointProvider>();
builder.Services.AddSingleton<IPuppeteerService, PuppeteerService>();
builder.Services.AddTransient<TempDirectory>();
builder.Services.AddTransient<IOSImageWriter>();
builder.Services.AddTransient<IOSPackageCreator>();
if (builder.Environment.IsDevelopment())
{
// In development, we use an in-memory queue for analysis jobs and store package jobs. This prevents issues around using Azure Managed Identity authentication locally.
builder.Services.AddSingleton<IAnalysisJobQueue, InMemoryAnalysisJobQueue>();
// In development, we use an in-memory database for Analysis objects. This makes local development and testing simpler, as we don't need to connect to Redis.
builder.Services.AddSingleton<IRedisCache, InMemoryRedisCache>();
// In development, we use an in-memory blob storage service.
builder.Services.AddSingleton<IBlobStorageService, InMemoryBlobStorageService>();
}
else
{
// In production, we use a Redis atomic list as the analysis queue.
builder.Services.AddSingleton<IAnalysisJobQueue, AnalysisJobQueue>();
// In production, we use PWABuilderDatabase, which uses Redis as a backing store.
builder.Services.AddSingleton<IRedisCache, RedisCache>();
// In production, we use Azure Storage for blob storage.
builder.Services.AddSingleton<IBlobStorageService, AzureStorageService>();
}
builder.Services.AddSingleton<WebStringCache>();
builder.Services.AddHostedService<AnalysisJobProcessor>();
builder.Services.AddSingleton<ManifestDetector>();
builder.Services.AddSingleton<ServiceWorkerDetector>();
builder.Services.AddSingleton<ManifestCreator>();
builder.Services.AddSingleton<GeneralWebAppCapabilityDetector>();
builder.Services.AddHttpClient();
builder.Services.AddScoped<ITelemetryService, TelemetryService>();
builder.Services.AddSingleton<ILighthouseService, LighthouseService>();
builder.Services.AddSingleton<ManifestAnalyzer>();
builder.Services.AddSingleton<IServiceWorkerAnalyzer, ServiceWorkerAnalyzer>();
builder.Services.AddSingleton<IImageValidationService, ImageValidationService>();
builder.Services.AddSingleton<StoreImageCreator>();
builder.Services.AddSingleton(services =>
{
// Create a single, reusable Puppeteer browser instance. This can be used across different requests so that we're not spinning up multiple browsers for each request.
var env = services.GetRequiredService<IHostEnvironment>();
return PuppeteerService.CreateBrowserAsync(env);
});
builder.Services.AddControllersWithViews();
// An HTTP client with the PWABuilderHttpAgent string appended.
builder.Services.AddHttpClient(Constants.PwaBuilderAgentHttpClient, client =>
{
client.DefaultRequestHeaders.UserAgent.ParseAdd($"{Constants.DesktopUserAgent} PWABuilderHttpAgent");
})
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
AutomaticDecompression = System.Net.DecompressionMethods.All
});
builder
.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.DefaultIgnoreCondition = System
.Text
.Json
.Serialization
.JsonIgnoreCondition
.WhenWritingNull;
});
// Configure HSTS for production
if (!builder.Environment.IsDevelopment())
{
builder.Services.AddHsts(options =>
{
options.Preload = true;
options.IncludeSubDomains = true;
options.MaxAge = TimeSpan.FromDays(365);
options.ExcludedHosts.Clear(); // Clear any default exclusions
});
}
if (builder.Environment.IsDevelopment())
{
builder.Services.AddCors(options =>
{
options.AddPolicy(
"AllowAllOrigins",
builder =>
{
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
}
);
});
}
var app = builder.Build();
// Configure forwarded headers FIRST - this must be before any other middleware
var forwardedHeadersOptions = new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
};
// For Azure App Service and other cloud providers, trust all proxies
forwardedHeadersOptions.KnownIPNetworks.Clear();
forwardedHeadersOptions.KnownProxies.Clear();
app.UseForwardedHeaders(forwardedHeadersOptions);
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseHsts();
app.UseHttpsRedirection();
}
app.UseRouting();
app.UseAuthorization();
app.MapControllers();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebSockets(); // used for hot module reload with Vite local dev server.
app.UseCors("AllowAllOrigins");
// For development, tell ASP.NET to serve .ts files. This is needed for Vite to serve web workers.
var contentFileProvider = new FileExtensionContentTypeProvider();
contentFileProvider.Mappings[".ts"] = "application/javascript";
app.UseStaticFiles(
new StaticFileOptions
{
FileProvider = new ViteStaticFileProvider(
builder.Environment.WebRootPath,
builder.Environment.ContentRootPath
),
ServeUnknownFileTypes = true,
ContentTypeProvider = contentFileProvider,
}
);
}
else
{
app.UseStaticFiles();
}
app.Run();