-
Notifications
You must be signed in to change notification settings - Fork 2
/
getServerConfig.ts
30 lines (27 loc) · 1.24 KB
/
getServerConfig.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
import { Message } from "rs-core/Message.ts";
import { config, IServerConfig } from "./config.ts";
export const getServerConfig = async (serverConfigLocation: string) => {
if (!serverConfigLocation) throw new Error('Missing server config location');
try {
const serverConfigText = await Deno.readTextFile(serverConfigLocation);
const serverConfig = JSON.parse(serverConfigText) as IServerConfig;
serverConfig.setServerCors = makeServerCorsSetter();
return serverConfig;
} catch (err) {
throw new Error(`Failed to load server config: ${err}`);
}
}
export const makeServerCorsSetter = () => (msg: Message) => {
const origin = msg.getHeader('origin');
if (origin) {
msg.setHeader('Access-Control-Allow-Origin', origin);
msg.setHeader(
'Access-Control-Allow-Headers',
'Origin,X-Requested-With,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Restspace-Request-Mode'
);
msg.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, HEAD, POST, PUT, PATCH, DELETE');
msg.setHeader('Access-Control-Allow-Credentials', 'true');
msg.setHeader('Access-Control-Expose-Headers', 'X-Restspace-Service');
}
return msg;
}