-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapplication.ts
127 lines (102 loc) · 3.38 KB
/
application.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
import path from 'path';
import express, { Express, json, urlencoded, static as expressStatic } from 'express';
import cors from 'cors';
import fs from 'fs';
import completion from '../../routes/completion.js';
import datastore from '../../routes/datastore.js';
import state from '../../routes/state.js';
import pricing from '../../routes/pricing.js';
import { Server } from 'http';
import { OpenAI } from 'openai';
import { config } from 'dotenv';
const __dirname = path.resolve();
config({ override: true });
export class Application {
public app: Express;
private server?: Server;
constructor() {
this.app = express();
this.setupMiddlewares();
this.setupRoutes();
this.setupGlobalVariables();
}
// start the server
public start(port: number): void {
this.checkEnvVariables();
if (!port) throw new Error('PORT is not defined');
if (isNaN(port)) throw new Error('PORT is not a number');
this.server = this.app.listen(port, () => {
console.log(`Server running on http (port ${port})`);
});
}
public dispose(): void {
if (this.server) {
this.server.close();
// Here you can dispose of any resources that the application uses,
// for example, database connections, file handles, etc.
}
}
private checkEnvVariables(): void {
const envFilePath = path.resolve(process.cwd(), '.env');
if (!this.fileExists(envFilePath)) {
console.error(`ERROR: .env file does not exist at ${envFilePath}`);
process.exit(1);
}
const requiredEnvVars = [
'INTERCOM_API_KEY',
'OPENAI_API_KEY',
'TELNYX_API_KEY',
'POSTGRES_HOST',
'POSTGRES_PORT',
'POSTGRES_USER',
'POSTGRES_DATABASE',
'POSTGRES_PASSWORD',
];
const missingOrEmptyEnvVars = requiredEnvVars.filter((envVar) => {
return !process.env[envVar] || process.env[envVar].trim() === '';
});
if (missingOrEmptyEnvVars.length > 0) {
console.error(`ERROR: Missing or empty required environment variables: ${missingOrEmptyEnvVars.join(', ')}`);
console.error('Please ensure all required environment variables are set and have non-empty values.');
process.exit(1);
}
}
private fileExists(filePath: string): boolean {
try {
fs.accessSync(filePath, fs.constants.F_OK);
return true;
} catch (e) {
return false;
}
}
private setupMiddlewares(): void {
this.app.use(cors({ credentials: true, origin: '*' }));
this.app.use(json());
this.app.use(urlencoded({ extended: true }));
this.app.use(expressStatic(path.join(__dirname, 'build')));
}
private setupRoutes(): void {
this.app.use('/state', state);
this.app.use('/completion', completion);
this.app.use('/datastore', datastore);
this.app.use('/pricing', pricing);
// health check
this.app.get('/health', (req: express.Request, res: express.Response) => {
return res.json({ status: 'OK' });
});
}
private setupGlobalVariables(): void {
const openai = new OpenAI({
baseURL: 'https://api.telnyx.com/v2/ai',
apiKey: process.env.TELNYX_API_KEY,
});
const openai_base = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
this.app.locals.openai = openai;
this.app.locals.openai_base = openai_base;
// operational | degraded | maintenance | offline
this.app.locals.state = {
status: 'operational',
notice: null,
};
}
}