mirror of
https://gh-proxy.org/https://github.com/klinsmaya/gateway.git
synced 2026-11-08 00:57:59 +08:00
dont crash on unhandled promise rejections and other changes like renaming settings to conf and handle reloading the conf file during runtime
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
# Local configuration file
|
||||
conf.json
|
||||
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
|
||||
+29
-17
@@ -1,5 +1,5 @@
|
||||
export const defaultOrganisationDetails = {
|
||||
id: '00000000-0000-0000-0000-000000000000',
|
||||
id: 'self-hosted-organisation',
|
||||
name: 'Portkey self hosted',
|
||||
settings: {
|
||||
debug_log: 1,
|
||||
@@ -39,26 +39,38 @@ const transformIntegrations = (integrations: any) => {
|
||||
usage_limits: integration.usage_limits,
|
||||
rate_limits: integration.rate_limits,
|
||||
models: integration.models,
|
||||
allow_all_models: integration.allow_all_models,
|
||||
},
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
let settings: any = undefined;
|
||||
try {
|
||||
const settingsFile = await import('./settings.json');
|
||||
if (settingsFile) {
|
||||
settings = {};
|
||||
settings.organisationDetails = defaultOrganisationDetails;
|
||||
if (settingsFile.integrations) {
|
||||
settings.integrations = transformIntegrations(settingsFile.integrations);
|
||||
export const getSettings = async () => {
|
||||
try {
|
||||
const isFetchSettingsFromFile =
|
||||
process?.env?.FETCH_SETTINGS_FROM_FILE === 'true';
|
||||
if (!isFetchSettingsFromFile) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(
|
||||
'WARNING: Unable to import settings from the path, please make sure the file exists',
|
||||
error
|
||||
);
|
||||
}
|
||||
let settings: any = undefined;
|
||||
const { readFile } = await import('fs/promises');
|
||||
const settingsFile = await readFile('./conf.json', 'utf-8');
|
||||
const settingsFileJson = JSON.parse(settingsFile);
|
||||
|
||||
export { settings };
|
||||
if (settingsFileJson) {
|
||||
settings = {};
|
||||
settings.organisationDetails = defaultOrganisationDetails;
|
||||
if (settingsFileJson.integrations) {
|
||||
settings.integrations = transformIntegrations(
|
||||
settingsFileJson.integrations
|
||||
);
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(
|
||||
'WARNING: unable to load settings from your conf.json file',
|
||||
error
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -38,10 +38,15 @@ import { imageEditsHandler } from './handlers/imageEditsHandler';
|
||||
import conf from '../conf.json';
|
||||
import modelResponsesHandler from './handlers/modelResponsesHandler';
|
||||
import { messagesCountTokensHandler } from './handlers/messagesCountTokensHandler';
|
||||
import { createCacheBackendsRedis } from './shared/services/cache';
|
||||
|
||||
// Create a new Hono server instance
|
||||
const app = new Hono();
|
||||
const runtime = getRuntimeKey();
|
||||
|
||||
if (runtime === 'node' && process.env.REDIS_CONNECTION_STRING) {
|
||||
createCacheBackendsRedis(process.env.REDIS_CONNECTION_STRING);
|
||||
}
|
||||
/**
|
||||
* Middleware that conditionally applies compression middleware based on the runtime.
|
||||
* Compression is automatically handled for lagon and workerd runtimes
|
||||
|
||||
@@ -188,3 +188,11 @@ if (!isHeadless) {
|
||||
|
||||
// Single-line ready message
|
||||
console.log('\n\x1b[32m✨ Ready for connections!\x1b[0m');
|
||||
|
||||
process.on('uncaughtException', (err) => {
|
||||
console.error('Unhandled exception', err);
|
||||
});
|
||||
|
||||
process.on('unhandledRejection', (err) => {
|
||||
console.error('Unhandled rejection', err);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user