-
Notifications
You must be signed in to change notification settings - Fork 75
/
index.js
64 lines (49 loc) · 1.73 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
'use strict';
var passport = require.main.require('passport');
var winston = require.main.require('winston');
var BearerStrategy = require('passport-http-bearer').Strategy;
var meta = require.main.require('./src/meta');
var auth = require('./lib/auth');
var sockets = require('./lib/sockets');
var API = {};
API.init = function (data, callback) {
// API Versions
var routes = require('./routes')(data.middleware);
data.router.use('/api/v1', routes.v1);
data.router.use('/api/v2', routes.v2);
// Set up HTTP bearer authentication via Passport
passport.use(new BearerStrategy({}, function (token, done) {
// Find the user by token. If there is no user with the given token, set
// the user to `false` to indicate failure. Otherwise, return the
// authenticated `user`. Note that in a production-ready application, one
// would want to validate the token for authenticity.
auth.verifyToken(token, done);
}));
require('./routes/admin')(data.router, data.middleware); // ACP
sockets.init(); // WebSocket listeners
API.reloadSettings();
callback();
};
API.addMenuItem = function (custom_header, callback) {
custom_header.plugins.push({
route: '/plugins/write-api',
icon: 'fa-cogs',
name: 'Write API',
});
callback(null, custom_header);
};
API.authenticate = async (data) => {
await require('./routes/v2/middleware').requireUser(data.req, data.res, data.next);
};
API.associateUser = require('./routes/v2/middleware').associateUser;
API.reloadSettings = function (hash) {
if (!hash || hash === 'settings:writeapi') {
meta.settings.get('writeapi', function (err, settings) {
if (err) {
winston.warn('[plugins/write-api] Unable to reload settings');
}
API.settings = settings;
});
}
};
module.exports = API;