-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
117 lines (99 loc) · 3.21 KB
/
Program.cs
File metadata and controls
117 lines (99 loc) · 3.21 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
using StormSafe.Services;
using System.Net.Http.Headers;
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
// Add Swagger/OpenAPI services
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "StormSafe API",
Version = "v1",
Description = "API for retrieving weather and storm data"
});
});
// Configure HTTP client for NOAA API
builder.Services.AddHttpClient("NOAA", client =>
{
client.BaseAddress = new Uri("https://api.weather.gov/");
client.DefaultRequestHeaders.Add("User-Agent", "StormSafe/1.0");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/geo+json"));
client.Timeout = TimeSpan.FromSeconds(30);
}).ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
AllowAutoRedirect = true,
MaxAutomaticRedirections = 5
});
// Register NexradStormMotionService (uses NOAA HttpClient for points API)
builder.Services.AddScoped<INexradStormMotionService>(sp =>
{
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
var logger = sp.GetRequiredService<ILogger<NexradStormMotionService>>();
var httpClient = httpClientFactory.CreateClient("NOAA");
return new NexradStormMotionService(httpClient, logger);
});
// Register WeatherService with the NOAA HttpClient
builder.Services.AddScoped<IWeatherService>(sp =>
{
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
var logger = sp.GetRequiredService<ILogger<WeatherService>>();
var nexrad = sp.GetRequiredService<INexradStormMotionService>();
var httpClient = httpClientFactory.CreateClient("NOAA");
return new WeatherService(httpClient, logger, nexrad);
});
builder.Services.AddMemoryCache();
// Add CORS
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
// Configure HTTPS port
builder.WebHost.ConfigureKestrel(options =>
{
options.ListenAnyIP(5202); // HTTP port
options.ListenAnyIP(7202, listenOptions => // HTTPS port
{
listenOptions.UseHttps();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
// Enable Swagger UI in development
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "StormSafe API V1");
c.RoutePrefix = "swagger";
});
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
// Enable CORS
app.UseCors("AllowAll");
app.UseAuthorization();
// Map API routes first
app.MapControllers();
// Configure default route
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();