forked from pirxpilot/liftie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
97 lines (82 loc) · 2.46 KB
/
Copy pathapp.js
File metadata and controls
97 lines (82 loc) · 2.46 KB
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
import http from 'node:http';
import path from 'node:path';
import connect from '@pirxpilot/connect';
import cookieParser from '@pirxpilot/cookie-parser';
import { compile } from '@pirxpilot/jade-core';
import cachifyStatic from 'connect-cachify-static';
import gzip from 'connect-gzip-static';
import renderer from 'connect-renderer';
import errorHandler from 'errorhandler';
import logger from 'morgan';
import lifts from './lib/lifts/index.js';
import loader from './lib/loader.js';
import * as loaders from './lib/loaders.js';
import opening from './lib/opening.js';
import * as plugins from './lib/plugins.js';
import dataRoutes from './lib/routes/data.js';
import routes from './lib/routes/index.js';
import weather from './lib/weather/index.js';
import webcams from './lib/webcams.js';
const app = connect();
export default app;
process.env.PORT ??= 3000;
process.env.SITE_URL ??= `http://localhost:${process.env.PORT}`;
process.env.NODE_ENV ??= 'development';
const root = path.join(path.dirname(new URL(import.meta.url).pathname), 'public');
const { SITE_URL: siteUrl, LIFTIE_STATIC_HOST: staticHost = '' } = process.env;
const cachify = cachifyStatic(root, { format: 'name' });
app.locals = {
min: '.min',
decorateAbout() {},
siteUrl,
staticHost,
serviceWorker: true,
og: {
image: `${staticHost || siteUrl}/img/snowflake-512.png`
}
};
app.use(
renderer(`${path.dirname(new URL(import.meta.url).pathname)}/views`).engine('jade', {
compile,
options: { compileDebug: process.env.NODE_ENV !== 'production' }
})
);
app.use(logger('dev'));
app.use(cookieParser());
app.use(cachify);
app.use(async (req, res, next) => {
req.app = app;
res.locals ??= {};
const fns = await cachify.helpers();
res.locals.cachify = fns.cachify;
next();
});
app.use(gzip(root));
if (process.env.NODE_ENV === 'development') {
app.locals.min = '';
app.use(errorHandler());
}
app.loaders = loaders;
app.loaders.register(loader);
app.plugins = plugins;
app.plugins.register('lifts', lifts);
app.plugins.register('opening', opening);
app.plugins.register('weather', weather);
app.plugins.register('webcams', webcams);
app.data = dataRoutes();
routes(app);
app.run = function run() {
app.data.init(err => {
if (err) {
console.error(err);
process.exit(1);
return;
}
http.createServer(app).listen(process.env.PORT, () => {
console.log(`Running on: http://localhost:${process.env.PORT}`);
});
});
};
if (import.meta.main) {
app.run();
}