forked from benbrown/shuttlecraft
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
280 lines (242 loc) · 7.86 KB
/
index.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
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
// index.js is the start point for our project
// middleware to parse request bodies in different formats (e.g., JSON)
import bodyParser from 'body-parser';
// middleware to parse cookies in incoming requests
import cookieParser from 'cookie-parser';
// Cross-Origin Resource Sharing (CORS) middleware for enabling cross-origin requests
import cors from 'cors';
// the dotenv module for loading environment variables from a .env file
import dotenv from 'dotenv';
// the Express.js framework for building web applications
import express from 'express';
// the Handlebars view engine for rendering dynamic HTML content
import { create } from 'express-handlebars';
// the built-in Node.js HTTP module for creating an HTTP server
import http from 'http';
// the Moment.js library for handling dates and times
import moment from 'moment';
// ActivityPub for handling ActivityPub requests
import { ActivityPub } from './lib/ActivityPub.js';
// Check if account already exists
import { ifAccount } from './lib/account.js';
// Authentication middleware
import { handleAuthenticatedUser } from './lib/authentication.js';
import {
UserProfileRouter,
WebfingerRouter,
inbox,
outbox,
admin,
notes,
publicFacing,
accountHandler
} from './routes/index.js';
// load process.env from .env file
dotenv.config();
const DOMAIN = process.env.DOMAIN;
const PORT = process.env.PORT;
// const envVariables = ['USER_NAME', 'PASS', 'DOMAIN'];
const envVariables = ['DOMAIN'];
const PATH_TO_TEMPLATES = './design';
/**
* Check the existence of required environment variables.
*
* @param {string[]} env_variables - An array of environment variable names that are required.
* @throws {Error} Throws an error and exits the process if any required environment variable is missing.
*/
function checkRequiredEnvironmentVariables(envVariables) {
envVariables.forEach(reqdVariable => {
/**
* Check if the required environment variable is missing.
* If missing, log an error message and exit the process.
*
* @example
* // Example usage:
* checkRequiredEnvironmentVariables(['PORT', 'DATABASE_URL']);
*/
if (!process.env[reqdVariable]) {
console.error(`Missing required environment variable: \`${reqdVariable}\`. Exiting.`);
process.exit(1);
}
});
}
checkRequiredEnvironmentVariables(envVariables);
export const app = express();
// Export app
// module.exports = app;
/**
* Handlebars helper functions for custom template rendering.
*
* @typedef {Object} HandlebarsHelpers
* @property {Function} isVideo - Check if a string contains 'video' and execute the provided block if true.
* @property {Function} isImage - Check if a string contains 'image' and execute the provided block if true.
* @property {Function} isEq - Check if two values are equal and execute the provided block if true.
* @property {Function} or - Logical OR between two values.
* @property {Function} timesince - Format a date to show the time elapsed since the specified date.
* @property {Function} getUsername - Get the username using the ActivityPub module.
* @property {Function} stripProtocol - Remove 'https://' from the beginning of a string.
* @property {Function} stripHTML - Remove HTML tags from a string.
*/
/**
* Create an instance of Handlebars with custom helpers.
*
* @type {Handlebars}
* @see {@link https://handlebarsjs.com/api-reference/helpers.html}
*/
const hbs = create({
helpers: {
/**
* Check if a string contains 'video' and execute the provided block if true.
* @function
* @param {string} str - The string to check.
* @param {Object} options - Handlebars options object.
* @returns {string} - The rendered block if the condition is true.
*/
isVideo: (str, options) => {
if (str && str.includes('video')) return options.fn(this);
},
/**
* Check if a string contains 'image' and execute the provided block if true.
* @function
* @param {string} str - The string to check.
* @param {Object} options - Handlebars options object.
* @returns {string} - The rendered block if the condition is true.
*/
isImage: (str, options) => {
if (str && str.includes('image')) return options.fn(this);
},
/**
* Check if two values are equal and execute the provided block if true.
* @function
* @param {*} a - The first value.
* @param {*} b - The second value.
* @param {Object} options - Handlebars options object.
* @returns {string} - The rendered block if the condition is true.
*/
isEq: (a, b, options) => {
// eslint-disable-next-line
if (a == b) return options.fn(this);
},
/**
* Logical OR between two values.
* @function
* @param {*} a - The first value.
* @param {*} b - The second value.
* @param {Object} options - Handlebars options object.
* @returns {*} - The result of the logical OR operation.
*/
or: (a, b, options) => {
return a || b;
},
/**
* Format a date to show the time elapsed since the specified date.
* @function
* @param {Date} date - The date to be formatted.
* @returns {string} - The formatted time elapsed string.
*/
timesince: date => {
return moment(date).fromNow();
},
/**
* Get the username using the ActivityPub module.
* @function
* @param {*} user - The user object.
* @returns {string} - The username.
*/
getUsername: user => {
return ActivityPub.getUsername(user);
},
/**
* Remove 'https://' from the beginning of a string.
* @function
* @param {string} str - The string to process.
* @returns {string} - The string with 'https://' removed.
*/
stripProtocol: str => str.replace(/^https:\/\//, ''),
/**
* Remove HTML tags from a string.
* @function
* @param {string} str - The string containing HTML tags.
* @returns {string} - The string with HTML tags removed.
*/
stripHTML: str =>
str
.replace(/<\/p>/, '\n')
.replace(/(<([^>]+)>)/gi, '')
.trim()
}
});
const setExpressApp = app => {
app.set('domain', DOMAIN);
app.set('port', process.env.PORT || PORT || 3000);
app.set('port-https', process.env.PORT_HTTPS || 8443);
app.engine('handlebars', hbs.engine);
app.set('views', PATH_TO_TEMPLATES);
app.set('view engine', 'handlebars');
app.use(
bodyParser.json({
type: 'application/activity+json'
})
); // support json encoded bodies
app.use(
bodyParser.json({
type: 'application/json'
})
); // support json encoded bodies
app.use(
bodyParser.json({
type: 'application/ld+json'
})
); // support json encoded bodies
app.use(cookieParser());
app.use(
bodyParser.urlencoded({
extended: true
})
); // support encoded bodies
};
setExpressApp(app);
const authWrapper = (req, res, next) => {
if (ifAccount()) {
handleAuthenticatedUser(req, res, next);
} else {
res.redirect('/account/create');
}
};
console.log(`ACCESS DASHBOARD: https://${DOMAIN}/private`);
// set up globals
app.set('domain', DOMAIN);
// app.set('account', myaccount);
// serve webfinger response
app.use('/.well-known/webfinger', cors(), WebfingerRouter);
// server user profile and follower list
app.use('/u', cors(), UserProfileRouter);
// serve individual posts
app.use('/m', cors(), notes);
// handle incoming requests
app.use('/api/inbox', cors(), inbox);
app.use('/api/outbox', cors(), outbox);
// serve account creation and login
app.use(
'/account',
cors({
credentials: true,
origin: true
}),
accountHandler
);
// serve user dashboard
app.use(
'/private',
cors({
credentials: true,
origin: true
}),
authWrapper,
admin
);
app.use('/', cors(), publicFacing);
app.use('/', express.static('public/'));
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});