-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
70 lines (55 loc) · 1.96 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
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using StackExchange.Redis;
using System;
using DotNetEnv;
Env.Load(); // Loads .env file if present
// Set the options with the desired content root path
var options = new WebApplicationOptions
{
ContentRootPath = "/"
};
var builder = WebApplication.CreateBuilder(options);
// Load Redis configuration
var redisHost = Environment.GetEnvironmentVariable("REDIS_HOST") ?? "localhost";
var redisPort = Environment.GetEnvironmentVariable("REDIS_PORT") ?? "6379";
var redisUsername = Environment.GetEnvironmentVariable("REDIS_USERNAME") ?? "default";
var redisPassword = Environment.GetEnvironmentVariable("REDIS_PASSWORD") ?? "";
var redisConnectionString = $"{redisHost}:{redisPort},password={redisPassword},ssl=False";
Console.WriteLine($"Connecting to Redis at {redisHost}:{redisPort}");
// Register Redis connection
builder.Services.AddSingleton<IConnectionMultiplexer>(sp =>
{
return ConnectionMultiplexer.Connect(redisConnectionString);
});
builder.Services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// Map the /health endpoint
app.MapGet("/health", () =>
{
try
{
var redisConnection = app.Services.GetRequiredService<IConnectionMultiplexer>();
if (redisConnection.IsConnected)
{
return Results.Ok(new { status = "healthy" });
}
return Results.Json(new { status = "unhealthy", message = "Unable to connect to Redis" }, statusCode: 500);
}
catch (Exception ex)
{
return Results.Json(new { status = "unhealthy", message = ex.Message }, statusCode: 500);
}
});
// Map the root ("/") endpoint
app.MapGet("/", () => Results.Ok("Welcome to the application!"));
// app.UseHttpsRedirection();
app.MapControllers();
app.Run();