Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions apps/desktop/src/main/managers/http-api-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { createHTTPHandler } from "@trpc/server/adapters/standalone";
import * as http from "node:http";
import type { Server } from "node:http";
import { logger } from "../logger";
import { router } from "../../trpc/router";
import { createContext } from "../../trpc/context";
import type { ServiceManager } from "./service-manager";

const DEFAULT_PORT = 21417;

/**
* Manages the localhost HTTP API server for external integrations
* Exposes the tRPC router over HTTP on localhost only
*/
export class HttpApiManager {
private server: Server | null = null;
private serviceManager: ServiceManager;
private port: number;

constructor(serviceManager: ServiceManager, port: number = DEFAULT_PORT) {
this.serviceManager = serviceManager;
this.port = port;
}

async start(): Promise<void> {
if (this.server) {
logger.main.warn("HTTP API server is already running");
return;
}

try {
const trpcHandler = createHTTPHandler({
router,
createContext: async () => createContext(this.serviceManager),
});

this.server = http.createServer((req, res) => {
// CORS headers for local development
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, OPTIONS"
);
res.setHeader(
"Access-Control-Allow-Headers",
"Content-Type, Authorization"
);

// Handle preflight requests
if (req.method === "OPTIONS") {
res.writeHead(200);
res.end();
return;
}

trpcHandler(req, res);
});

// Listen only on localhost for security
await new Promise<void>((resolve, reject) => {
this.server!.listen(this.port, "127.0.0.1", () => {
logger.main.info(`HTTP API server started on http://127.0.0.1:${this.port}`);
resolve();
});

this.server!.on("error", (error: NodeJS.ErrnoException) => {
if (error.code === "EADDRINUSE") {
logger.main.warn(`Port ${this.port} is already in use, HTTP API server not started`);
this.server = null;
resolve(); // Don't fail, just skip
} else {
reject(error);
}
});
});
} catch (error) {
logger.main.error("Failed to start HTTP API server:", error);
throw error;
}
}

stop(): void {
if (this.server) {
this.server.close(() => {
logger.main.info("HTTP API server stopped");
});
this.server = null;
}
}

isRunning(): boolean {
return this.server !== null;
}

getPort(): number {
return this.port;
}
}
20 changes: 20 additions & 0 deletions apps/desktop/src/main/managers/service-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { isMacOS, isWindows } from "../../utils/platform";
import { TelemetryService } from "../../services/telemetry-service";
import { AuthService } from "../../services/auth-service";
import { OnboardingService } from "../../services/onboarding-service";
import { HttpApiManager } from "./http-api-manager";

/**
* Service map for type-safe service access
Expand All @@ -29,6 +30,7 @@ export interface ServiceMap {
shortcutManager: ShortcutManager;
windowManager: WindowManager;
onboardingService: OnboardingService;
httpApiManager: HttpApiManager;
}

/**
Expand All @@ -51,6 +53,7 @@ export class ServiceManager {
private recordingManager: RecordingManager | null = null;
private shortcutManager: ShortcutManager | null = null;
private windowManager: WindowManager | null = null;
private httpApiManager: HttpApiManager | null = null;

async initialize(): Promise<void> {
if (this.isInitialized) {
Expand All @@ -72,6 +75,7 @@ export class ServiceManager {
this.initializeRecordingManager();
await this.initializeShortcutManager();
this.initializeAutoUpdater();
await this.initializeHttpApiManager();

this.isInitialized = true;
logger.main.info("Services initialized successfully");
Expand Down Expand Up @@ -200,6 +204,17 @@ export class ServiceManager {
this.autoUpdaterService = new AutoUpdaterService();
}

private async initializeHttpApiManager(): Promise<void> {
try {
this.httpApiManager = new HttpApiManager(this);
await this.httpApiManager.start();
logger.main.info("HTTP API manager initialized");
} catch (error) {
logger.main.warn("Failed to initialize HTTP API manager:", error);
// Don't throw - HTTP API is not critical for basic functionality
}
}

getLogger() {
return logger;
}
Expand All @@ -224,12 +239,17 @@ export class ServiceManager {
shortcutManager: this.shortcutManager!,
windowManager: this.windowManager!,
onboardingService: this.onboardingService!,
httpApiManager: this.httpApiManager!,
};

return services[serviceName];
}

async cleanup(): Promise<void> {
if (this.httpApiManager) {
logger.main.info("Stopping HTTP API server...");
this.httpApiManager.stop();
}
if (this.shortcutManager) {
logger.main.info("Cleaning up shortcut manager...");
this.shortcutManager.cleanup();
Expand Down