forked from restspace/rs-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ts
87 lines (80 loc) · 2.94 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
import { Modules } from "./Modules.ts";
import { Tenant } from "./tenant.ts";
import * as log from "std/log/mod.ts";
import Ajv from "https://cdn.skypack.dev/ajv?dts";
import * as path from "std/path/mod.ts";
import { LogRecord } from "https://deno.land/[email protected]/log/logger.ts";
import { Authoriser } from "./auth/Authoriser.ts";
import { IChordServiceConfig } from "rs-core/IServiceConfig.ts";
import { schemaIChordServiceConfig } from "rs-core/IServiceConfig.ts";
import { IChord } from "./IChord.ts";
import { Message } from "../rs-core/Message.ts";
export interface Infra {
adapterSource: string;
}
export interface IServerConfig {
tenancy: "single" | "multi";
mainDomain: string,
domainMap: { [domain: string]: string };
infra: { [ name: string ]: Infra };
configStore: string;
incomingAlwaysHttps?: boolean;
setServerCors(msg: Message): Message;
}
const formatter = (rec: LogRecord) => {
const dt = rec.datetime;
const hr = dt.getHours().toString().padStart(2, '0');
const mn = dt.getMinutes().toString().padStart(2, '0');
const sc = dt.getSeconds().toString().padStart(2, '0');
const ms = dt.getMilliseconds().toString().padStart(3, '0');
return `${hr}:${mn}:${sc}:${ms} ${rec.msg}`;
}
export type LogLevel = "NOTSET" | "DEBUG" | "INFO" | "WARNING" | "ERROR" | "CRITICAL";
// we allow for extra schema properties like 'editor' to direct UI
const ajv = new Ajv({ allErrors: true, strictSchema: false, allowUnionTypes: true });
export const config = {
server: {} as IServerConfig,
modules: new Modules(ajv),
tenants: {} as { [ name: string ]: Tenant },
logger: log.getLogger(),
// path.resolves resolves relative to dir of current source file, which is repo root
fixRelativeToRoot: (pathUrl: string) => pathUrl.startsWith('.') ? path.resolve(pathUrl) : pathUrl,
ajv,
jwtExpiryMins: 30,
getParam: (key: string) => Deno.env.get(key),
authoriser: new Authoriser(),
validateChordService: ajv.compile<IChordServiceConfig>(schemaIChordServiceConfig),
validateChord: ajv.compile<IChord>({
type: "object",
properties: {
id: { type: "string" },
newServices: {
type: "array",
items: {
schemaIChordServiceConfig
}
}
}
}),
requestExternal: (msg: Message) => msg.requestExternal()
}
export const setupLogging = async (level: LogLevel) => {
await log.setup({
handlers: {
console: new log.handlers.ConsoleHandler(level, { formatter }),
file: new log.handlers.RotatingFileHandler(level, {
maxBytes: 512 * 1024,
maxBackupCount: 5,
filename: './main.log',
formatter
})
},
loggers: {
default: {
level,
handlers: [ 'console', 'file' ]
}
}
});
config.logger = log.getLogger();
}