import {Server} from 'hapi'
import Logger from '@modulus/logger'
import Mongoose from 'mongoose'

import Auth from './handlers/auth'
import Config from '../config'
import CronJob from '../server/jobs'
import GithubActivity from '../server/jobs/github-activity'
import Plugins from './plugins'
import Routes from './routes'

let logger = Logger('server/index')
let server = new Server()

Mongoose.connect(Config.mongo.url, (err) => {
  if (err) {
    logger.error(`Mongoose.connect error: ${err.message}`)
    throw err
  }

  logger.info(`Connected to ${Config.mongo.url}`)
})

server.connection({
  host: Config.env !== 'production' ? Config.host : null,
  port: parseInt(Config.port, 10)
})

server.register(Plugins, (err) => {
  if (err) {
    logger.error(`server.register error: ${err.message}`)
    throw err
  }
})

server.auth.strategy('simple', 'bearer-access-token', {
  allowQueryToken: true,
  allowMultipleHeaders: false,
  accessTokenName: 'auth_token',
  validateFunc: Auth.validate
})

server.auth.default({
  strategy: 'simple'
})

server.start((err) => {
  if (err) {
    logger.error(`server.start error: ${err.message}`)
    throw err
  }

  logger.info(`Server starting at ${server.info.uri}`)

  server.route(Routes)

  CronJob(GithubActivity, Config.jobs.frequency.githubActivity)
})

export default server

@@ -254,16 +254,70 @@
 tivity'%0A
+import UpdateCache from '../server/jobs/update-cache'%0A
 import P
@@ -1351,16 +1351,74 @@
 tivity)%0A
+  CronJob(UpdateCache, Config.jobs.frequency.updateCache)%0A
 %7D)%0A%0Aexpo

Add update cache job