- 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
import 'babel-polyfill'
import os from 'os'
import path from 'path'
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()}
})
app
  .use(session({key:'SESSION', store: new Store()}))
  .use(cors())
  .use(router.routes())
  .use(router.allowedMethods())
  .use(serve(path.join(__dirname, '/public/'), {}));
  ;
router
  .get('/pages'                 , route.pages.getPages)
  .post('/page'                 , koabody, route.pages.upsertPage)
  .put('/page/:id'              , koabody, route.pages.upsertPage)
  .delete('/page/:id'           , koabody, route.pages.deletePage)
  .get('/promosets'             , route.promosets.getPromosets)
  .post('/promoset'             , koabody, route.promosets.upsertPromoset)
  .put('/promoset/:id'          , koabody, route.promosets.upsertPromoset)
  .get('/crescendos'            , route.crescendos.getCrescendos)
  .get('/mastheads'             , route.mastheads.getMastheads)
  .post('/upload-masthteads'    , koabody, route.mastheads.uploadMastheads)
  .get('/masthead-types'        , route.mastheads.getMastheadTypes)
  .get('/flips'                 , route.flips.getFlips)
  .post('/flip'                 , koabody, route.flips.upsertFlip)
  .put('/flip/:id'              , koabody, route.flips.upsertFlip)
  .post('/login'                , koabody, route.auth.login)
  .post('/upload/:cat'          , koabody, (ctx, next) => route.upload[ctx.params.cat](ctx, next))
/****************************************************
 * 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);