- 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
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
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 session from "koa-session2"
import WebSocket from 'ws'
import * as route from './routes'
import cfg from './config'
import Store from './memstore'
import SocketStore from './stores/socket-store'
const serve = require('koa-static')
const app = new Koa()
const router = new Router()
// Start websockets server
const wss = new WebSocket.Server({
perMessageDeflate: false,
port: 3003
})
// Koabody handles form submission data, like formdata
const koabody = new koaBody({
multipart: true,
formidable: {uploadDir: os.tmpdir()}
})
// So bad. TODO find clean way to pass
// sockets down to middleware
global.sockets = new SocketStore(wss)
app
.use(session({key:'RSSESSION', store: new Store()}))
.use(async (ctx, next) => {
console.log('incoming:: ',ctx.req.url)
ctx.session.fruit = 'banana'
//console.log('All sessions: ', ctx.session)
await next()
})
.use(cors())
//.use(serve(path.join(__dirname, '/public/'), {}))
//.use(router.allowedMethods())
.use(router.routes())
;
router
.get('/', async(ctx, next) => {
console.log('Home sessions: ', ctx.session)
await next()
})
//.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, async (ctx, next) => {
await route.auth.login(ctx, next)
ctx.body = {result: 1, payload: 'success'}
console.log('coming back UP login: ', ctx.session.username)
})
.post('/upload/:cat' ,koabody, async (ctx, next) => {
await 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);
/*
Because it's promise-based
It's not like Express which is callback-based
next() simply returns a promise that is resolved when the next chain of middleware is done,
which is what allows a single Koa middleware to act as a before and after
If you are using await next() then you dont have to return is,
because the middleware calling await isn't done until it reaches it's end
I should probably do a small tutorial on how Koa middleware works some day
https://eev.ee/blog/2013/03/03/the-controller-pattern-is-awful-and-other-oo-heresy/
is it simply doing app.clients = [] ? not sure if antipattern, polluting the app namespace like that
catch(NetworkFailure failure) {
// Retry the request now or later
...
}
855-894-1076
xoom
squarecash
*/