Thu Jan 26 2017
Copied to clipboard! Copy reply
  • 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
import 'babel-polyfill'
import os from 'os'
import fs from 'fs-extra'
import exec from 'child_process'
import Koa from 'koa'
import cors from 'kcors'
import Router from 'koa-router'
import koaBody from 'koa-body'
import convert from 'koa-convert'
import session from "koa-session2"
import websockify from 'koa-websocket'
import * as route from './routes'
import cfg from './config'
import Store from './memstore'

const serve = require('koa-static')
const app = websockify(new Koa())
const router = new Router()
const koabody = new koaBody({
  multipart: true,
  formidable: {uploadDir: os.tmpdir()}
})



router.get('/pages'                 , route.pages.getPages)
router.post('/page'                 , koabody, route.pages.upsertPage)
router.put('/page/:id'              , koabody, route.pages.upsertPage)
router.delete('/page/:id'           , koabody, route.pages.deletePage)
router.get('/promosets'             , route.promosets.getPromosets)
router.post('/promoset'             , koabody, route.promosets.upsertPromoset)
router.put('/promoset/:id'          , koabody, route.promosets.upsertPromoset)
router.get('/crescendos'            , route.crescendos.getCrescendos)
router.get('/mastheads'             , route.mastheads.getMastheads)
router.post('/upload-masthteads'    , koabody, route.mastheads.uploadMastheads)
router.get('/masthead-types'        , route.mastheads.getMastheadTypes)
router.get('/flips'                 , route.flips.getFlips)
router.post('/flip'                 , koabody, route.flips.upsertFlip)
router.put('/flip/:id'              , koabody, route.flips.upsertFlip)
router.post('/login'                , koabody, route.auth.login)
router.post('/upload/:cat'          , koabody, (ctx, next) => route.upload[ctx.params.cat](ctx, next))


/*router.post('/upload/:category'     , koabody, (ctx, next) => {
  const category = ctx.params.category
  const upload = new Upload()
  console.log('cat: ', category)
  upload[category]()
})
*/


String.prototype.toWindows = function() {
  return os.platform().match(/win/i) ? this.replace(/\//g, '\\') : this
}

app
  .use(session({key:'SESSION', store: new Store()}))
  .use(cors())
  //.use(convert(session({store: memstore})))

  .use(router.routes())
  .use(router.allowedMethods())
  .use(serve(__dirname + '/public/'.toWindows(), {}));
  ;



/****************************************************
 * Initial setup "script". Just create needed files
 * if they don't yet exist
 ****************************************************/

// Create pages.json (it should certainly already exist though)
fs.readJson(cfg.files.pages, (e, data) => {
  if (!data) {
    fs.outputFileSync(cfg.files.pages, '[]')
  }
})

// Create promosets.json (it should certainly already exist though)
fs.readJson(cfg.files.promosets, (e, data) => {
  if (!data) {
    fs.outputFileSync(cfg.files.promosets, '{}')
  }
})

// Create our schedule.json file with [] in it
fs.readJson(cfg.files.schedule, (e, data) => {
  if (!data) {
    fs.outputFileSync(cfg.files.schedule, '[]')
  }
})

// Create our files dir
fs.ensureDirSync(cfg.dirs.flipFiles)

// Create our public dir so we can serve masthead imgs, etc
fs.ensureDirSync(cfg.dirs.public)

// Create symbolic mastheads folder
fs.ensureSymlinkSync(`${cfg['dirs']['mastheads']}`, `${cfg['dirs']['mastheadsSymlink']}`)



app.listen(3000);