summaryrefslogtreecommitdiff
path: root/examples/lib/okpush/index.js
blob: e8b654910e0252420c1b7badab5570043ccf4285 (plain)
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
/**
 */

var OKTemplate = require('../../../app/node_modules/oktemplate')
var passport = require('passport')
var apn = require('./apn')
var db = require('./db')

passport.use(new DigestStrategy({qop: 'auth'},
    function authenticate(username, done) {
  if (!process.env.OK_USER || !process.env.OK_PASS) {
    return done(new Error('No user or pass configured on server'))
  } else {
    return done(null, process.env.OK_USER, process.env.OK_PASS)
  }
}))

function OKPush (options) {
  if (!(this instanceof OKPush)) return new OKPush(options)
  options = options || {}
  if (!options.express)
    throw new Error('Express not provided to OKPush')
  if (!options.config)
    throw new Error('Configuration not provided to OKPush')
  if (!options.config.notifications)
    throw new Error('Notifications not defined in OKPush')
  
  var express = options.express
  var router = express.Router()
  var config = options.config
  var meta = options.meta
  var error = options.errorHandler
  // var okcms_db = options.db
  
  var templateProvider = this._templateProvider = new OKTemplate({
    root: path.join(__dirname, './templates'),
    debug: meta.debug
  })

  var templates = {}
  templates['index'] = templateProvider.getTemplate('index')
  
  apn.init()
  db.init()

  router.use('/admin/', passport.initialize())
  router.all('/admin/(:path*)?', passport.authenticate('digest', {
    session: false
  }))

  // pass in admin middleware!
  router.get('/admin/', function (req, res) {
    var data = {
      meta: meta,
      notifications: config.notifications,
    }
    templates['index'].render(data).then(function(rendered) {
      res.send(rendered);
    }).fail(error(req, res, 500))
  })

  router.post('/send', function (req, res) {
    // add a key
  })

  // should work without middleware
  router.post('/add', function (req, res) {
    // add a key
  })

  this._router = router
}

OKExample.prototype.middleware = function () {
  return this._router
}

module.exports = OKExample