-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
123 lines (111 loc) · 4.19 KB
/
app.js
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
import path from 'node:path';
import createError from 'http-errors';
import express from 'express';
import logger from 'morgan';
import globalRouter from './routes/global.js';
import universeRouter from './routes/universe.js';
import pkginfoRouter from './routes/pkginfo.js';
import {get_latest} from './src/db.js';
const production = process.env.NODE_ENV == 'production';
const app = express();
// view engine setup
app.set('views', 'views');
app.set('view engine', 'pug');
//app.set('view cache', true); //enabled by default in prod?
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static('static')); //TODO: remove?
app.use('/_global/favicon.ico', express.static('static/favicon.ico'));
app.use('/_global/static', express.static('static'));
// remove trailing slashes
app.use((req, res, next) => {
if (req.path.slice(-1) === '/' && req.path.length > 1) {
const query = req.url.slice(req.path.length)
const safepath = req.path.slice(0, -1).replace(/\/+/g, '/')
res.redirect(301, safepath + query)
} else {
next()
}
})
// set pug globals for 'universe' and 'node_env'
app.use(function(req, res, next){
if(process.env.UNIVERSE){
req.universe = process.env.UNIVERSE;
} else if(req.app.get('env') === 'production'){
req.universe = req.hostname.replace('.r-universe.dev', '');
res.locals.vhost = req.headers['r-universe-vhost'];
}
res.locals.universe = req.universe || 'ropensci';
res.locals.node_env = req.app.get('env');
next();
});
// check if package/universe exists and handle caching values
app.use('/:package', function(req, res, next){
if(!production){
res.set('Cache-Control', 'no-cache');
return next();
}
const universe = res.locals.universe;
const pkg = req.params.package;
const tabs = ["builds", "packages", "badges", "apis", "datasets", "contributors", "articles"];
const metapage = tabs.includes(pkg);
if(pkg == '_global'){
var query = {};
var cdn_cache = 3600;
} else if (metapage){
var query = {_universes: universe};
var cdn_cache = 60;
} else {
var query = {_user: universe, Package: pkg, _registered: true}; //remotes dont have webpage
var cdn_cache = 30;
}
return get_latest(query).then(function(doc){
//also cache 404 errors below
res.set('Cache-Control', 'public, max-age=60');
//Using 'CDN-Cache-Control' would make nginx also do this and we'd need to refresh twice?
res.set('Cloudflare-CDN-Cache-Control', `public, max-age=60, stale-while-revalidate=${cdn_cache}`);
if(doc){
const etag = `W/"${doc._id}"`;
const date = doc._published.toUTCString();
res.set('ETag', etag);
res.set('Last-Modified', date);
//clients may cache front-end pages for 60s before revalidating.
//revalidation can either be done by comparing Etag or Last-Modified.
//do not set 'must-revalidate' as this will disallow using stale cache when server is offline.
if(etag === req.header('If-None-Match') || date === req.header('If-Modified-Since')){
//todo: also invalidate for updates in frontend itself?
res.status(304).send();
} else {
next(); //proceed to routing
}
} else if(metapage) {
throw createError(404, `Universe not found: ${universe}`);
} else {
// Try to find case insensitive or in other universe
var altquery = {_type: 'src', _nocasepkg: pkg.toLowerCase(), _universes: universe, _registered: true};
return get_latest(altquery).then(function(alt){
if(!alt)
throw createError(404, `Package ${pkg} not found in ${universe}`);
res.redirect(`https://${alt._user}.r-universe.dev/${alt.Package}${req.path.replace(/\/$/, '')}`);
});
};
});
});
app.use('/_global/', globalRouter);
app.use('/', universeRouter);
app.use('/', pkginfoRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404, `Page not found: ${req.path}`));
});
// global error handler
app.use(function(err, req, res, next) {
res.locals.error = err;
res.locals.mode = req.app.get('env')
// render the error page
res.status(err.status || 500);
res.header(err.headers);
res.render('error');
});
export default app;