-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
199 lines (173 loc) · 4.17 KB
/
index.ts
File metadata and controls
199 lines (173 loc) · 4.17 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { loadConfig } from '@codex-team/config-loader';
import * as process from 'process';
import arg from 'arg';
import path from 'path';
import { z } from 'zod';
/**
* Metrics configuration
*/
const MetricsConfig = z.object({
enabled: z.boolean(), // todo use this config to setup metrics server later
host: z.string(),
port: z.number(),
});
/**
* Authentication configuration
*/
const AuthConfig = z.object({
accessSecret: z.string(),
refreshSecret: z.string(),
accessExpiresIn: z.number(),
refreshExpiresIn: z.number(),
});
export type AuthConfig = z.infer<typeof AuthConfig>;
const GoogleOAuth2Config = z.object({
clientId: z.string(),
clientSecret: z.string(),
redirectUrl: z.string(),
callbackUrl: z.string(),
});
export type GoogleOAuth2Config = z.infer<typeof GoogleOAuth2Config>;
const OAuth2Config = z.object({
google: GoogleOAuth2Config,
});
export type OAuth2Config = z.infer<typeof OAuth2Config>;
/**
* Database configuration
*/
const DatabaseConfig = z.object({
dsn: z.string(), // todo url or params
});
/**
* OpenAI access config
*/
const OpenAIConfig = z.object({
token: z.string(),
});
/**
* S3 storage configuration
*/
const S3StorageConfig = z.object({
accessKeyId: z.string(),
secretAccessKey: z.string(),
region: z.optional(z.string()),
endpoint: z.optional(z.string()),
});
export type S3StorageConfig = z.infer<typeof S3StorageConfig>;
export type DatabaseConfig = z.infer<typeof DatabaseConfig>;
/**
* Available logging levels configuration
*/
const LoggingLevel = z.union([
z.boolean(), // disabled if false, 'info' if true
z.literal('fatal'),
z.literal('error'),
z.literal('warn'),
z.literal('info'),
z.literal('debug'),
z.literal('trace'),
z.literal('silent'),
]);
/**
* Logging configuration
*/
export const LoggingConfig = z.object({
global: LoggingLevel,
metricsServer: LoggingLevel,
appServer: LoggingLevel,
database: LoggingLevel,
s3Storage: LoggingLevel,
middlewares: LoggingLevel,
policies: LoggingLevel,
});
export type LoggingConfig = z.infer<typeof LoggingConfig>;
/**
* Http API configuration
*/
const HttpApiConfig = z.object({
fileSizeLimit: z.number(),
host: z.string(),
port: z.number(),
cookieSecret: z.string(),
cookieDomain: z.string(),
accessTokenSecret: z.string(),
allowedOrigins: z.union([z.array(z.string()), z.literal('*')]),
oauth2: OAuth2Config,
});
export type HttpApiConfig = z.infer<typeof HttpApiConfig>;
/**
* Application configuration
*/
const AppConfig = z.object({
httpApi: HttpApiConfig,
metrics: MetricsConfig,
logging: LoggingConfig,
database: DatabaseConfig,
auth: AuthConfig,
openai: OpenAIConfig,
s3: S3StorageConfig,
});
export type AppConfig = z.infer<typeof AppConfig>;
const defaultConfig: AppConfig = {
httpApi: {
fileSizeLimit: 10000000, // 10mb
host: '0.0.0.0',
port: 3000,
cookieSecret: 'cookieSecret',
cookieDomain: 'localhost',
accessTokenSecret: 'accessTokenSecret',
allowedOrigins: [],
oauth2: {
google: {
clientId: '',
clientSecret: '',
redirectUrl: '/oauth/google/login',
callbackUrl: '/oauth/google/callback',
},
},
},
auth: {
accessSecret: 'accessSecret',
refreshSecret: 'refreshSecret',
accessExpiresIn: 900000,
refreshExpiresIn: 2592000000,
},
metrics: {
enabled: true,
host: '0.0.0.0',
port: 9090,
},
logging: {
global: 'info',
metricsServer: 'info',
appServer: 'info',
database: 'info',
s3Storage: 'info',
middlewares: 'info',
policies: 'info',
},
database: {
dsn: 'postgres://user:pass@postgres/codex-notes',
},
s3: {
accessKeyId: '',
secretAccessKey: '',
},
openai: {
token: '',
},
};
const args = arg({ /* eslint-disable @typescript-eslint/naming-convention */
'--config': [String],
'-c': '--config',
});
const cwd = process.cwd();
const paths = (args['--config'] || ['./app-config.yaml']).map((configPath) => {
if (path.isAbsolute(configPath)) {
return configPath;
}
return path.join(cwd, configPath);
});
const loadedConfig = loadConfig(...[defaultConfig, ...paths]);
const appConfig = AppConfig.parse(loadedConfig);
export default appConfig;