74 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			74 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| /**
 | |
|  * API Server
 | |
|  * HTTP REST API compatible with Mailgun
 | |
|  */
 | |
| 
 | |
| import * as plugins from '../plugins.ts';
 | |
| 
 | |
| export interface IApiServerOptions {
 | |
|   port: number;
 | |
|   apiKeys: string[];
 | |
| }
 | |
| 
 | |
| export class ApiServer {
 | |
|   private server: Deno.HttpServer | null = null;
 | |
| 
 | |
|   constructor(private options: IApiServerOptions) {}
 | |
| 
 | |
|   /**
 | |
|    * Start the API server
 | |
|    */
 | |
|   async start(): Promise<void> {
 | |
|     console.log(`[ApiServer] Starting on port ${this.options.port}...`);
 | |
| 
 | |
|     this.server = Deno.serve({ port: this.options.port }, (req) => {
 | |
|       return this.handleRequest(req);
 | |
|     });
 | |
|   }
 | |
| 
 | |
|   /**
 | |
|    * Stop the API server
 | |
|    */
 | |
|   async stop(): Promise<void> {
 | |
|     console.log('[ApiServer] Stopping...');
 | |
|     if (this.server) {
 | |
|       await this.server.shutdown();
 | |
|       this.server = null;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   /**
 | |
|    * Handle incoming HTTP request
 | |
|    */
 | |
|   private async handleRequest(req: Request): Promise<Response> {
 | |
|     const url = new URL(req.url);
 | |
| 
 | |
|     // Basic routing
 | |
|     if (url.pathname === '/v1/messages' && req.method === 'POST') {
 | |
|       return this.handleSendEmail(req);
 | |
|     }
 | |
| 
 | |
|     if (url.pathname === '/v1/domains' && req.method === 'GET') {
 | |
|       return this.handleListDomains(req);
 | |
|     }
 | |
| 
 | |
|     return new Response('Not Found', { status: 404 });
 | |
|   }
 | |
| 
 | |
|   private async handleSendEmail(req: Request): Promise<Response> {
 | |
|     // TODO: Implement email sending
 | |
|     return new Response(JSON.stringify({ message: 'Email queued' }), {
 | |
|       status: 200,
 | |
|       headers: { 'Content-Type': 'application/json' },
 | |
|     });
 | |
|   }
 | |
| 
 | |
|   private async handleListDomains(req: Request): Promise<Response> {
 | |
|     // TODO: Implement domain listing
 | |
|     return new Response(JSON.stringify({ domains: [] }), {
 | |
|       status: 200,
 | |
|       headers: { 'Content-Type': 'application/json' },
 | |
|     });
 | |
|   }
 | |
| }
 |