- Updated .gitlab-ci.yml with complete build, test, and deploy stages
- Added authentication redirect fix in Program.cs (302 redirect for admin routes)
- Fixed Cookie vs Bearer authentication conflict for admin panel
- Configure pipeline to build from .NET 9.0 source
- Deploy to Hostinger VPS with proper environment variables
- Include rollback capability for production deployments
🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
166 lines
6.2 KiB
C#
166 lines
6.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using Hangfire;
|
|
using Hangfire.LiteDB;
|
|
using LittleShop.Client.Extensions;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Serilog;
|
|
using Serilog.Events;
|
|
using TeleBot;
|
|
using TeleBot.Handlers;
|
|
using TeleBot.Services;
|
|
using TeleBot.Http;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
var BrandName = "Little Shop";
|
|
// Configuration
|
|
builder.Configuration
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
|
|
.AddEnvironmentVariables();
|
|
|
|
// Add MVC Controllers for webhook endpoints
|
|
builder.Services.AddControllers();
|
|
|
|
// Serilog
|
|
Log.Logger = new LoggerConfiguration()
|
|
.MinimumLevel.Information()
|
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
|
|
.Enrich.FromLogContext()
|
|
.WriteTo.Console()
|
|
.WriteTo.File("logs/telebot-.txt", rollingInterval: RollingInterval.Day)
|
|
.CreateLogger();
|
|
|
|
builder.Logging.ClearProviders();
|
|
builder.Logging.AddSerilog();
|
|
|
|
// Services
|
|
builder.Services.AddSingleton<IPrivacyService, PrivacyService>();
|
|
builder.Services.AddSingleton<SessionManager>();
|
|
builder.Services.AddSingleton<ISessionManager>(provider => provider.GetRequiredService<SessionManager>());
|
|
builder.Services.AddHostedService<SessionManager>(provider => provider.GetRequiredService<SessionManager>());
|
|
|
|
// LittleShop Client with TOR support
|
|
builder.Services.AddLittleShopClient(options =>
|
|
{
|
|
var config = builder.Configuration;
|
|
options.BaseUrl = config["LittleShop:ApiUrl"] ?? "https://localhost:5001";
|
|
options.TimeoutSeconds = 30;
|
|
options.MaxRetryAttempts = 3;
|
|
|
|
// Set the brand name globally
|
|
BotConfig.BrandName = config["LittleShop:BrandName"] ?? "Little Shop";
|
|
},
|
|
// Pass TOR configuration
|
|
useTorProxy: builder.Configuration.GetValue<bool>("LittleShop:UseTor"),
|
|
torSocksPort: builder.Configuration.GetValue<int>("Privacy:TorSocksPort", 9050));
|
|
|
|
builder.Services.AddSingleton<ILittleShopService, LittleShopService>();
|
|
|
|
// Redis (if enabled)
|
|
if (builder.Configuration.GetValue<bool>("Redis:Enabled"))
|
|
{
|
|
builder.Services.AddStackExchangeRedisCache(options =>
|
|
{
|
|
options.Configuration = builder.Configuration["Redis:ConnectionString"] ?? "localhost:6379";
|
|
options.InstanceName = builder.Configuration["Redis:InstanceName"] ?? "TeleBot";
|
|
});
|
|
}
|
|
|
|
// Hangfire (if enabled)
|
|
if (builder.Configuration.GetValue<bool>("Hangfire:Enabled"))
|
|
{
|
|
var hangfireDb = builder.Configuration["Hangfire:DatabasePath"] ?? "hangfire.db";
|
|
builder.Services.AddHangfire(config =>
|
|
{
|
|
config.UseLiteDbStorage(hangfireDb);
|
|
});
|
|
builder.Services.AddHangfireServer();
|
|
}
|
|
|
|
// Bot Handlers
|
|
builder.Services.AddSingleton<ICommandHandler, CommandHandler>();
|
|
builder.Services.AddSingleton<ICallbackHandler, CallbackHandler>();
|
|
builder.Services.AddSingleton<IMessageHandler, MessageHandler>();
|
|
|
|
// Bot Manager Service (for registration and metrics) - Single instance with TOR support
|
|
builder.Services.AddHttpClient<BotManagerService>()
|
|
.ConfigurePrimaryHttpMessageHandler(sp =>
|
|
{
|
|
var config = sp.GetRequiredService<IConfiguration>();
|
|
var logger = sp.GetRequiredService<ILoggerFactory>().CreateLogger("TOR.BotManager");
|
|
return Socks5HttpHandler.Create(config, logger);
|
|
});
|
|
builder.Services.AddSingleton<BotManagerService>();
|
|
builder.Services.AddHostedService(provider => provider.GetRequiredService<BotManagerService>());
|
|
|
|
// Message Delivery Service - Single instance
|
|
builder.Services.AddSingleton<MessageDeliveryService>();
|
|
builder.Services.AddSingleton<IMessageDeliveryService>(sp => sp.GetRequiredService<MessageDeliveryService>());
|
|
builder.Services.AddHostedService<MessageDeliveryService>(sp => sp.GetRequiredService<MessageDeliveryService>());
|
|
|
|
// Bot Activity Tracking with TOR support
|
|
builder.Services.AddHttpClient<IBotActivityTracker, BotActivityTracker>()
|
|
.ConfigurePrimaryHttpMessageHandler(sp =>
|
|
{
|
|
var config = sp.GetRequiredService<IConfiguration>();
|
|
var logger = sp.GetRequiredService<ILoggerFactory>().CreateLogger("TOR.ActivityTracker");
|
|
return Socks5HttpHandler.Create(config, logger);
|
|
});
|
|
|
|
// Product Carousel Service with TOR support
|
|
builder.Services.AddHttpClient<ProductCarouselService>()
|
|
.ConfigurePrimaryHttpMessageHandler(sp =>
|
|
{
|
|
var config = sp.GetRequiredService<IConfiguration>();
|
|
var logger = sp.GetRequiredService<ILoggerFactory>().CreateLogger("TOR.Carousel");
|
|
return Socks5HttpHandler.Create(config, logger);
|
|
});
|
|
builder.Services.AddSingleton<IProductCarouselService, ProductCarouselService>();
|
|
|
|
// Bot Service - Single instance
|
|
builder.Services.AddSingleton<TelegramBotService>();
|
|
builder.Services.AddHostedService(provider => provider.GetRequiredService<TelegramBotService>());
|
|
|
|
// Build the application
|
|
var app = builder.Build();
|
|
|
|
// Connect the services
|
|
var botManagerService = app.Services.GetRequiredService<BotManagerService>();
|
|
var telegramBotService = app.Services.GetRequiredService<TelegramBotService>();
|
|
botManagerService.SetTelegramBotService(telegramBotService);
|
|
|
|
// Configure the HTTP request pipeline
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
|
|
app.UseRouting();
|
|
app.MapControllers();
|
|
|
|
try
|
|
{
|
|
Log.Information("Starting TeleBot - Privacy-First E-Commerce Bot");
|
|
Log.Information("Privacy Mode: {PrivacyMode}", builder.Configuration["Privacy:Mode"]);
|
|
Log.Information("Ephemeral by Default: {Ephemeral}", builder.Configuration["Privacy:EphemeralByDefault"]);
|
|
Log.Information("Tor Enabled: {Tor}", builder.Configuration["Privacy:EnableTor"]);
|
|
Log.Information("Webhook endpoints available at /api/webhook");
|
|
|
|
await app.RunAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Fatal(ex, "Application start-up failed");
|
|
}
|
|
finally
|
|
{
|
|
Log.CloseAndFlush();
|
|
} |