forked from browserify/wzrd.in
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.js
43 lines (37 loc) · 1.18 KB
/
admin.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
var basic = require('basic')
module.exports = function (app, bundle) {
app.get('/admin/cull/:module', handle)
var user = process.env['CDN_ADMIN_USER']
var pass = process.env['CDN_ADMIN_PASS']
var auth = basic(function (u, p, callback) {
if (user === u && pass === p) return callback(null)
callback(401)
})
function handle(req, res) {
res.setHeader('content-type', 'application/json')
if (!user || !pass) {
res.statusCode = 401
return res.end(JSON.stringify({error: 'Unauthorized'}))
}
auth(req, res, function(err) {
if (err) {
res.statusCode = 401
res.setHeader("WWW-Authenticate", "Basic realm=\"Secure Area\"")
return res.end(JSON.stringify({error: 'Unauthorized'}))
}
return cull(req, res)
})
}
function cull(req, res) {
var module = req.params.module
bundle.cache.aliases.db.del(module, function(err1) {
bundle.cache.multibundles.db.del(module, function(err2) {
if (err1 || err2) {
res.statusCode = 500
return res.end(JSON.stringify({error: 'Error culling cache'}))
}
res.end(JSON.stringify({culled: module}))
})
})
}
}