forked from jolocom/jolocom-example-service
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.ts
80 lines (68 loc) · 2.28 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
import appRootDir from 'app-root-path'
import dotenv from 'dotenv'
import { strict as assert } from 'assert'
import { Oas3ToolsObjectOrientedConfig } from '@jolocom/oas3-tools-object-oriented'
import { oas3AppOptions } from './oas3'
import { OpenApiRequestHandler } from 'express-openapi-validator/dist/framework/types'
// Defining default environment variables
process.env.NODE_ENV = process.env.APP_ENV || 'dev'
process.env.API_VERSION = process.env.API_VERSION || 'v1'
// Loading env variables defined in .env file into the process.env scope
const envFound = dotenv.config()
if (envFound.error) {
throw new Error("Couldn't find .env file")
}
// Asserting that required env variables are defined
const assertRequiredVariablesDefined = () => {
assert(process.env.APP_PORT, "Expected 'process.env.APP_PORT' to be defined")
assert(process.env.APP_ENV, "Expected 'process.env.APP_ENV' to be defined")
assert(process.env.APP_HOST, "Expected 'process.env.APP_HOST' to be defined")
assert(
process.env.APP_SCHEME,
"Expected 'process.env.APP_SCHEME' to be defined",
)
assert(
process.env.SDK_CALLBACK_URL,
"Expected 'process.env.SDK_CALLBACK_URL' to be defined",
)
}
assertRequiredVariablesDefined()
/**
* The main application configuration definition abstraction.
*/
export interface AppConfig {
port: string
env: string
host: string
schema: string
apiVersion: string
apiRootPath: string
controllersPath: string
logger: {
logDir: string
}
openapi: (
customMiddlewares?: OpenApiRequestHandler[],
) => Oas3ToolsObjectOrientedConfig
}
const oas3DeclarationFilePath = appRootDir + '/src/api/openapi.yaml'
/**
* The main application configuration definition.
*/
export const config: AppConfig = {
port: process.env.APP_PORT as string,
env: process.env.APP_ENV as string,
host: process.env.APP_HOST as string,
schema: process.env.APP_SCHEME as string,
apiVersion: process.env.API_VERSION as string,
apiRootPath: '/api/' + process.env.API_VERSION,
controllersPath: appRootDir + '/src/controller',
logger: {
logDir: process.env.LOG_DIR || appRootDir + '/var/log',
},
openapi: (customMiddlewares?: OpenApiRequestHandler[]) => ({
oas3AppOptions: oas3AppOptions(oas3DeclarationFilePath),
oas3DeclarationFilePath,
customMiddlewares,
}),
}