diff --git a/.gitignore b/.gitignore index 031ff86c..31820cc3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Local configuration file +conf.json + # Logs logs *.log diff --git a/settings.example.json b/conf.example.json similarity index 100% rename from settings.example.json rename to conf.example.json diff --git a/initializeSettings.ts b/initializeSettings.ts index b6704ecd..b961ad01 100644 --- a/initializeSettings.ts +++ b/initializeSettings.ts @@ -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 + ); + } +}; diff --git a/src/index.ts b/src/index.ts index 15f3d43c..aac23c2f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 diff --git a/src/start-server.ts b/src/start-server.ts index f58da423..5c9cc0b1 100644 --- a/src/start-server.ts +++ b/src/start-server.ts @@ -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); +});