diff --git a/electron/handlers/app.ts b/electron/handlers/app.ts index 5af222d64..82ac634f0 100644 --- a/electron/handlers/app.ts +++ b/electron/handlers/app.ts @@ -4,7 +4,7 @@ import { WindowManager } from './../managers/window' import { getResourcePath, userSpacePath } from './../utils/path' import { AppRoute } from '@janhq/core' import { ModuleManager, init, log } from '@janhq/core/node' -import { startServer, stopServer } from '@janhq/server' +import { ServerConfig, startServer, stopServer } from '@janhq/server' export function handleAppIPCs() { /** @@ -51,19 +51,19 @@ export function handleAppIPCs() { /** * Start Jan API Server. */ - ipcMain.handle( - AppRoute.startServer, - async (_event, host, port, isCorsEnabled, isVerbose) => - startServer( - host, - port, - isCorsEnabled, - isVerbose, - app.isPackaged - ? join(getResourcePath(), 'docs', 'openapi', 'jan.yaml') - : undefined, - app.isPackaged ? join(getResourcePath(), 'docs', 'openapi') : undefined - ) + ipcMain.handle(AppRoute.startServer, async (_event, configs?: ServerConfig) => + startServer({ + host: configs?.host, + port: configs?.port, + isCorsEnabled: configs?.isCorsEnabled, + isVerboseEnabled: configs?.isVerboseEnabled, + schemaPath: app.isPackaged + ? join(getResourcePath(), 'docs', 'openapi', 'jan.yaml') + : undefined, + baseDir: app.isPackaged + ? join(getResourcePath(), 'docs', 'openapi') + : undefined, + }) ) /** diff --git a/server/index.ts b/server/index.ts index 70d43f482..11676e2af 100644 --- a/server/index.ts +++ b/server/index.ts @@ -21,7 +21,7 @@ let corsEnbaled: boolean = true; let isVerbose: boolean = true; /** - * Function to start the server + * Server configurations * @param host - The host address for the server * @param port - The port number for the server * @param isCorsEnabled - Flag to enable or disable CORS @@ -29,19 +29,25 @@ let isVerbose: boolean = true; * @param schemaPath - Path to the OpenAPI schema file * @param baseDir - Base directory for the OpenAPI schema file */ -export const startServer = async ( - host?: string, - port?: number, - isCorsEnabled?: boolean, - isVerboseEnabled?: boolean, - schemaPath?: string, - baseDir?: string -) => { +export interface ServerConfig { + host?: string; + port?: number; + isCorsEnabled?: boolean; + isVerboseEnabled?: boolean; + schemaPath?: string; + baseDir?: string; +} + +/** + * Function to start the server +* @param configs - Server configurations + */ +export const startServer = async (configs?: ServerConfig) => { // Update server settings - isVerbose = isVerboseEnabled ?? true; - hostSetting = host ?? JAN_API_HOST; - portSetting = port ?? JAN_API_PORT; - corsEnbaled = isCorsEnabled ?? true; + isVerbose = configs?.isVerboseEnabled ?? true; + hostSetting = configs?.host ?? JAN_API_HOST; + portSetting = configs?.port ?? JAN_API_PORT; + corsEnbaled = configs?.isCorsEnabled ?? true; // Start the server try { @@ -64,15 +70,15 @@ export const startServer = async ( await server.register(require("@fastify/swagger"), { mode: "static", specification: { - path: schemaPath ?? "./../docs/openapi/jan.yaml", - baseDir: baseDir ?? "./../docs/openapi", + path: configs?.schemaPath ?? "./../docs/openapi/jan.yaml", + baseDir: configs?.baseDir ?? "./../docs/openapi", }, }); // Register Swagger UI await server.register(require("@fastify/swagger-ui"), { routePrefix: "/", - baseDir: baseDir ?? path.join(__dirname, "../..", "./docs/openapi"), + baseDir: configs?.baseDir ?? path.join(__dirname, "../..", "./docs/openapi"), uiConfig: { docExpansion: "full", deepLinking: false, diff --git a/web/screens/LocalServer/index.tsx b/web/screens/LocalServer/index.tsx index a9c7de599..5927cee19 100644 --- a/web/screens/LocalServer/index.tsx +++ b/web/screens/LocalServer/index.tsx @@ -109,12 +109,12 @@ const LocalServerScreen = () => { if (!activeModel) { startModel(String(selectedModel?.id)) } - window.core?.api?.startServer( + window.core?.api?.startServer({ host, port, isCorsEnabled, - isVerboseEnabled - ) + isVerboseEnabled, + }) setServerEnabled(true) if (firstTimeVisitAPIServer) { localStorage.setItem(FIRST_TIME_VISIT_API_SERVER, 'false')