Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
120 lines (96 loc) · 4.14 KB
/
Copy pathProgram.cs
File metadata and controls
120 lines (96 loc) · 4.14 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
using Microsoft.EntityFrameworkCore;
using price_web_api.Data;
using price_web_api.Models;
using price_web_api.Services;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
Console.WriteLine(">>> Starting Price Web API...");
// Configure Kestrel for HTTPS using PFX
builder.WebHost.ConfigureKestrel(serverOptions =>
{
var certsDir = "/app/certs";
var pfxPath = Path.Combine(certsDir, "cloudflare.pfx"); //
// Debug: List all files in certs directory
Console.WriteLine($">>> Looking for certificates in: {certsDir}");
if (Directory.Exists(certsDir))
{
Console.WriteLine("Directory exists. Files found:");
foreach (var file in Directory.GetFiles(certsDir))
{
Console.WriteLine($" - {file}");
}
}
else
{
Console.WriteLine("Directory does NOT exist!");
}
Console.WriteLine($">>> Checking for PFX at: {pfxPath}");
Console.WriteLine($">>> PFX exists: {File.Exists(pfxPath)}");
string? envCertPwd = Environment.GetEnvironmentVariable("CERT_PASSWORD");
Console.WriteLine($"CERT_PASSWORD environment variable is {(string.IsNullOrEmpty(envCertPwd) ? "not set or empty" : "set")}");
if (File.Exists(pfxPath))
{
Console.WriteLine("Loading HTTPS with PFX certificate...");
serverOptions.ListenAnyIP(8080); // HTTP
serverOptions.ListenAnyIP(8081, listenOptions =>
{
listenOptions.UseHttps(pfxPath, envCertPwd ?? "");
});
Console.WriteLine("HTTPS configured on port 8081");
}
else
{
// Fallback to HTTP only if certs not found
serverOptions.ListenAnyIP(8080);
Console.WriteLine("HTTPS certificates not found at /app/certs/, running HTTP only.");
}
});
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
var dbDirectory = "/data/pricewebapi";
Directory.CreateDirectory(dbDirectory);
var dbPath = Path.Combine(dbDirectory, "prices.db");
builder.Services.AddDbContext<PriceContext>(options =>
options.EnableSensitiveDataLogging()
.UseSqlite($"Data Source={dbPath}"));
builder.Services.AddScoped<IPriceRecordQueryService, PriceRecordQueryService>();
builder.Services.AddScoped<IInvestmentCommandService, InvestmentCommandService>();
builder.Services.AddCors();
var app = builder.Build();
// Ensure database and tables are created at runtime (no migrations required)
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<PriceContext>();
db.Database.Migrate();
}
// Configure HTTPS redirection and HSTS
// Only enable HTTPS redirection if in production AND you want to force HTTPS
// if (!app.Environment.IsDevelopment())
// {
// app.UseHsts();
// app.UseHttpsRedirection();
// }
// Database schema is managed by EF Core migrations.
// Post methods for creating records
// Local network only
app.MapPost("addpricerecord", LocalOnlyEndpoint.AddPriceRecord).AddEndpointFilter<LocalNetworkOnlyFilter>();
app.MapPost("addfund", LocalOnlyEndpoint.AddFund).AddEndpointFilter<LocalNetworkOnlyFilter>();
app.MapPost("addmetal", LocalOnlyEndpoint.AddMetal).AddEndpointFilter<LocalNetworkOnlyFilter>();
//app.MapPost("addinvestment", LocalOnlyEndpoint.AddFund).AddEndpointFilter<LocalNetworkOnlyFilter>();
// ***
// Get methods for retrieving records
app.MapGet("investments", RemoteEndPoint.GetInvestments);
app.MapGet("investment/{ticker}", RemoteEndPoint.GetInvestmentByTicker);
app.MapGet("info.xml/{ticker}", RemoteEndPoint.GetInvestmentInfoAsXml);
app.MapGet("prices/{ticker}", RemoteEndPoint.GetPriceRecordsByTicker);
app.MapGet("prices.xml/{ticker}", RemoteEndPoint.GetPriceRecordsByTickerAsXml);
app.MapGet("pricerecord/{ticker}/{date}", RemoteEndPoint.GetPriceRecord);
app.MapGet("pricerecord.xml/{ticker}/{date}", RemoteEndPoint.GetPriceRecordAsXml);
app.MapGet("nonnullprice.xml/{ticker}/{date}", RemoteEndPoint.GetNonNullPriceAsXml);
//
app.UseCors(policy => policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
app.Run();