-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
87 lines (69 loc) · 2.37 KB
/
Program.cs
File metadata and controls
87 lines (69 loc) · 2.37 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
using Scalar.AspNetCore;
using Microsoft.EntityFrameworkCore;
using background_jobs.Data;
using background_jobs.Services;
var builder = WebApplication.CreateBuilder(args);
DotNetEnv.Env.Load();
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddHttpContextAccessor();
var GetConnectionString = Environment.GetEnvironmentVariable("CONNECTION_STRING");
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(GetConnectionString));
builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration = builder.Configuration.GetConnectionString("Redis");
options.InstanceName = "PharmacyAppCache:";
// Add connection resilience
options.ConfigurationOptions = new StackExchange.Redis.ConfigurationOptions
{
EndPoints = { builder.Configuration.GetConnectionString("Redis")! },
AbortOnConnectFail = false,
ConnectRetry = 5,
ConnectTimeout = 5000,
SyncTimeout = 5000
};
});
//add cors
builder.Services.AddCors(options =>
{
// Define a specific policy
options.AddPolicy("AllowSpecificOrigin",
builder =>
{
builder.WithOrigins("http://localhost:5173")
.AllowAnyHeader()
.AllowAnyMethod();
});
// Optionally, define a more permissive default policy (use with caution)
options.AddPolicy("AllowAll",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
builder.Services.AddSingleton<RedisCacheService>();
builder.Services.AddScoped<ICoinDataService, CoinDataService>();
builder.Services.AddHostedService<ExchangeRateUpdaterService>();
builder.Services.AddHttpClient("CoinGecko", client =>
{
client.BaseAddress = new Uri("https://api.coingecko.com/api/v3/");
client.DefaultRequestHeaders.UserAgent.ParseAdd("MyCryptoApp/1.0"); // Set the User-Agent
});
builder.Services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference();
}
app.UseCors("AllowSpecificOrigin");
app.UseHttpsRedirection();
app.UseStaticFiles();
app.MapControllers();
app.Run();