Skip to content

Commit

Permalink
chore: refactor server configs
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan authored and urmauur committed Jan 17, 2024
1 parent 8eb5012 commit 2d9120f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
28 changes: 14 additions & 14 deletions electron/handlers/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
/**
Expand Down Expand Up @@ -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,
})
)

/**
Expand Down
38 changes: 22 additions & 16 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,33 @@ 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
* @param isVerboseEnabled - Flag to enable or disable verbose logging
* @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 {
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions web/screens/LocalServer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 2d9120f

Please sign in to comment.