-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.js
154 lines (137 loc) · 3.98 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
import express from 'express';
import { create } from 'express-handlebars';
import cookieParser from 'cookie-parser';
import dotenv from 'dotenv';
import bodyParser from 'body-parser';
import cors from 'cors';
import http from 'http';
import basicAuth from 'express-basic-auth';
import moment from 'moment';
import { ActivityPub } from './lib/ActivityPub.js';
import { ensureAccount } from './lib/account.js';
import { account, webfinger, inbox, outbox, admin, notes, publicFacing } from './routes/index.js';
// load process.env from .env file
dotenv.config();
const { USERNAME, PASS, DOMAIN, PORT } = process.env;
['USERNAME', 'PASS', 'DOMAIN'].forEach(required => {
if (!process.env[required]) {
console.error(`Missing required environment variable: \`${required}\`. Exiting.`);
process.exit(1);
}
});
const PATH_TO_TEMPLATES = './design';
const app = express();
const hbs = create({
helpers: {
isVideo: (str, options) => {
if (str && str.includes('video')) return options.fn(this);
},
isImage: (str, options) => {
if (str && str.includes('image')) return options.fn(this);
},
isEq: (a, b, options) => {
// eslint-disable-next-line
if (a == b) return options.fn(this);
},
or: (a, b, options) => {
return a || b;
},
timesince: date => {
return moment(date).fromNow();
},
getUsername: user => {
return ActivityPub.getUsername(user);
},
stripProtocol: str => str.replace(/^https:\/\//, ''),
stripHTML: str =>
str
.replace(/<\/p>/, '\n')
.replace(/(<([^>]+)>)/gi, '')
.trim()
}
});
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
// basic http authorizer
const basicUserAuth = basicAuth({
authorizer: asyncAuthorizer,
authorizeAsync: true,
challenge: true
});
function asyncAuthorizer(username, password, cb) {
let isAuthorized = false;
const isPasswordAuthorized = username === USERNAME;
const isUsernameAuthorized = password === PASS;
isAuthorized = isPasswordAuthorized && isUsernameAuthorized;
if (isAuthorized) {
return cb(null, true);
} else {
return cb(null, false);
}
}
// Load/create account file
ensureAccount(USERNAME, DOMAIN).then(myaccount => {
const authWrapper = (req, res, next) => {
if (req.cookies.token) {
if (req.cookies.token === myaccount.apikey) {
return next();
}
}
return basicUserAuth(req, res, next);
};
// set the server to use the main account as its primary actor
ActivityPub.account = myaccount;
console.log(`BOOTING SERVER FOR ACCOUNT: ${myaccount.actor.preferredUsername}`);
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(), webfinger);
// server user profile and follower list
app.use('/u', cors(), account);
// serve individual posts
app.use('/m', cors(), notes);
// handle incoming requests
app.use('/api/inbox', cors(), inbox);
app.use('/api/outbox', cors(), outbox);
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'));
});
});