Files
fubon-api/Program.cs

49 lines
1.3 KiB
C#

using fubon_api.Controllers;
using fubon_api.Models;
using FubonNeo.Sdk;
var builder = WebApplication.CreateBuilder(args);
// appsettings
var fubonSettings = builder.Configuration.GetSection("FubonSettings").Get<FubonSettings>();
if (fubonSettings == null)
{
throw new InvalidOperationException("FubonSettings is missing.");
}
// 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.AddSingleton<FubonSDK>();
builder.Services.AddSingleton<Account>(provider =>
{
var sdk = provider.GetRequiredService<FubonSDK>();
var logger = provider.GetRequiredService<ILogger<Program>>();
var loginResponse = sdk.Login(fubonSettings.Id, fubonSettings.Password, fubonSettings.CertPath, fubonSettings.CertPassword);
if (loginResponse.isSuccess != true || loginResponse.data[0] == null)
{
logger.LogError("Login failed.");
throw new Exception("Login failed.");
}
return loginResponse.data[0];
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();