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 | 3x
3x
3x
3x
3x
3x
3x
3x
3x
3x
3x
3x
3x
3x
3x
20x
20x
20x
20x
20x
1x
19x
20x
3x
15x
15x
15x
15x
15x
15x
15x
11x
11x
11x
15x
20x
20x
20x
15x
1x
15x
15x
13x
15x
20x
14x
14x
13x
14x
14x
20x
12x
15x
15x
15x
15x
8x
15x
15x
15x
15x
11x
11x
11x
12x
11x
1x
11x
15x
| import {GatewayStorefrontInstance} from "./gatewayStorefront";
import {Page} from "./page";
import async from "async";
import {EVENTS, HEALTHCHECK_PATHS, HTTP_METHODS, HTTP_STATUS_CODE} from "./enums";
import {wait} from "./util";
import {Logger} from "./logger";
import {callableOnce, sealed} from "./decorators";
import {container, TYPES} from "./base";
import {Server} from "./server";
import {IGatewayMap, IPageMap, IStorefrontConfig} from "./types";
import ResourceFactory from "./resourceFactory";
import {GATEWAY_PREPERATION_CHECK_INTERVAL, PUZZLE_DEBUGGER_LINK, TEMP_FOLDER} from "./config";
import {StorefrontConfigurator} from "./configurator";
import path from "path";
import fs from "fs";
const logger = container.get(TYPES.Logger) as Logger;
@sealed
export class Storefront {
server: Server;
config: IStorefrontConfig;
pages: IPageMap = {};
gateways: IGatewayMap = {};
private gatewaysReady = 0;
/**
* Storefront Instance
* @param {IStorefrontConfig} storefrontConfig
* @param {Server} _server
*/
constructor(storefrontConfig: IStorefrontConfig | StorefrontConfigurator, _server?: Server) {
this.server = _server || container.get(TYPES.Server);
if (storefrontConfig instanceof StorefrontConfigurator) {
this.config = storefrontConfig.configuration;
} else {
this.config = storefrontConfig;
}
this.bootstrap();
}
/**
* Starts storefront instance
* @param {Function} cb
*/
@callableOnce
init(cb?: Function) {
logger.info('Starting Puzzle Storefront');
async.series([
this.registerDependencies.bind(this),
this.waitForGateways.bind(this),
this.registerDebugScripts.bind(this),
this.addCustomHeaders.bind(this),
this.addHealthCheckRoute.bind(this),
this.preLoadPages.bind(this),
this.addPageRoute.bind(this)
], err => {
Eif (!err) {
this.server.listen(this.config.port, () => {
logger.info(`Storefront is listening on port ${this.config.port}`);
cb && cb();
}, this.config.ipv4);
} else {
throw err;
}
});
}
private preLoadPages(cb: Function) {
Promise.all(Object.values(this.pages).map(async (page) => {
logger.info(`Preloading page: ${page.name}`);
await page.reCompile();
logger.info(`Preloaded page: ${page.name}`);
})).then(() => cb());
}
private bootstrap() {
Iif (!fs.existsSync(TEMP_FOLDER)) {
fs.mkdirSync(TEMP_FOLDER);
}
this.server.useProtocolOptions(this.config.spdy);
this.createStorefrontPagesAndGateways();
}
/**
* Creates static routes for debugging scripts
* @param {Function} cb
* @returns {Promise<void>}
*/
private async registerDebugScripts(cb: Function) {
this.server.addRoute(PUZZLE_DEBUGGER_LINK, HTTP_METHODS.GET, (req, res) => {
res.sendFile(path.join(__dirname, './public/puzzle_debug.js'));
});
cb(null);
}
/**
* Waits for gateways to be prepared
* @param {Function} cb
* @returns {Promise<void>}
*/
private async waitForGateways(cb: Function) {
while (Object.keys(this.gateways).length != this.gatewaysReady) {
await wait(GATEWAY_PREPERATION_CHECK_INTERVAL);
}
cb(null);
}
/**
* Creates gateway pages, pages and subscribes event to gateways to track ready status
*/
private createStorefrontPagesAndGateways() {
this.config.gateways.forEach(gatewayConfiguration => {
const gateway = new GatewayStorefrontInstance(gatewayConfiguration, this.config.authToken, this.config.satisfyUpdateCount);
gateway.events.once(EVENTS.GATEWAY_READY, () => {
this.gatewaysReady++;
});
gateway.startUpdating();
this.gateways[gatewayConfiguration.name] = gateway;
});
this.config.pages.forEach(pageConfiguration => {
this.pages[pageConfiguration.url.toString()] = new Page(pageConfiguration.html, this.gateways, pageConfiguration.name);
});
}
/**
* Registers provided dependencies in storefront configuration
* @param {Function} cb
*/
private registerDependencies(cb: Function) {
this.config.dependencies.forEach(dependency => {
logger.info(`Registering Dependency: ${dependency.name}`);
ResourceFactory.instance.registerDependencies(dependency);
});
cb();
}
/**
* Adds healthcheck route.
* @param {Function} cb
*/
private addHealthCheckRoute(cb: Function) {
logger.info(`Registering healthcheck routes: ${HEALTHCHECK_PATHS}`);
this.server.addRoute(HEALTHCHECK_PATHS, HTTP_METHODS.GET, (req, res) => {
res.status(HTTP_STATUS_CODE.OK).end();
});
cb();
}
/**
* Adds custom headers
* @param {Function} cb
*/
private addCustomHeaders(cb: Function) {
this.server.addCustomHeaders(this.config.customHeaders);
cb();
}
/**
* Adds page routes then connects with page instance responsible for it.
* @param {Function} cb
*/
private addPageRoute(cb: Function) {
this.config.pages.forEach(page => {
const targetPage = page.url.toString();
logger.info(`Adding page ${page.name} route: ${targetPage}`);
this.server.addRoute(page.url, HTTP_METHODS.GET, (req, res, next) => {
if (typeof page.condition === 'function' ? page.condition(req) : true) {
this.pages[targetPage].handle(req, res);
} else {
next();
}
});
this.server.addRoute(page.url, HTTP_METHODS.POST, (req, res, next) => {
this.pages[targetPage].post(req, res, next);
});
});
cb();
}
}
|