-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.ts
283 lines (208 loc) · 6.98 KB
/
server.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import 'zone.js/dist/zone-node';
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { join } from 'path';
import axios from 'axios';
import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';
import { existsSync } from 'fs';
import rateLimit from 'express-rate-limit';
import slugify from 'slugify';
import cron from 'node-cron';
import * as fs from 'fs';
function formatDate(timestamp: string) {
// Extraire la partie de la date du timestamp
const datePart = timestamp.split('T')[0];
return datePart;
}
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes)
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
keyGenerator: (request, response) => String(request.headers['x-forwarded-for'])
});
import { createMiddleware, getContentType, getSummary, signalIsUp } from '@promster/express';
async function gen_sitemaps() {
console.log('Generating sitemaps');
let courseBaseURL = 'https://zerofiltre.tech/cours/';
let article_data = await axios.get('https://blog-api.zerofiltre.tech/article?pageNumber=0&pageSize=10000&status=published')
let articles = article_data.data.content;
const articles_urls = articles.map(article => {
let slug = slugify(article.title, {
lower: true,
replacement: '-',
strict: true,
locale: 'fr',
remove: /[*+~.()'"!:@,]/g,
trim: true
});
let url = `https://zerofiltre.tech/articles/${article.id}-${slug}`;
let xml = `
<url>
<loc>${url}</loc>
<lastmod>${formatDate(article.lastSavedAt)}</lastmod>
<priority>0.80</priority>
</url>
`
return xml;
});
let courses_data = await axios.get('https://blog-api.zerofiltre.tech/course?pageNumber=0&pageSize=10000&status=published')
let courseXml = "";
for (let i = 0; i < courses_data.data.content.length; i++) {
let course = courses_data.data.content[i];
let courseUrl = `${courseBaseURL}${course.id}-${slugify(course.title, {
lower: true,
replacement: '-',
strict: true,
locale: 'fr',
remove: /[*+~.()'"!:@,]/g,
trim: true
})}`;
courseXml += `
<url>
<loc>${courseUrl}</loc>
<lastmod>${formatDate(course.lastSavedAt)}</lastmod>
<priority>0.80</priority>
</url>
`;
let chapters = await axios.get(`https://blog-api.zerofiltre.tech/chapter/course/${course.id}`)
chapters.data.forEach(chapter => {
chapter.lessons.forEach(lesson => {
let lessonUrl = `${courseUrl}/${lesson.id}-${slugify(lesson.title, {
lower: true,
replacement: '-',
strict: true,
locale: 'fr',
remove: /[*+~.()'"!:@,]/g,
trim: true
})}`;
let lessonXml = `
<url>
<loc>${lessonUrl}</loc>
<lastmod>${formatDate(course.lastSavedAt)}</lastmod>
<priority>0.80</priority>
</url>
`;
courseXml += lessonXml
});
});
}
let xml = `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>https://zerofiltre.tech/</loc>
<lastmod>2023-04-22T01:14:27+00:00</lastmod>
<priority>1.00</priority>
</url>
<!-- ARTICLES ROUTES -->
<url>
<loc>https://zerofiltre.tech/articles</loc>
<lastmod>2023-04-22T01:14:27+00:00</lastmod>
<priority>0.80</priority>
</url>
<!-- RANDOM ROUTES -->
<url>
<loc>https://zerofiltre.tech/wachatgpt</loc>
<lastmod>2023-04-22T01:14:27+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>https://zerofiltre.tech/login</loc>
<lastmod>2023-04-22T01:14:27+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>https://zerofiltre.tech/register</loc>
<lastmod>2023-04-22T01:14:27+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>https://zerofiltre.tech/resetPassword</loc>
<lastmod>2023-04-22T01:14:27+00:00</lastmod>
<priority>0.64</priority>
</url>
<!-- END RANDOM ROUTES -->
<!-- COURSES ROUTES -->
<url>
<loc>https://zerofiltre.tech/cours</loc>
<lastmod>2023-04-22T01:14:27+00:00</lastmod>
<priority>0.90</priority>
</url>
\n\n`;
articles_urls.forEach(url => {
xml += url;
});
xml += courseXml;
xml += '</urlset>';
fs.writeFileSync('sitemap.xml', xml);
}
//every weeek
cron.schedule('0 0 * * 0', async () => {
await gen_sitemaps();
});
// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
const server = express();
const distFolder = join(process.cwd(), 'dist/zerofiltre-blog/browser');
const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
server.engine('html', ngExpressEngine({
inlineCriticalCss: false,
bootstrap: AppServerModule,
}));
server.set('view engine', 'html');
server.set('views', distFolder);
server.use(createMiddleware({
app: server,
options: {
metricPrefix: 'front_'
}
}));
server.get('/metrics', async (req, res) => {
req.statusCode = 200;
res.setHeader('Content-Type', getContentType());
res.end(await getSummary());
});
// Example Express Rest API endpoints
// server.get('/api/**', (req, res) => { });
// Serve static files from /browser
server.get('*.*', express.static(distFolder, {
maxAge: '1y'
}));
server.get('/sitemap.xml', async (req, res) => {
if(!fs.existsSync('sitemap.xml')){
await gen_sitemaps();
}
let xml = fs.readFileSync('sitemap.xml', 'utf8');
res.setHeader('Content-Type', 'text/xml');
res.end(xml);
});
// All regular routes use the Universal engine
server.get('*', (req, res) => {
console.log('IP: ', req.headers['x-forwarded-for'])
res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
});
return server;
}
function run(): void {
const port = process.env['PORT'] || 4000;
// Start up the Node server
const server = app();
server.listen(port, async () => {
console.log(`Node Express server listening on http://localhost:${port}`);
await gen_sitemaps();
signalIsUp()
});
}
// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
run();
}
export * from './src/main.server';