forked from janhq/jan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
141 lines (126 loc) · 3.71 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import fastify from "fastify";
import dotenv from "dotenv";
import {
getServerLogPath,
v1Router,
logServer,
getJanExtensionsPath,
} from "@janhq/core/node";
import { join } from "path";
// Load environment variables
dotenv.config();
// Define default settings
const JAN_API_HOST = process.env.JAN_API_HOST || "127.0.0.1";
const JAN_API_PORT = Number.parseInt(process.env.JAN_API_PORT || "1337");
// Initialize server settings
let server: any | undefined = undefined;
let hostSetting: string = JAN_API_HOST;
let portSetting: number = JAN_API_PORT;
let corsEnabled: boolean = true;
let isVerbose: boolean = true;
/**
* 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 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 = configs?.isVerboseEnabled ?? true;
hostSetting = configs?.host ?? JAN_API_HOST;
portSetting = configs?.port ?? JAN_API_PORT;
corsEnabled = configs?.isCorsEnabled ?? true;
const serverLogPath = getServerLogPath();
// Start the server
try {
// Log server start
if (isVerbose) logServer(`Debug: Starting JAN API server...`);
// Initialize Fastify server with logging
server = fastify({
logger: {
level: "info",
file: serverLogPath,
},
});
// Register CORS if enabled
if (corsEnabled) await server.register(require("@fastify/cors"), {});
// Register Swagger for API documentation
await server.register(require("@fastify/swagger"), {
mode: "static",
specification: {
path: configs?.schemaPath ?? "./../docs/openapi/jan.yaml",
baseDir: configs?.baseDir ?? "./../docs/openapi",
},
});
// Register Swagger UI
await server.register(require("@fastify/swagger-ui"), {
routePrefix: "/",
baseDir: configs?.baseDir ?? join(__dirname, "../..", "./docs/openapi"),
uiConfig: {
docExpansion: "full",
deepLinking: false,
},
staticCSP: false,
transformSpecificationClone: true,
});
// Register static file serving for extensions
// TODO: Watch extension files changes and reload
await server.register(
(childContext: any, _: any, done: any) => {
childContext.register(require("@fastify/static"), {
root: getJanExtensionsPath(),
wildcard: false,
});
done();
},
{ prefix: "extensions" }
);
// Register API routes
await server.register(v1Router, { prefix: "/v1" });
// Start listening for requests
await server
.listen({
port: portSetting,
host: hostSetting,
})
.then(() => {
// Log server listening
if (isVerbose)
logServer(
`Debug: JAN API listening at: http://${hostSetting}:${portSetting}`
);
});
} catch (e) {
// Log any errors
if (isVerbose) logServer(`Error: ${e}`);
}
};
/**
* Function to stop the server
*/
export const stopServer = async () => {
try {
// Log server stop
if (isVerbose) logServer(`Debug: Server stopped`);
// Stop the server
await server.close();
} catch (e) {
// Log any errors
if (isVerbose) logServer(`Error: ${e}`);
}
};