Files
gott/Program.cs
T
Meleki fa8deb15b6 feat: API-Keys-Verwaltung in DB statt appsettings
- Neue Tabelle api_keys (Provider, Name, ApiKey, IsActive)
- Admin-CRUD: GET/POST/PUT/DELETE /api/admin/api-keys
- Aktivieren/Deaktivieren pro Provider (nur einer aktiv)
- Live-Token-Update im AnthropicService ohne Neustart
- Migration: AnthropicToken aus appsettings.Production.json → DB
- AnthropicToken aus ServerSettings-Definitionen + appsettings entfernt
- Frontend: API-Keys-Seite mit Erstellen/Bearbeiten/Löschen/Toggle
- Dashboard: API-Keys-Link für Admins
2026-03-12 12:07:22 +01:00

131 lines
5.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.IO;
using System.Net.Http;
using System.Runtime.InteropServices;
using Gott.Endpoints;
using Gott.Middleware;
using Gott.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var builder = WebApplication.CreateBuilder(args);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
builder.Services.AddWindowsService();
builder.Services.AddHttpClient();
var dataDir = Path.Combine(AppContext.BaseDirectory, "data");
Directory.CreateDirectory(dataDir);
var dbPath = Path.Combine(dataDir, "gott.db");
var authService = new AuthService(dbPath);
var dbService = new DatabaseService(dbPath);
dbService.EnsureDefaultUser(authService);
builder.Services.AddSingleton(authService);
builder.Services.AddSingleton(dbService);
builder.Services.AddSingleton(new ImageService());
builder.Services.AddSingleton(new StreamingTracker());
builder.Services.AddSingleton(new RemoteConnectionManager());
var memoryService = new MemoryService(dataDir);
var skillService = new SkillService(dataDir);
builder.Services.AddSingleton(memoryService);
builder.Services.AddSingleton(skillService);
// Shared Services
var toolService = new ToolService(builder.Configuration);
builder.Services.AddSingleton(toolService);
var maxHistory = builder.Configuration.GetValue<int>("MaxHistory", 120);
var reminderInterval = builder.Configuration.GetValue<int>("ReminderInterval", 30);
var chatService = new ChatService(dbService, toolService, memoryService, skillService, maxHistory, reminderInterval);
builder.Services.AddSingleton(chatService);
// Embedding-Services (lokal, ONNX)
var embeddingService = new Gott.Services.EmbeddingService();
builder.Services.AddSingleton(embeddingService);
var vectorSearchService = new Gott.Services.VectorSearchService(dbPath, embeddingService);
builder.Services.AddSingleton(vectorSearchService);
var embeddingBgService = new Gott.Services.EmbeddingBackgroundService(vectorSearchService, embeddingService, dbPath);
builder.Services.AddSingleton(embeddingBgService);
builder.Services.AddHostedService(sp => sp.GetRequiredService<Gott.Services.EmbeddingBackgroundService>());
// AnthropicService (Singleton – shared zwischen ChatEndpoint und TelegramService)
// Token wird aus DB geladen (api_keys Tabelle), Fallback auf Config
var anthropicToken = "";
var activeKey = dbService.GetActiveApiKey("anthropic").GetAwaiter().GetResult();
if (activeKey != null)
anthropicToken = activeKey.ApiKey;
else if (!string.IsNullOrWhiteSpace(builder.Configuration["AnthropicToken"]))
anthropicToken = builder.Configuration["AnthropicToken"];
var payloadLogging = builder.Configuration.GetValue<bool>("PayloadLogging", false);
var payloadLoggingGzip = builder.Configuration.GetValue<bool>("PayloadLoggingGzip", false);
builder.Services.AddSingleton<AnthropicService>(sp =>
{
var svc = new AnthropicService(
sp.GetRequiredService<IHttpClientFactory>(),
anthropicToken, toolService, dbService, memoryService,
payloadLogging, payloadLoggingGzip,
vectorSearch: vectorSearchService,
embeddingBg: embeddingBgService);
svc.SetChatService(chatService);
return svc;
});
// OpenAI-kompatibler Service (Nvidia etc.)
builder.Services.AddSingleton<OpenAiService>(sp =>
new OpenAiService(sp.GetRequiredService<IHttpClientFactory>(), dbService, sp.GetRequiredService<ToolService>(), builder.Configuration, dataDir));
// Telegram Bot (pro Session – Token aus Session-Config)
var maxIterations = builder.Configuration.GetValue<int>("MaxIterations", 100);
builder.Services.AddSingleton<TelegramService>(sp =>
new TelegramService(
sp.GetRequiredService<IHttpClientFactory>(),
dbService, chatService,
sp.GetRequiredService<AnthropicService>(),
maxIterations));
// Krea Image Generation
var kreaToken = builder.Configuration["KreaToken"] ?? "";
builder.Services.AddSingleton(sp =>
new KreaService(sp.GetRequiredService<IHttpClientFactory>(), kreaToken, dataDir));
// Cronjob-Service (BackgroundService)
builder.Services.AddSingleton<CronjobService>();
builder.Services.AddHostedService<CronjobService>(sp => sp.GetRequiredService<CronjobService>());
var app = builder.Build();
app.Services.GetRequiredService<AnthropicService>().SetTelegramService(app.Services.GetRequiredService<TelegramService>());
await app.Services.GetRequiredService<OpenAiService>().LoadModelsAsync();
app.UseWebSockets();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMiddleware<AuthMiddleware>();
app.MapAuthEndpoints();
app.MapChatEndpoint();
app.MapHistoryEndpoint();
app.MapSessionEndpoints();
app.MapAgentEndpoints();
app.MapWorkspaceEndpoint();
app.MapInfoEndpoint();
app.MapModelsEndpoint();
app.MapBackupEndpoint();
app.MapSkillEndpoints();
app.MapProfileEndpoints();
app.MapSettingsEndpoints();
app.MapTelegramEndpoints();
app.MapCronjobEndpoints();
app.MapKreaEndpoints();
app.MapSharedEndpoints();
app.MapAdminEndpoints();
app.MapServerSettingsEndpoints();
app.MapApiKeysEndpoints();
app.MapOpenClawEndpoints();
app.MapRemoteEndpoints();
var port = app.Configuration.GetValue<int>("Port", 19000);
app.Run($"http://0.0.0.0:{port}");