|
1 | 1 | using System; |
| 2 | +using System.IO; |
2 | 3 | using System.Linq; |
3 | 4 | using System.Net.Http; |
4 | 5 | using Microsoft.ApplicationInsights.AspNetCore.Extensions; |
|
10 | 11 | using PWABuilder.MicrosoftStore.Models; |
11 | 12 | using PWABuilder.MicrosoftStore.Services; |
12 | 13 |
|
13 | | -namespace PWABuilder.MicrosoftStore |
| 14 | +namespace PWABuilder.MicrosoftStore; |
| 15 | + |
| 16 | +public class Startup |
14 | 17 | { |
15 | | - public class Startup |
| 18 | + readonly string AllowedOriginsPolicyName = "allowedOrigins"; |
| 19 | + |
| 20 | + public Startup(IConfiguration configuration) |
16 | 21 | { |
17 | | - readonly string AllowedOriginsPolicyName = "allowedOrigins"; |
| 22 | + Configuration = configuration; |
| 23 | + } |
18 | 24 |
|
19 | | - public Startup(IConfiguration configuration) |
| 25 | + public IConfiguration Configuration { get; } |
| 26 | + |
| 27 | + // This method gets called by the runtime. Use this method to add services to the container. |
| 28 | + public void ConfigureServices(IServiceCollection services) |
| 29 | + { |
| 30 | + services.Configure<AppSettings>(Configuration.GetSection("AppSettings")); |
| 31 | + |
| 32 | + // Validate Windows SDK directory exists |
| 33 | + ValidateWindowsSdkDirectory(); |
| 34 | + |
| 35 | + services.AddCors(options => |
20 | 36 | { |
21 | | - Configuration = configuration; |
22 | | - } |
| 37 | + options.AddPolicy( |
| 38 | + name: AllowedOriginsPolicyName, |
| 39 | + builder => |
| 40 | + builder |
| 41 | + .SetIsOriginAllowed(CheckAllowedOriginCors) |
| 42 | + .AllowAnyHeader() |
| 43 | + .AllowAnyMethod() |
| 44 | + ); |
| 45 | + }); |
23 | 46 |
|
24 | | - public IConfiguration Configuration { get; } |
| 47 | + var appSettings = Configuration.GetSection("AppSettings"); |
| 48 | + var aiOptions = setUpAppInsights(appSettings); |
25 | 49 |
|
26 | | - // This method gets called by the runtime. Use this method to add services to the container. |
27 | | - public void ConfigureServices(IServiceCollection services) |
28 | | - { |
29 | | - services.Configure<AppSettings>(Configuration.GetSection("AppSettings")); |
30 | | - services.AddCors(options => |
31 | | - { |
32 | | - options.AddPolicy( |
33 | | - name: AllowedOriginsPolicyName, |
34 | | - builder => |
35 | | - builder |
36 | | - .SetIsOriginAllowed(CheckAllowedOriginCors) |
37 | | - .AllowAnyHeader() |
38 | | - .AllowAnyMethod() |
39 | | - ); |
40 | | - }); |
| 50 | + services.AddTransient<PwaBuilderWrapper>(); |
| 51 | + services.AddTransient<WindowsAppPackageCreator>(); |
| 52 | + services.AddTransient<WindowsAppPackageInspector>(); |
| 53 | + services.AddTransient<WindowsAppPackageUpdater>(); |
| 54 | + services.AddTransient<WindowsAppPackageBundler>(); |
| 55 | + services.AddTransient<WindowsActionsService>(); |
| 56 | + services.AddTransient<LooseLayoutPackager>(); |
| 57 | + services.AddTransient<SpartanWindowsPackageCreator>(); |
| 58 | + services.AddTransient<ModernWindowsPackageCreator>(); |
| 59 | + services.AddTransient<ClassicWindowsPackageCreator>(); |
| 60 | + services.AddTransient<ImageGenerator>(); |
| 61 | + services.AddTransient<WebManifestFinder>(); |
| 62 | + services.AddTransient<MakePriWrapper>(); |
| 63 | + services.AddTransient<MakeAppxWrapper>(); |
| 64 | + services.AddTransient<TempDirectory>(); |
| 65 | + services.AddTransient<Analytics>(); |
| 66 | + services.AddSingleton<ZombieProcessKiller>(); |
| 67 | + services.AddTransient<ProcessRunner>(); |
| 68 | + services.AddApplicationInsightsTelemetry(aiOptions); |
| 69 | + services.AddHttpClient(); |
| 70 | + services |
| 71 | + .AddHttpClient( |
| 72 | + "NoRedirectClient", |
| 73 | + client => |
| 74 | + { |
| 75 | + client.AddLatestEdgeUserAgent(); |
| 76 | + } |
| 77 | + ) |
| 78 | + .ConfigurePrimaryHttpMessageHandler(() => |
| 79 | + new HttpClientHandler { AllowAutoRedirect = false } |
| 80 | + ); |
41 | 81 |
|
42 | | - var appSettings = Configuration.GetSection("AppSettings"); |
43 | | - var aiOptions = setUpAppInsights(appSettings); |
| 82 | + services.AddControllers(); |
| 83 | + } |
44 | 84 |
|
45 | | - services.AddTransient<PwaBuilderWrapper>(); |
46 | | - services.AddTransient<WindowsAppPackageCreator>(); |
47 | | - services.AddTransient<WindowsAppPackageInspector>(); |
48 | | - services.AddTransient<WindowsAppPackageUpdater>(); |
49 | | - services.AddTransient<WindowsAppPackageBundler>(); |
50 | | - services.AddTransient<WindowsActionsService>(); |
51 | | - services.AddTransient<LooseLayoutPackager>(); |
52 | | - services.AddTransient<SpartanWindowsPackageCreator>(); |
53 | | - services.AddTransient<ModernWindowsPackageCreator>(); |
54 | | - services.AddTransient<ClassicWindowsPackageCreator>(); |
55 | | - services.AddTransient<ImageGenerator>(); |
56 | | - services.AddTransient<WebManifestFinder>(); |
57 | | - services.AddTransient<MakePriWrapper>(); |
58 | | - services.AddTransient<MakeAppxWrapper>(); |
59 | | - services.AddTransient<TempDirectory>(); |
60 | | - services.AddTransient<Analytics>(); |
61 | | - services.AddSingleton<ZombieProcessKiller>(); |
62 | | - services.AddTransient<ProcessRunner>(); |
63 | | - services.AddApplicationInsightsTelemetry(aiOptions); |
64 | | - services.AddHttpClient(); |
65 | | - services |
66 | | - .AddHttpClient( |
67 | | - "NoRedirectClient", |
68 | | - client => |
69 | | - { |
70 | | - client.AddLatestEdgeUserAgent(); |
71 | | - } |
72 | | - ) |
73 | | - .ConfigurePrimaryHttpMessageHandler(() => |
74 | | - new HttpClientHandler { AllowAutoRedirect = false } |
75 | | - ); |
| 85 | + static ApplicationInsightsServiceOptions setUpAppInsights(IConfigurationSection appSettings) |
| 86 | + { |
| 87 | + var connectionString = appSettings["ApplicationInsightsConnectionString"]; |
| 88 | + var aiOptions = new ApplicationInsightsServiceOptions(); |
| 89 | + aiOptions.EnableRequestTrackingTelemetryModule = false; |
| 90 | + aiOptions.EnableDependencyTrackingTelemetryModule = true; |
| 91 | + aiOptions.EnableHeartbeat = false; |
| 92 | + aiOptions.EnableAzureInstanceMetadataTelemetryModule = false; |
| 93 | + aiOptions.EnableActiveTelemetryConfigurationSetup = false; |
| 94 | + aiOptions.EnableAdaptiveSampling = false; |
| 95 | + aiOptions.EnableAppServicesHeartbeatTelemetryModule = false; |
| 96 | + aiOptions.EnableAuthenticationTrackingJavaScript = false; |
| 97 | + aiOptions.ConnectionString = connectionString; |
| 98 | + return aiOptions; |
| 99 | + } |
76 | 100 |
|
77 | | - services.AddControllers(); |
78 | | - } |
| 101 | + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
| 102 | + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
| 103 | + { |
| 104 | + app.UseDeveloperExceptionPage(); |
| 105 | + app.UseHttpsRedirection(); |
| 106 | + app.UseStaticFiles(); |
| 107 | + app.UseRouting(); |
| 108 | + app.UseCors(AllowedOriginsPolicyName); |
| 109 | + app.UseAuthorization(); |
| 110 | + app.UseEndpoints(endpoints => |
| 111 | + { |
| 112 | + endpoints.MapControllers(); |
| 113 | + }); |
| 114 | + } |
79 | 115 |
|
80 | | - static ApplicationInsightsServiceOptions setUpAppInsights(IConfigurationSection appSettings) |
| 116 | + private bool CheckAllowedOriginCors(string origin) |
| 117 | + { |
| 118 | + //Add to this list if more domains call this service |
| 119 | + var allowedOrigins = new[] |
81 | 120 | { |
82 | | - var connectionString = appSettings["ApplicationInsightsConnectionString"]; |
83 | | - var aiOptions = new ApplicationInsightsServiceOptions(); |
84 | | - aiOptions.EnableRequestTrackingTelemetryModule = false; |
85 | | - aiOptions.EnableDependencyTrackingTelemetryModule = true; |
86 | | - aiOptions.EnableHeartbeat = false; |
87 | | - aiOptions.EnableAzureInstanceMetadataTelemetryModule = false; |
88 | | - aiOptions.EnableActiveTelemetryConfigurationSetup = false; |
89 | | - aiOptions.EnableAdaptiveSampling = false; |
90 | | - aiOptions.EnableAppServicesHeartbeatTelemetryModule = false; |
91 | | - aiOptions.EnableAuthenticationTrackingJavaScript = false; |
92 | | - aiOptions.ConnectionString = connectionString; |
93 | | - return aiOptions; |
94 | | - } |
| 121 | + "https://www.pwabuilder.com", |
| 122 | + "https://pwabuilder.com", |
| 123 | + "https://preview.pwabuilder.com", |
| 124 | + "https://localhost:3333", |
| 125 | + "https://localhost:3000", |
| 126 | + "http://localhost:3333", |
| 127 | + "http://localhost:3000", |
| 128 | + "https://localhost:8000", |
| 129 | + "http://localhost:8000", |
| 130 | + "https://localhost:7217", |
| 131 | + "http://localhost:5777", |
| 132 | + "https://nice-field-047c1420f.azurestaticapps.net", |
| 133 | + "https://partner.microsoft.com", |
| 134 | + "https://brave-grass-02c461d10.1.azurestaticapps.net", |
| 135 | + "https://pwabuilder-summer25-consolidation.azurewebsites.net", |
| 136 | + }; |
| 137 | + return allowedOrigins.Any(o => origin.Contains(o, StringComparison.OrdinalIgnoreCase)); |
| 138 | + } |
95 | 139 |
|
96 | | - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
97 | | - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
| 140 | + /// <summary> |
| 141 | + /// Validates that the Windows SDK directory specified in AppSettings exists on disk. |
| 142 | + /// Throws an exception if the directory is not found. |
| 143 | + /// </summary> |
| 144 | + private void ValidateWindowsSdkDirectory() |
| 145 | + { |
| 146 | + var appSettings = Configuration.GetSection("AppSettings"); |
| 147 | + var windowsSdkDirectory = appSettings["WindowsSdkDirectory"]; |
| 148 | + |
| 149 | + if (string.IsNullOrWhiteSpace(windowsSdkDirectory)) |
98 | 150 | { |
99 | | - app.UseDeveloperExceptionPage(); |
100 | | - app.UseHttpsRedirection(); |
101 | | - app.UseStaticFiles(); |
102 | | - app.UseRouting(); |
103 | | - app.UseCors(AllowedOriginsPolicyName); |
104 | | - app.UseAuthorization(); |
105 | | - app.UseEndpoints(endpoints => |
106 | | - { |
107 | | - endpoints.MapControllers(); |
108 | | - }); |
| 151 | + throw new InvalidOperationException( |
| 152 | + "WindowsSdkDirectory is not configured in AppSettings. " + |
| 153 | + "Please ensure the Windows SDK is installed and the correct path is specified in configuration."); |
109 | 154 | } |
110 | | - |
111 | | - private bool CheckAllowedOriginCors(string origin) |
| 155 | + |
| 156 | + if (!Directory.Exists(windowsSdkDirectory)) |
| 157 | + { |
| 158 | + throw new DirectoryNotFoundException( |
| 159 | + $"Windows SDK directory not found: '{windowsSdkDirectory}'. " + |
| 160 | + "Please ensure the Windows SDK is properly installed. " + |
| 161 | + "Expected tools like makeappx.exe should be available in this directory."); |
| 162 | + } |
| 163 | + |
| 164 | + // Additional validation: check if makeappx.exe exists in the SDK directory |
| 165 | + var makeAppxPath = Path.Combine(windowsSdkDirectory, "makeappx.exe"); |
| 166 | + if (!File.Exists(makeAppxPath)) |
112 | 167 | { |
113 | | - //Add to this list if more domains call this service |
114 | | - var allowedOrigins = new[] |
115 | | - { |
116 | | - "https://www.pwabuilder.com", |
117 | | - "https://pwabuilder.com", |
118 | | - "https://preview.pwabuilder.com", |
119 | | - "https://localhost:3333", |
120 | | - "https://localhost:3000", |
121 | | - "http://localhost:3333", |
122 | | - "http://localhost:3000", |
123 | | - "https://localhost:8000", |
124 | | - "http://localhost:8000", |
125 | | - "https://localhost:7217", |
126 | | - "http://localhost:5777", |
127 | | - "https://nice-field-047c1420f.azurestaticapps.net", |
128 | | - "https://partner.microsoft.com", |
129 | | - "https://brave-grass-02c461d10.1.azurestaticapps.net", |
130 | | - "https://pwabuilder-summer25-consolidation.azurewebsites.net", |
131 | | - }; |
132 | | - return allowedOrigins.Any(o => origin.Contains(o, StringComparison.OrdinalIgnoreCase)); |
| 168 | + throw new FileNotFoundException( |
| 169 | + $"Required tool 'makeappx.exe' not found in Windows SDK directory: '{windowsSdkDirectory}'. " + |
| 170 | + "Please ensure the Windows SDK is properly installed with MSIX packaging tools."); |
133 | 171 | } |
| 172 | + |
| 173 | + Console.WriteLine($"✓ Windows SDK validated successfully at: {windowsSdkDirectory}"); |
134 | 174 | } |
135 | 175 | } |
0 commit comments