forked from janhq/jan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
159 lines (140 loc) · 4.21 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import fastify from "fastify";
import dotenv from "dotenv";
import { log, v1Router } from "@janhq/core/node";
import path from "path";
import os from "os";
// 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");
const serverLogPath = path.join(os.homedir(), "jan", "logs", "server.log");
// Initialize server settings
let server: any | undefined = undefined;
let hostSetting: string = JAN_API_HOST;
let portSetting: number = JAN_API_PORT;
let corsEnbaled: boolean = true;
let isVerbose: boolean = true;
/**
* Function to start the server
* @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
) => {
// Update server settings
isVerbose = isVerboseEnabled ?? true;
hostSetting = host ?? JAN_API_HOST;
portSetting = port ?? JAN_API_PORT;
corsEnbaled = isCorsEnabled ?? true;
// Start the server
try {
// Log server start
if (isVerbose)
log(`[API]::Debug: Starting JAN API server...`, "server.log");
// Initialize Fastify server with logging
server = fastify({
logger: {
level: "info",
file: serverLogPath,
},
});
// Register CORS if enabled
if (corsEnbaled) await server.register(require("@fastify/cors"), {});
// Register Swagger for API documentation
await server.register(require("@fastify/swagger"), {
mode: "static",
specification: {
path: schemaPath ?? "./../docs/openapi/jan.yaml",
baseDir: baseDir ?? "./../docs/openapi",
},
});
// Register Swagger UI
await server.register(require("@fastify/swagger-ui"), {
routePrefix: "/",
baseDir: baseDir ?? path.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:
process.env.EXTENSION_ROOT ||
path.join(require("os").homedir(), "jan", "extensions"),
wildcard: false,
});
done();
},
{ prefix: "extensions" }
);
// Register API routes
await server.register(v1Router, { prefix: "/v1" });
// Start listening for requests
await server
.listen({
port: hostSetting,
host: portSetting,
})
.then(() => {
// Log server listening
if (isVerbose)
log(
`[API]::Debug: JAN API listening at: http://${JAN_API_HOST}:${JAN_API_PORT}`
);
});
} catch (e) {
// Log any errors
if (isVerbose) log(`[API]::Error: ${e}`);
}
};
/**
* Function to stop the server
*/
export const stopServer = async () => {
try {
// Log server stop
if (isVerbose) log(`[API]::Debug: Server stopped`, "server.log");
// Stop the server
await server.close();
} catch (e) {
// Log any errors
if (isVerbose) log(`[API]::Error: ${e}`);
}
};
/**
* Function to check if CORS is enabled
* @returns - True if CORS is enabled, false otherwise
*/
export const isCorsEnabled = () => corsEnbaled;
/**
* Function to check if verbose logging is enabled
* @returns - True if verbose logging is enabled, false otherwise
*/
export const isVerboseEnabled = () => isVerbose;
/**
* Function to get the host setting
* @returns - The current host setting
*/
export const getHost = () => hostSetting;
/**
* Function to get the port setting
* @returns - The current port setting
*/
export const getPort = () => portSetting;