feat : start login after start program, add appsetings

This commit is contained in:
2025-08-01 16:58:14 +08:00
parent 6a82898765
commit 0dc7e05029
5 changed files with 46 additions and 15 deletions

View File

@@ -9,7 +9,7 @@ namespace fubon_api.Controllers;
public class FubonSdkAccountController : ControllerBase public class FubonSdkAccountController : ControllerBase
{ {
private readonly FubonSDK _sdk; private readonly FubonSDK _sdk;
private Account _account; private readonly Account _account;
private readonly ILogger<FubonSdkAccountController> _logger; private readonly ILogger<FubonSdkAccountController> _logger;
public FubonSdkAccountController(ILogger<FubonSdkAccountController> logger, FubonSDK sdk, Account account) public FubonSdkAccountController(ILogger<FubonSdkAccountController> logger, FubonSDK sdk, Account account)
@@ -19,18 +19,10 @@ public class FubonSdkAccountController : ControllerBase
_logger = logger; _logger = logger;
} }
[HttpPost("Login")] [HttpGet("Account")]
public LoginResponse Login([FromBody] LoginRequest request) public Account GetAccount()
{ {
if (request == null) return _account;
{
throw new ArgumentException("Invalid login request.");
}
var result = _sdk.Login(request.Id, request.Password, "Your Cert Path", "Your Cert Password");
_account = result.data[0];
return result;
} }
[HttpGet("Inventories")] [HttpGet("Inventories")]

9
Models/FubonSettings.cs Normal file
View File

@@ -0,0 +1,9 @@
namespace fubon_api.Models;
public class FubonSettings
{
public string Id { get; set; }
public string Password { get; set; }
public string CertPath { get; set; }
public string CertPassword{ get; set; }
}

View File

@@ -1,18 +1,36 @@
using fubon_api.Controllers; using fubon_api.Controllers;
using fubon_api.Models;
using FubonNeo.Sdk; using FubonNeo.Sdk;
var builder = WebApplication.CreateBuilder(args); 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. // Add services to the container.
builder.Services.AddControllers(); builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<FubonSDK>(); 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(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.

View File

@@ -4,5 +4,11 @@
"Default": "Information", "Default": "Information",
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
},
"FubonSettings":{
"Id": "UserId",
"Password": "Password",
"CertPath": "./static",
"CertPassword":"CertPassword"
} }
} }

View File

@@ -5,5 +5,11 @@
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, },
"FubonSettings":{
"Id": "UserId",
"Password": "Password",
"CertPath": "./static",
"CertPassword":"CertPassword"
},
"AllowedHosts": "*" "AllowedHosts": "*"
} }