chore: refactor server configs

This commit is contained in:
Louis 2024-01-17 12:43:52 +07:00 committed by Faisal Amir
parent 8eb5012aea
commit 2d9120f34a
3 changed files with 39 additions and 33 deletions

View File

@ -4,7 +4,7 @@ import { WindowManager } from './../managers/window'
import { getResourcePath, userSpacePath } from './../utils/path' import { getResourcePath, userSpacePath } from './../utils/path'
import { AppRoute } from '@janhq/core' import { AppRoute } from '@janhq/core'
import { ModuleManager, init, log } from '@janhq/core/node' import { ModuleManager, init, log } from '@janhq/core/node'
import { startServer, stopServer } from '@janhq/server' import { ServerConfig, startServer, stopServer } from '@janhq/server'
export function handleAppIPCs() { export function handleAppIPCs() {
/** /**
@ -51,19 +51,19 @@ export function handleAppIPCs() {
/** /**
* Start Jan API Server. * Start Jan API Server.
*/ */
ipcMain.handle( ipcMain.handle(AppRoute.startServer, async (_event, configs?: ServerConfig) =>
AppRoute.startServer, startServer({
async (_event, host, port, isCorsEnabled, isVerbose) => host: configs?.host,
startServer( port: configs?.port,
host, isCorsEnabled: configs?.isCorsEnabled,
port, isVerboseEnabled: configs?.isVerboseEnabled,
isCorsEnabled, schemaPath: app.isPackaged
isVerbose,
app.isPackaged
? join(getResourcePath(), 'docs', 'openapi', 'jan.yaml') ? join(getResourcePath(), 'docs', 'openapi', 'jan.yaml')
: undefined, : undefined,
app.isPackaged ? join(getResourcePath(), 'docs', 'openapi') : undefined baseDir: app.isPackaged
) ? join(getResourcePath(), 'docs', 'openapi')
: undefined,
})
) )
/** /**

View File

@ -21,7 +21,7 @@ let corsEnbaled: boolean = true;
let isVerbose: boolean = true; let isVerbose: boolean = true;
/** /**
* Function to start the server * Server configurations
* @param host - The host address for the server * @param host - The host address for the server
* @param port - The port number for the server * @param port - The port number for the server
* @param isCorsEnabled - Flag to enable or disable CORS * @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 schemaPath - Path to the OpenAPI schema file
* @param baseDir - Base directory for the OpenAPI schema file * @param baseDir - Base directory for the OpenAPI schema file
*/ */
export const startServer = async ( export interface ServerConfig {
host?: string, host?: string;
port?: number, port?: number;
isCorsEnabled?: boolean, isCorsEnabled?: boolean;
isVerboseEnabled?: boolean, isVerboseEnabled?: boolean;
schemaPath?: string, schemaPath?: string;
baseDir?: string baseDir?: string;
) => { }
/**
* Function to start the server
* @param configs - Server configurations
*/
export const startServer = async (configs?: ServerConfig) => {
// Update server settings // Update server settings
isVerbose = isVerboseEnabled ?? true; isVerbose = configs?.isVerboseEnabled ?? true;
hostSetting = host ?? JAN_API_HOST; hostSetting = configs?.host ?? JAN_API_HOST;
portSetting = port ?? JAN_API_PORT; portSetting = configs?.port ?? JAN_API_PORT;
corsEnbaled = isCorsEnabled ?? true; corsEnbaled = configs?.isCorsEnabled ?? true;
// Start the server // Start the server
try { try {
@ -64,15 +70,15 @@ export const startServer = async (
await server.register(require("@fastify/swagger"), { await server.register(require("@fastify/swagger"), {
mode: "static", mode: "static",
specification: { specification: {
path: schemaPath ?? "./../docs/openapi/jan.yaml", path: configs?.schemaPath ?? "./../docs/openapi/jan.yaml",
baseDir: baseDir ?? "./../docs/openapi", baseDir: configs?.baseDir ?? "./../docs/openapi",
}, },
}); });
// Register Swagger UI // Register Swagger UI
await server.register(require("@fastify/swagger-ui"), { await server.register(require("@fastify/swagger-ui"), {
routePrefix: "/", routePrefix: "/",
baseDir: baseDir ?? path.join(__dirname, "../..", "./docs/openapi"), baseDir: configs?.baseDir ?? path.join(__dirname, "../..", "./docs/openapi"),
uiConfig: { uiConfig: {
docExpansion: "full", docExpansion: "full",
deepLinking: false, deepLinking: false,

View File

@ -109,12 +109,12 @@ const LocalServerScreen = () => {
if (!activeModel) { if (!activeModel) {
startModel(String(selectedModel?.id)) startModel(String(selectedModel?.id))
} }
window.core?.api?.startServer( window.core?.api?.startServer({
host, host,
port, port,
isCorsEnabled, isCorsEnabled,
isVerboseEnabled isVerboseEnabled,
) })
setServerEnabled(true) setServerEnabled(true)
if (firstTimeVisitAPIServer) { if (firstTimeVisitAPIServer) {
localStorage.setItem(FIRST_TIME_VISIT_API_SERVER, 'false') localStorage.setItem(FIRST_TIME_VISIT_API_SERVER, 'false')