-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
143 lines (121 loc) · 3.72 KB
/
Program.cs
File metadata and controls
143 lines (121 loc) · 3.72 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
using MinimalApiDemo.Configuration;
var builder = WebApplication.CreateBuilder(args);
// Configure strongly-typed settings
builder.Services.Configure<AppSettings>(builder.Configuration);
var appSettings = builder.Configuration.Get<AppSettings>();
// Add services to the container.
builder.Services.AddEndpointsApiExplorer();
// Configure Swagger based on settings
if (appSettings?.Swagger.Enabled == true)
{
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new()
{
Title = appSettings.Swagger.Title,
Description = appSettings.Swagger.Description,
Version = appSettings.Swagger.Version,
Contact = new()
{
Name = appSettings.Swagger.Contact.Name,
Email = appSettings.Swagger.Contact.Email
}
});
});
}
// Configure HTTPS
builder.Services.AddHttpsRedirection(options =>
{
options.HttpsPort = 7001;
});
// Add CORS
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
{
policy.WithOrigins(appSettings?.Cors.AllowedOrigins ?? Array.Empty<string>())
.AllowAnyHeader()
.AllowAnyMethod();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
if (appSettings?.Swagger.Enabled == true)
{
app.UseSwagger();
app.UseSwaggerUI();
}
if (appSettings?.Development.ShowDetailedErrors == true)
{
app.UseDeveloperExceptionPage();
}
}
// Use HTTPS redirection if required
if (appSettings?.Security.RequireHttps == true)
{
app.UseHttpsRedirection();
}
// Use CORS
app.UseCors();
// Define minimal API endpoints
app.MapGet("/", () => new
{
message = "Hello World! This is a Minimal API.",
name = appSettings?.ApiSettings.Name,
version = appSettings?.ApiSettings.Version,
description = appSettings?.ApiSettings.Description
});
app.MapGet("/api/hello", () => new
{
message = "Hello from Minimal API!",
timestamp = DateTime.UtcNow,
environment = app.Environment.EnvironmentName
});
app.MapGet("/api/weather", () =>
{
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
});
app.MapPost("/api/echo", (EchoRequest request) =>
new { message = request.Message, receivedAt = DateTime.UtcNow });
// Health check endpoint
if (appSettings?.HealthChecks.Enabled == true)
{
app.MapGet(appSettings.HealthChecks.Endpoint, () => new
{
status = "Healthy",
timestamp = DateTime.UtcNow,
environment = app.Environment.EnvironmentName
});
}
// Configuration endpoint (only in development)
if (app.Environment.IsDevelopment())
{
app.MapGet("/api/config", () => new
{
apiSettings = appSettings?.ApiSettings,
cors = new { allowedOrigins = appSettings?.Cors.AllowedOrigins },
swagger = new { enabled = appSettings?.Swagger.Enabled },
security = new { requireHttps = appSettings?.Security.RequireHttps }
});
}
app.Run();
// Record types for the API
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
record EchoRequest(string Message);