84 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
		
		
			
		
	
	
			84 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * Configuration Manager
							 | 
						||
| 
								 | 
							
								 * Handles configuration storage and retrieval
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import * as plugins from '../plugins.ts';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								export interface IMailerConfig {
							 | 
						||
| 
								 | 
							
								  domains: IDomainConfig[];
							 | 
						||
| 
								 | 
							
								  apiKeys: string[];
							 | 
						||
| 
								 | 
							
								  smtpPort: number;
							 | 
						||
| 
								 | 
							
								  apiPort: number;
							 | 
						||
| 
								 | 
							
								  hostname: string;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								export interface IDomainConfig {
							 | 
						||
| 
								 | 
							
								  domain: string;
							 | 
						||
| 
								 | 
							
								  dnsMode: 'forward' | 'internal-dns' | 'external-dns';
							 | 
						||
| 
								 | 
							
								  cloudflare?: {
							 | 
						||
| 
								 | 
							
								    apiToken: string;
							 | 
						||
| 
								 | 
							
								  };
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								export class ConfigManager {
							 | 
						||
| 
								 | 
							
								  private configPath: string;
							 | 
						||
| 
								 | 
							
								  private config: IMailerConfig | null = null;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  constructor(configPath?: string) {
							 | 
						||
| 
								 | 
							
								    this.configPath = configPath || plugins.path.join(Deno.env.get('HOME') || '/root', '.mailer', 'config.json');
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  /**
							 | 
						||
| 
								 | 
							
								   * Load configuration from disk
							 | 
						||
| 
								 | 
							
								   */
							 | 
						||
| 
								 | 
							
								  async load(): Promise<IMailerConfig> {
							 | 
						||
| 
								 | 
							
								    try {
							 | 
						||
| 
								 | 
							
								      const data = await Deno.readTextFile(this.configPath);
							 | 
						||
| 
								 | 
							
								      this.config = JSON.parse(data);
							 | 
						||
| 
								 | 
							
								      return this.config!;
							 | 
						||
| 
								 | 
							
								    } catch (error) {
							 | 
						||
| 
								 | 
							
								      // Return default config if file doesn't exist
							 | 
						||
| 
								 | 
							
								      this.config = this.getDefaultConfig();
							 | 
						||
| 
								 | 
							
								      return this.config;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  /**
							 | 
						||
| 
								 | 
							
								   * Save configuration to disk
							 | 
						||
| 
								 | 
							
								   */
							 | 
						||
| 
								 | 
							
								  async save(config: IMailerConfig): Promise<void> {
							 | 
						||
| 
								 | 
							
								    this.config = config;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // Ensure directory exists
							 | 
						||
| 
								 | 
							
								    const dir = plugins.path.dirname(this.configPath);
							 | 
						||
| 
								 | 
							
								    await Deno.mkdir(dir, { recursive: true });
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // Write config
							 | 
						||
| 
								 | 
							
								    await Deno.writeTextFile(this.configPath, JSON.stringify(config, null, 2));
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  /**
							 | 
						||
| 
								 | 
							
								   * Get current configuration
							 | 
						||
| 
								 | 
							
								   */
							 | 
						||
| 
								 | 
							
								  getConfig(): IMailerConfig {
							 | 
						||
| 
								 | 
							
								    if (!this.config) {
							 | 
						||
| 
								 | 
							
								      throw new Error('Configuration not loaded. Call load() first.');
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    return this.config;
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  /**
							 | 
						||
| 
								 | 
							
								   * Get default configuration
							 | 
						||
| 
								 | 
							
								   */
							 | 
						||
| 
								 | 
							
								  private getDefaultConfig(): IMailerConfig {
							 | 
						||
| 
								 | 
							
								    return {
							 | 
						||
| 
								 | 
							
								      domains: [],
							 | 
						||
| 
								 | 
							
								      apiKeys: [],
							 | 
						||
| 
								 | 
							
								      smtpPort: 25,
							 | 
						||
| 
								 | 
							
								      apiPort: 8080,
							 | 
						||
| 
								 | 
							
								      hostname: 'localhost',
							 | 
						||
| 
								 | 
							
								    };
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								}
							 |