Files
AI-Stock-Trader/WebServer/Program.cs
T
2026-03-11 14:34:31 -07:00

75 lines
2.5 KiB
C#

using WebServer.Components;
using Controllers.PythonInterop;
using Controllers.DataBase;
using Controllers.Payment;
using System.Runtime.InteropServices;
using Controllers.Automation;
// Load the module in globally and use correct path for local or docker runners
AIModule _aiModule;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
_aiModule = new AIModule("/opt/homebrew/bin//python3.11");
} else {
_aiModule = new AIModule();
}
// Load the Payment Processor
IPayment _paymentProcessor = new PaymentTestor();
if (args.Contains("Retrain-AI")) {
// This runs once per month on the first -> set by the crontab.txt
DbDriver _dbDriver = new DbDriver();
AutomationController auto = new AutomationController(_aiModule, _dbDriver, _paymentProcessor);
auto.GlobalTrainAI();
} else if (args.Contains("Perform-AI")) {
// This runs every hour that the stock market is open -> set by the crontab.txt
DbDriver _dbDriver = new DbDriver();
AutomationController auto = new AutomationController(_aiModule, _dbDriver, _paymentProcessor);
auto.GlobalPredictAI();
} else {
// This runs when the server is started normally
// Make sure the data is ready before first run
string firstPullRan = (new DbDriver()).Get("FirstPull");
if (firstPullRan != "1") {
_aiModule.PullAI();
_aiModule.TrainAI();
(new DbDriver()).Set("FirstPull", "1");
}
// Create the webapp
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
// Insert the DB driver for Dependency Injection
builder.Services.AddScoped<DbDriver>();
// Insert the payment Processor
builder.Services.AddSingleton<IPayment>(
_paymentProcessor
);
// Insert the python modlue for Dependency Injection
builder.Services.AddSingleton(_aiModule);
// Build the app
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) {
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
// Conffigure policies
app.UseHttpsRedirection();
app.UseAntiforgery();
app.MapStaticAssets();
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
// Start the web app
app.Run();
}