feat : start login after start program, add appsetings
This commit is contained in:
22
Program.cs
22
Program.cs
@@ -1,18 +1,36 @@
|
||||
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>();
|
||||
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.
|
||||
|
Reference in New Issue
Block a user