-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.ts
133 lines (119 loc) · 4.38 KB
/
config.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
import { networkInterfaces } from 'node:os';
import * as Logger from 'bunyan';
import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions';
import * as restify from 'restify';
import { RequestHandler as RestifyRequestHandler } from 'restify';
import { Options as SequelizeOptions } from 'sequelize';
import { IOrmMwConfig, IOrmsOut, RequestHandler } from '@offscale/orm-mw/interfaces';
import { uri_to_config } from '@offscale/nodejs-utils';
import { IRoutesMergerConfig, TApp } from '@offscale/routes-merger/interfaces';
/* TODO: Put this all in tiered environment-variable powered .json file */
// Enforce UTC
process.env.TZ = 'Etc/UTC';
export const db_uri: string = process.env['RDBMS_URI']
|| process.env['DATABASE_URL']
|| process.env['POSTGRES_URL']
|| '';
if (db_uri == null || !db_uri.length)
throw ReferenceError('Database URI not set. See README.md for setup tutorial.');
export const typeorm_config: PostgresConnectionOptions = Object.freeze(
Object.assign(Object.entries(uri_to_config(db_uri))
.map((kv: [string, any]) => ({ [kv[0] === 'user' ? 'username' : kv[0]]: kv[1] }))
.reduce((a, b) => Object.assign(a, b), {}),
{
type: 'postgres',
autoSchemaSync: true,
synchronize: true,
logging: false
}
) as PostgresConnectionOptions
);
// import * as sequelize from 'sequelize';
export const sequelize_config: SequelizeOptions = {
dialect: 'postgres' as 'mysql' | 'postgres' | 'sqlite' | 'mariadb' | 'mssql' | 'mariadb',
define: {
timestamps: false
}
};
// Database waterline_config
/*import { ConfigOptions } from 'waterline';
import * as waterline_postgres from 'waterline-postgresql';*/
export const waterline_config /*: ConfigOptions*/ = Object.freeze({
adapters: {
url: db_uri,
postgres: undefined // waterline_postgres
},
defaults: {
migrate: 'create'
},
connections: {
main_db: {
adapter: 'postgres',
connection: db_uri,
pool: {
min: 2,
max: 20
}
}
}
} /* as any as ConfigOptions */);
// ONLY USE `_orms_out` FOR TESTS!
export const _orms_out: { orms_out: IOrmsOut } = { orms_out: undefined as any };
export const getOrmMwConfig = (models: Map<string, any>, logger: Logger,
cb: (err: Error | undefined,
with_app?: IRoutesMergerConfig['with_app'],
orms_out?: IOrmsOut) => void): IOrmMwConfig => ({
models, logger,
orms_in: {
redis: {
skip: false,
config: {
port: parseInt(process.env.REDIS_PORT || 6379 as any, 10),
host: process.env.REDIS_HOST || 'localhost'
}
},
sequelize: {
skip: true,
config: sequelize_config,
uri: db_uri
},
typeorm: {
skip: false,
config: typeorm_config as any
},
waterline: {
skip: true /*,
config: waterline_config*/
}
},
callback: (e?: Error | undefined, mw?: RequestHandler, orms_out?: IOrmsOut) => {
if (e != null) {
if (cb != null) return cb(e);
throw e;
}
_orms_out.orms_out = orms_out!;
return cb(void 0, (_app: TApp) => {
if ((_app as typeof _app & { use: boolean })['use'])
(_app as restify.Server).use(mw as RestifyRequestHandler);
// import { Next, Server } from 'restify';
// import { WaterlineError } from '@offscale/custom-restify-errors';
// import { WLError } from 'waterline';
/*_app.on('WLError', (req, res, err: WLError, next: Next) =>
next(new WaterlineError(err))
);*/
return _app;
}, orms_out!);
}
});
export const getPrivateIPAddress = (): string => {
const interfaces = networkInterfaces();
for (const devName in interfaces) {
if (!interfaces.hasOwnProperty(devName)) continue;
const iface = interfaces[devName];
if (iface != null)
for (const alias of iface)
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
return alias.address;
}
return '0.0.0.0';
};