-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
105 lines (88 loc) · 2.41 KB
/
Program.cs
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
using System.Net;
using System.Net.Sockets;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// builder.Services.AddHealthChecks()
// .AddCheck("HealthChecker", () => HealthCheckResult.Healthy("A healthy result."));
// enable blazor pages
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// add a handler at / that returns HTML
app.MapGet("/", async (HttpContext context) =>
{
context.Response.StatusCode = 200;
await context.Response.WriteAsync(@"
<html>
<head>
<title>ASP.NET Core Demo App</title>
</head>
<body>
<a href=""/swagger"">Swagger</a>
</body>
</html>
");
});
// add a handler at /health that returns JSON
app.MapGet("/health", async (HttpContext context) =>
{
// 200
context.Response.StatusCode = 200;
await context.Response.WriteAsync(@"
{
""status"": ""Healthy""
}
");
});
// app.UseHttpsRedirection();
// app.UseAuthorization();
app.MapControllers();
// enable debug logging
app.Use(async (context, next) =>
{
Console.WriteLine(context.Request.Path);
await next();
});
Console.WriteLine("Configuration:");
foreach (var item in app.Configuration.AsEnumerable())
{
Console.WriteLine($"{item.Key} = {item.Value}");
}
var host = app.Services.GetRequiredService<IHostApplicationLifetime>();
host.ApplicationStarted.Register(() =>
{
var ip = Dns.GetHostAddresses(Dns.GetHostName())
.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork);
Console.WriteLine($"Container IP: {ip}");
});
// do not do the below, as it breaks the default health check.
// easier to have / return a 200 and some content
// add a redirect from / to /swagger/index.html
// app.Use(async (context, next) =>
// {
// if (context.Request.Path == "/")
// {
// context.Response.Redirect("/swagger/index.html");
// }
// else
// {
// await next();
// }
// });
// print out app URLS that are listening
var urls = app.Urls;
Console.WriteLine("Listening on:");
foreach (var url in urls)
{
Console.WriteLine(url);
}
app.Run();