|
1 | 1 | /* eslint-disable no-unused-vars */
|
2 |
| -import convict from 'convict'; |
3 |
| -import yaml from 'js-yaml'; |
4 |
| -import pino from 'pino'; |
5 |
| -import path from 'path'; |
6 |
| -import fs from 'fs'; |
7 |
| -import os from 'os'; |
| 2 | +import convict from "convict"; |
| 3 | +import yaml from "js-yaml"; |
| 4 | +import pino from "pino"; |
| 5 | +import path from "path"; |
| 6 | +import fs from "fs"; |
| 7 | +import os from "os"; |
8 | 8 |
|
9 | 9 | const CWD = process.cwd();
|
10 | 10 |
|
11 | 11 | let pack = {};
|
12 | 12 | try {
|
13 |
| - pack = JSON.parse(fs.readFileSync(path.join(CWD, 'package.json'), 'utf-8')); |
| 13 | + pack = JSON.parse(fs.readFileSync(path.join(CWD, "package.json"), "utf-8")); |
14 | 14 | } catch (error) {
|
15 |
| - /* empty */ |
| 15 | + /* empty */ |
16 | 16 | }
|
17 | 17 |
|
18 |
| -convict.addParser({ extension: ['yml', 'yaml'], parse: yaml.load }); |
| 18 | +convict.addParser({ extension: ["yml", "yaml"], parse: yaml.load }); |
19 | 19 |
|
20 | 20 | convict.addFormat({
|
21 |
| - name: 'secret-string', |
22 |
| - validate: (value) => { |
23 |
| - if (typeof value !== 'string') { |
24 |
| - throw new Error('Value must be a String'); |
25 |
| - } |
26 |
| - }, |
27 |
| - coerce: (value) => { |
28 |
| - if (path.isAbsolute(value)) { |
29 |
| - try { |
30 |
| - const file = fs.readFileSync(value); |
31 |
| - return file.toString(); |
32 |
| - } catch (error) { |
33 |
| - throw new Error( |
34 |
| - `Config could not load secret from path: ${value}`, |
35 |
| - ); |
36 |
| - } |
37 |
| - } |
38 |
| - return value; |
39 |
| - }, |
| 21 | + name: "secret-string", |
| 22 | + validate: (value) => { |
| 23 | + if (typeof value !== "string") { |
| 24 | + throw new Error("Value must be a String"); |
| 25 | + } |
| 26 | + }, |
| 27 | + coerce: (value) => { |
| 28 | + if (path.isAbsolute(value)) { |
| 29 | + try { |
| 30 | + const file = fs.readFileSync(value); |
| 31 | + return file.toString(); |
| 32 | + } catch (error) { |
| 33 | + throw new Error(`Config could not load secret from path: ${value}`); |
| 34 | + } |
| 35 | + } |
| 36 | + return value; |
| 37 | + }, |
40 | 38 | });
|
41 | 39 |
|
42 | 40 | const conf = convict({
|
43 |
| - name: { |
44 |
| - doc: 'Name of the apllication', |
45 |
| - default: pack.name, |
46 |
| - format: String, |
47 |
| - }, |
48 |
| - env: { |
49 |
| - doc: 'Applicaton environments', |
50 |
| - format: ['development', 'production'], |
51 |
| - default: 'development', |
52 |
| - env: 'NODE_ENV', |
53 |
| - arg: 'node-env', |
54 |
| - }, |
55 |
| - metrics: { |
56 |
| - format: Boolean, |
57 |
| - default: true, |
58 |
| - env: 'METRICS', |
59 |
| - }, |
60 |
| - log: { |
61 |
| - level: { |
62 |
| - doc: 'Log level to log at', |
63 |
| - format: ['trace', 'debug', 'info', 'warn', 'error', 'fatal'], |
64 |
| - default: 'info', |
65 |
| - env: 'LOG_LEVEL', |
66 |
| - arg: 'log-level', |
67 |
| - }, |
68 |
| - }, |
69 |
| - http: { |
70 |
| - http2: { |
71 |
| - doc: 'Enable http2 for the server', |
72 |
| - format: Boolean, |
73 |
| - default: false, |
74 |
| - env: 'HTTP_HTTP2', |
75 |
| - }, |
76 |
| - address: { |
77 |
| - doc: 'The address the http server should bind to', |
78 |
| - format: String, |
79 |
| - default: 'localhost', |
80 |
| - env: 'HTTP_ADDRESS', |
81 |
| - }, |
82 |
| - port: { |
83 |
| - doc: 'The port the http server should bind to', |
84 |
| - format: 'port', |
85 |
| - default: 4001, |
86 |
| - env: 'HTTP_PORT', |
87 |
| - }, |
88 |
| - }, |
89 |
| - compression: { |
90 |
| - global: { |
91 |
| - doc: 'Enable global compression for all http routes', |
92 |
| - format: Boolean, |
93 |
| - default: true, |
94 |
| - env: 'COMPRESSION_GLOBAL', |
95 |
| - }, |
96 |
| - }, |
97 |
| - jwt: { |
98 |
| - secret: { |
99 |
| - doc: 'Secret used for JWT signing', |
100 |
| - format: 'secret-string', |
101 |
| - default: 'change_me', |
102 |
| - env: 'AUTH_JWT_SECRET', |
103 |
| - sensitive: true, |
104 |
| - }, |
105 |
| - expire: { |
106 |
| - doc: 'Expire time for JWT', |
107 |
| - format: String, |
108 |
| - default: '60d', |
109 |
| - env: 'AUTH_JWT_EXPIRE', |
110 |
| - }, |
111 |
| - }, |
112 |
| - basicAuth: { |
113 |
| - type: { |
114 |
| - doc: 'Type of basic auth to use', |
115 |
| - format: ['key', 'disabled'], |
116 |
| - default: 'key', |
117 |
| - env: 'BASIC_AUTH_TYPE', |
118 |
| - }, |
119 |
| - key: { |
120 |
| - doc: 'Key used for basic authorization', |
121 |
| - format: 'secret-string', |
122 |
| - default: 'change_me', |
123 |
| - env: 'BASIC_AUTH_KEY', |
124 |
| - sensitive: true, |
125 |
| - }, |
126 |
| - }, |
127 |
| - organization: { |
128 |
| - name: { |
129 |
| - doc: 'Organization name - Used as a folder name in the storage of files', |
130 |
| - format: String, |
131 |
| - default: 'local', |
132 |
| - env: 'ORG_NAME', |
133 |
| - }, |
134 |
| - hostnames: { |
135 |
| - doc: 'Hostnames the organization maps to', |
136 |
| - format: Array, |
137 |
| - default: ['localhost', '127.0.0.1'], |
138 |
| - env: 'ORG_HOSTNAMES', |
139 |
| - }, |
140 |
| - }, |
141 |
| - sink: { |
142 |
| - type: { |
143 |
| - doc: 'Type of sink to use', |
144 |
| - format: ['fs', 'mem', 'test'], |
145 |
| - default: 'fs', |
146 |
| - env: 'SINK_TYPE', |
147 |
| - }, |
148 |
| - path: { |
149 |
| - doc: 'Absolute path to store files in when using the "fs" sink', |
150 |
| - format: String, |
151 |
| - default: path.join(os.tmpdir(), 'eik'), |
152 |
| - env: 'SINK_PATH', |
153 |
| - }, |
154 |
| - }, |
| 41 | + name: { |
| 42 | + doc: "Name of the apllication", |
| 43 | + default: pack.name, |
| 44 | + format: String, |
| 45 | + }, |
| 46 | + env: { |
| 47 | + doc: "Applicaton environments", |
| 48 | + format: ["development", "production"], |
| 49 | + default: "development", |
| 50 | + env: "NODE_ENV", |
| 51 | + arg: "node-env", |
| 52 | + }, |
| 53 | + metrics: { |
| 54 | + format: Boolean, |
| 55 | + default: true, |
| 56 | + env: "METRICS", |
| 57 | + }, |
| 58 | + log: { |
| 59 | + level: { |
| 60 | + doc: "Log level to log at", |
| 61 | + format: ["trace", "debug", "info", "warn", "error", "fatal"], |
| 62 | + default: "info", |
| 63 | + env: "LOG_LEVEL", |
| 64 | + arg: "log-level", |
| 65 | + }, |
| 66 | + }, |
| 67 | + http: { |
| 68 | + http2: { |
| 69 | + doc: "Enable http2 for the server", |
| 70 | + format: Boolean, |
| 71 | + default: false, |
| 72 | + env: "HTTP_HTTP2", |
| 73 | + }, |
| 74 | + address: { |
| 75 | + doc: "The address the http server should bind to", |
| 76 | + format: String, |
| 77 | + default: "localhost", |
| 78 | + env: "HTTP_ADDRESS", |
| 79 | + }, |
| 80 | + port: { |
| 81 | + doc: "The port the http server should bind to", |
| 82 | + format: "port", |
| 83 | + default: 4001, |
| 84 | + env: "HTTP_PORT", |
| 85 | + }, |
| 86 | + }, |
| 87 | + compression: { |
| 88 | + global: { |
| 89 | + doc: "Enable global compression for all http routes", |
| 90 | + format: Boolean, |
| 91 | + default: true, |
| 92 | + env: "COMPRESSION_GLOBAL", |
| 93 | + }, |
| 94 | + }, |
| 95 | + jwt: { |
| 96 | + secret: { |
| 97 | + doc: "Secret used for JWT signing", |
| 98 | + format: "secret-string", |
| 99 | + default: "change_me", |
| 100 | + env: "AUTH_JWT_SECRET", |
| 101 | + sensitive: true, |
| 102 | + }, |
| 103 | + expire: { |
| 104 | + doc: "Expire time for JWT", |
| 105 | + format: String, |
| 106 | + default: "60d", |
| 107 | + env: "AUTH_JWT_EXPIRE", |
| 108 | + }, |
| 109 | + }, |
| 110 | + basicAuth: { |
| 111 | + type: { |
| 112 | + doc: "Type of basic auth to use", |
| 113 | + format: ["key", "disabled"], |
| 114 | + default: "key", |
| 115 | + env: "BASIC_AUTH_TYPE", |
| 116 | + }, |
| 117 | + key: { |
| 118 | + doc: "Key used for basic authorization", |
| 119 | + format: "secret-string", |
| 120 | + default: "change_me", |
| 121 | + env: "BASIC_AUTH_KEY", |
| 122 | + sensitive: true, |
| 123 | + }, |
| 124 | + }, |
| 125 | + organization: { |
| 126 | + name: { |
| 127 | + doc: "Organization name - Used as a folder name in the storage of files", |
| 128 | + format: String, |
| 129 | + default: "local", |
| 130 | + env: "ORG_NAME", |
| 131 | + }, |
| 132 | + hostnames: { |
| 133 | + doc: "Hostnames the organization maps to", |
| 134 | + format: Array, |
| 135 | + default: ["localhost", "127.0.0.1"], |
| 136 | + env: "ORG_HOSTNAMES", |
| 137 | + }, |
| 138 | + }, |
| 139 | + sink: { |
| 140 | + type: { |
| 141 | + doc: "Type of sink to use", |
| 142 | + format: ["fs", "mem", "test"], |
| 143 | + default: "fs", |
| 144 | + env: "SINK_TYPE", |
| 145 | + }, |
| 146 | + path: { |
| 147 | + doc: 'Absolute path to store files in when using the "fs" sink', |
| 148 | + format: String, |
| 149 | + default: path.join(os.tmpdir(), "eik"), |
| 150 | + env: "SINK_PATH", |
| 151 | + }, |
| 152 | + }, |
155 | 153 | });
|
156 | 154 |
|
157 |
| -const env = conf.get('env'); |
| 155 | +const env = conf.get("env"); |
158 | 156 |
|
159 | 157 | // @ts-expect-error This is in fact callable
|
160 | 158 | const logger = pino({
|
161 |
| - level: conf.get('log.level'), |
162 |
| - name: conf.get('name'), |
| 159 | + level: conf.get("log.level"), |
| 160 | + name: conf.get("name"), |
163 | 161 | });
|
164 | 162 |
|
165 | 163 | try {
|
166 |
| - conf.loadFile(path.join(CWD, 'config', `${env}.yaml`)); |
| 164 | + conf.loadFile(path.join(CWD, "config", `${env}.yaml`)); |
167 | 165 | } catch (error) {
|
168 |
| - logger.error(error); |
| 166 | + logger.error(error); |
169 | 167 | }
|
170 | 168 |
|
171 | 169 | conf.validate();
|
|
0 commit comments