import { Injectable, inject } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { firstValueFrom } from 'rxjs'; import { IApiResponse, IService, IServiceCreate, IServiceUpdate, ISystemStatus, IDomain, IDomainDetail, IDnsRecord, IRegistry, IRegistryCreate, IRegistryToken, ICreateTokenRequest, ITokenCreatedResponse, ISetting, ISettings, IPlatformService, IPlatformResource, TPlatformServiceType, INetworkTarget, INetworkStats, IContainerStats, IMetric, } from '../types/api.types'; @Injectable({ providedIn: 'root' }) export class ApiService { private http = inject(HttpClient); // System Status async getStatus(): Promise> { return firstValueFrom(this.http.get>('/api/status')); } // Services async getServices(): Promise> { return firstValueFrom(this.http.get>('/api/services')); } async getService(name: string): Promise> { return firstValueFrom(this.http.get>(`/api/services/${name}`)); } async createService(data: IServiceCreate): Promise> { return firstValueFrom(this.http.post>('/api/services', data)); } async updateService(name: string, data: IServiceUpdate): Promise> { return firstValueFrom(this.http.put>(`/api/services/${name}`, data)); } async deleteService(name: string): Promise> { return firstValueFrom(this.http.delete>(`/api/services/${name}`)); } async startService(name: string): Promise> { return firstValueFrom(this.http.post>(`/api/services/${name}/start`, {})); } async stopService(name: string): Promise> { return firstValueFrom(this.http.post>(`/api/services/${name}/stop`, {})); } async restartService(name: string): Promise> { return firstValueFrom(this.http.post>(`/api/services/${name}/restart`, {})); } async getServiceLogs(name: string): Promise> { return firstValueFrom(this.http.get>(`/api/services/${name}/logs`)); } async getServiceStats(name: string): Promise> { return firstValueFrom(this.http.get>(`/api/services/${name}/stats`)); } async getServiceMetrics(name: string, limit?: number): Promise> { const params = limit ? `?limit=${limit}` : ''; return firstValueFrom(this.http.get>(`/api/services/${name}/metrics${params}`)); } // Registries async getRegistries(): Promise> { return firstValueFrom(this.http.get>('/api/registries')); } async createRegistry(data: IRegistryCreate): Promise> { return firstValueFrom(this.http.post>('/api/registries', data)); } async deleteRegistry(id: number): Promise> { return firstValueFrom(this.http.delete>(`/api/registries/${id}`)); } // Registry Tokens async getRegistryTokens(): Promise> { return firstValueFrom(this.http.get>('/api/registry/tokens')); } async createRegistryToken(data: ICreateTokenRequest): Promise> { return firstValueFrom(this.http.post>('/api/registry/tokens', data)); } async deleteRegistryToken(id: number): Promise> { return firstValueFrom(this.http.delete>(`/api/registry/tokens/${id}`)); } // DNS Records async getDnsRecords(): Promise> { return firstValueFrom(this.http.get>('/api/dns')); } async createDnsRecord(domain: string, ip?: string): Promise> { return firstValueFrom(this.http.post>('/api/dns', { domain, ip })); } async deleteDnsRecord(domain: string): Promise> { return firstValueFrom(this.http.delete>(`/api/dns/${domain}`)); } async syncDnsRecords(): Promise> { return firstValueFrom(this.http.post>('/api/dns/sync', {})); } // Domains async getDomains(): Promise> { return firstValueFrom(this.http.get>('/api/domains')); } async getDomainDetail(domain: string): Promise> { return firstValueFrom(this.http.get>(`/api/domains/${domain}`)); } async syncCloudflareDomains(): Promise> { return firstValueFrom(this.http.post>('/api/domains/sync', {})); } // SSL Certificates async obtainCertificate(domain: string, includeWildcard?: boolean): Promise> { return firstValueFrom( this.http.post>('/api/ssl/obtain', { domain, includeWildcard }) ); } async renewCertificate(domain: string): Promise> { return firstValueFrom(this.http.post>(`/api/ssl/${domain}/renew`, {})); } // Settings async getSettings(): Promise> { return firstValueFrom(this.http.get>('/api/settings')); } async updateSettings(settings: Record | ISettings): Promise> { return firstValueFrom(this.http.put>('/api/settings', settings)); } async updateSetting(key: string, value: string): Promise> { return firstValueFrom(this.http.put>('/api/settings', { key, value })); } // Auth async changePassword(currentPassword: string, newPassword: string): Promise> { return firstValueFrom( this.http.post>('/api/auth/change-password', { currentPassword, newPassword, }) ); } // Platform Services async getPlatformServices(): Promise> { return firstValueFrom(this.http.get>('/api/platform-services')); } async getPlatformService(type: TPlatformServiceType): Promise> { return firstValueFrom(this.http.get>(`/api/platform-services/${type}`)); } async startPlatformService(type: TPlatformServiceType): Promise> { return firstValueFrom(this.http.post>(`/api/platform-services/${type}/start`, {})); } async stopPlatformService(type: TPlatformServiceType): Promise> { return firstValueFrom(this.http.post>(`/api/platform-services/${type}/stop`, {})); } async getPlatformServiceStats(type: TPlatformServiceType): Promise> { return firstValueFrom(this.http.get>(`/api/platform-services/${type}/stats`)); } async getServicePlatformResources(serviceName: string): Promise> { return firstValueFrom(this.http.get>(`/api/services/${serviceName}/platform-resources`)); } // Network async getNetworkTargets(): Promise> { return firstValueFrom(this.http.get>('/api/network/targets')); } async getNetworkStats(): Promise> { return firstValueFrom(this.http.get>('/api/network/stats')); } }