Convert to midwest, routes to routers etc etc
This commit is contained in:
parent
a35c3df976
commit
583abc5004
21
package.json
21
package.json
@ -24,29 +24,28 @@
|
|||||||
"cookie-parser": "^1.4.3",
|
"cookie-parser": "^1.4.3",
|
||||||
"dollr": "0.0.9",
|
"dollr": "0.0.9",
|
||||||
"express": "^4.14.0",
|
"express": "^4.14.0",
|
||||||
"express-module-membership": "github:thecodebureau/express-module-membership",
|
|
||||||
"express-service-errors": "github:thecodebureau/express-service-errors",
|
"express-service-errors": "github:thecodebureau/express-service-errors",
|
||||||
"express-session": "^1.14.1",
|
"express-session": "^1.14.1",
|
||||||
"lodash": "^4.15.0",
|
"lodash": "^4.15.0",
|
||||||
"marko": "^3.7.2",
|
"marko": "^3.10.1",
|
||||||
"marko-widgets": "^6.3.4",
|
"marko-widgets": "^6.3.4",
|
||||||
|
"midwest": "0.0.2",
|
||||||
|
"midwest-module-membership": "github:thebitmill/midwest-module-membership",
|
||||||
|
"midwest-service-errors": "github:thebitmill/midwest-service-errors",
|
||||||
"mongoose": "^4.6.0",
|
"mongoose": "^4.6.0",
|
||||||
"mongopot": "github:lohfu/mongopot",
|
"mongopot": "github:lohfu/mongopot",
|
||||||
"morgan": "^1.7.0",
|
"morgan": "^1.7.0",
|
||||||
"nodemailer": "^2.6.0",
|
"nodemailer": "^2.6.0",
|
||||||
"passport": "^0.3.2",
|
"passport": "^0.3.2",
|
||||||
"passport-local": "^1.0.0",
|
"passport-local": "^1.0.0",
|
||||||
"require-dir": "^0.3.0",
|
"require-dir": "^0.3.0"
|
||||||
"warepot": "github:lohfu/warepot"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"autoprefixer": "^6.4.1",
|
"autoprefixer": "^6.4.1",
|
||||||
"babel-plugin-add-module-exports": "^0.2.1",
|
"babel-plugin-add-module-exports": "^0.2.1",
|
||||||
"babel-preset-es2015": "^6.14.0",
|
"babel-preset-es2015": "^6.14.0",
|
||||||
"babel-preset-react": "^6.11.1",
|
"babel-preset-react": "^6.11.1",
|
||||||
"babelify": "^7.3.0",
|
|
||||||
"browser-sync": "^2.16.0",
|
"browser-sync": "^2.16.0",
|
||||||
"browserify": "^13.1.0",
|
|
||||||
"chalk": "^1.1.3",
|
"chalk": "^1.1.3",
|
||||||
"csswring": "^5.1.0",
|
"csswring": "^5.1.0",
|
||||||
"eslint": "^3.5.0",
|
"eslint": "^3.5.0",
|
||||||
@ -65,15 +64,19 @@
|
|||||||
"gulp-uglify": "^2.0.0",
|
"gulp-uglify": "^2.0.0",
|
||||||
"gulp-util": "^3.0.7",
|
"gulp-util": "^3.0.7",
|
||||||
"marko": "^3.10.1",
|
"marko": "^3.10.1",
|
||||||
"markoify": "^2.1.1",
|
|
||||||
"mkdirp": "^0.5.1",
|
"mkdirp": "^0.5.1",
|
||||||
"nodemon": "^1.10.2",
|
"nodemon": "^1.10.2",
|
||||||
"postcss": "^5.2.0",
|
"postcss": "^5.2.0",
|
||||||
"pretty-hrtime": "^1.0.2",
|
"pretty-hrtime": "^1.0.2",
|
||||||
"rimraf": "^2.5.4",
|
"rimraf": "^2.5.4",
|
||||||
|
"rollup": "^0.35.10",
|
||||||
|
"rollup-plugin-babel": "^2.6.1",
|
||||||
|
"rollup-plugin-commonjs": "^3.3.0",
|
||||||
|
"rollup-plugin-marko": "0.0.2",
|
||||||
|
"rollup-plugin-node-resolve": "^2.0.0",
|
||||||
|
"rollup-plugin-replace": "^1.1.1",
|
||||||
"through2": "^2.0.1",
|
"through2": "^2.0.1",
|
||||||
"vinyl-buffer": "^1.0.0",
|
"vinyl-buffer": "^1.0.0",
|
||||||
"vinyl-source-stream": "^1.1.0",
|
"vinyl-source-stream": "^1.1.0"
|
||||||
"watchify": "^3.7.0"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,7 +21,7 @@ const defaults = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const ErrorModel = require('mongopot/models/error')
|
const ErrorModel = require('midwest-service-errors/model')
|
||||||
|
|
||||||
function store(error) {
|
function store(error) {
|
||||||
ErrorModel.create(error, (err) => {
|
ErrorModel.create(error, (err) => {
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
smtp: require('./smtp'),
|
|
||||||
site: require('./site')
|
site: require('./site')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -11,11 +10,24 @@ module.exports = {
|
|||||||
subject: 'You have been invited to ' + config.site.title
|
subject: 'You have been invited to ' + config.site.title
|
||||||
},
|
},
|
||||||
|
|
||||||
|
timeouts: {
|
||||||
|
// 1 day
|
||||||
|
changePassword: 24 * 60 * 60 * 1000,
|
||||||
|
// verify email
|
||||||
|
verifyEmail: 7 * 24 * 60 * 60 * 1000
|
||||||
|
},
|
||||||
|
|
||||||
paths: {
|
paths: {
|
||||||
register: '/register',
|
register: '/register',
|
||||||
login: '/login',
|
login: '/login',
|
||||||
forgotPassword: '/forgot-password',
|
forgotPassword: '/forgot-password',
|
||||||
updatePassword: '/update-password'
|
updatePassword: '/change-password'
|
||||||
|
},
|
||||||
|
|
||||||
|
redirects: {
|
||||||
|
login: '/admin',
|
||||||
|
logout: '/',
|
||||||
|
register: '/admin'
|
||||||
},
|
},
|
||||||
|
|
||||||
remember: {
|
remember: {
|
||||||
@ -38,6 +50,7 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
register: {
|
register: {
|
||||||
|
missingProperties: 'Oh no missing stuff',
|
||||||
notAuthorized: 'The email is not authorized to create an account.',
|
notAuthorized: 'The email is not authorized to create an account.',
|
||||||
duplicateEmail: 'The email has already been registered.'
|
duplicateEmail: 'The email has already been registered.'
|
||||||
}
|
}
|
||||||
@ -58,12 +71,10 @@ module.exports = {
|
|||||||
// passReqToCallback: true
|
// passReqToCallback: true
|
||||||
// },
|
// },
|
||||||
|
|
||||||
// google: {
|
},
|
||||||
// clientID: 'change-this-fool',
|
|
||||||
// clientSecret: 'change-this-fool',
|
// needs to be even
|
||||||
// callbackURL: p.join(config.site.domain, '/auth/google/callback'),
|
tokenLength: 64,
|
||||||
// passReqToCallback: true
|
// needs to be even
|
||||||
// }
|
saltLength: 16
|
||||||
//}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,13 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const masterTemplate = require('../src/master.marko')
|
const masterTemplate = require('../client/master.marko')
|
||||||
|
|
||||||
module.exports = [
|
const router = new (require('express')).Router()
|
||||||
[ '/', 'get', (req, res, next) => {
|
|
||||||
res.template = masterTemplate
|
|
||||||
|
|
||||||
next()
|
router.get('/', (req, res, next) => {
|
||||||
} ],
|
res.template = masterTemplate
|
||||||
]
|
|
||||||
|
next()
|
||||||
|
})
|
||||||
|
|
||||||
|
module.exports = router
|
||||||
|
|||||||
@ -33,11 +33,8 @@ const cookieParser = require('cookie-parser')
|
|||||||
// modules > 3rd party
|
// modules > 3rd party
|
||||||
const chalk = require('chalk')
|
const chalk = require('chalk')
|
||||||
|
|
||||||
// modules > warepot
|
// modules > midwest
|
||||||
const colorizeStack = require('warepot/util/colorize-stack')
|
const colorizeStack = require('midwest/util/colorize-stack')
|
||||||
|
|
||||||
// modules > locals
|
|
||||||
const initRoutes = require('warepot/util/init-routes')
|
|
||||||
|
|
||||||
const config = requireDir('./config')
|
const config = requireDir('./config')
|
||||||
|
|
||||||
@ -52,6 +49,19 @@ process.on('uncaughtException', (err) => {
|
|||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// mongoose mpromise library is being deprecated
|
||||||
|
mongoose.Promise = Promise
|
||||||
|
|
||||||
|
// connect to mongodb
|
||||||
|
mongoose.connect(config.mongo.uri, _.omit(config.mongo, 'uri'), (err) => {
|
||||||
|
if (err) {
|
||||||
|
console.error(err)
|
||||||
|
process.exit()
|
||||||
|
}
|
||||||
|
|
||||||
|
console.info('[' + chalk.cyan('INIT') + '] Mongoose is connected.')
|
||||||
|
})
|
||||||
|
|
||||||
const prewares = [
|
const prewares = [
|
||||||
express.static(config.dir.static, ENV === 'production' ? { maxAge: '1 year' } : null),
|
express.static(config.dir.static, ENV === 'production' ? { maxAge: '1 year' } : null),
|
||||||
bodyParser.json(),
|
bodyParser.json(),
|
||||||
@ -65,25 +75,15 @@ const prewares = [
|
|||||||
if (ENV === 'development') {
|
if (ENV === 'development') {
|
||||||
// only log requests to console in development mode
|
// only log requests to console in development mode
|
||||||
prewares.unshift(require('morgan')('dev'))
|
prewares.unshift(require('morgan')('dev'))
|
||||||
prewares.push(require('express-module-membership/passport/automatic-login'))
|
prewares.push(require('midwest-module-membership/passport/automatic-login'))
|
||||||
}
|
}
|
||||||
|
|
||||||
const postwares = [
|
const postwares = [
|
||||||
require('warepot/ensure-found'),
|
require('midwest/middleware/ensure-found'),
|
||||||
// transform and log error
|
// transform and log error
|
||||||
require('warepot/error-handler'),
|
require('midwest/middleware/error-handler'),
|
||||||
// respond
|
// respond
|
||||||
require('warepot/responder'),
|
require('midwest/middleware/responder'),
|
||||||
// handle error rendering error
|
|
||||||
require('warepot/responder-error'),
|
|
||||||
]
|
|
||||||
|
|
||||||
const routes = [
|
|
||||||
...prewares,
|
|
||||||
...require('./pages'),
|
|
||||||
...require('./services/pomodoros/routes'),
|
|
||||||
...require('express-module-membership/routes'),
|
|
||||||
...postwares
|
|
||||||
]
|
]
|
||||||
|
|
||||||
const server = express()
|
const server = express()
|
||||||
@ -108,20 +108,29 @@ server.response.render = function (template) {
|
|||||||
template.render(locals, this)
|
template.render(locals, this)
|
||||||
}
|
}
|
||||||
|
|
||||||
initRoutes(server, routes)
|
// load all prewares
|
||||||
|
server.use(...prewares)
|
||||||
|
|
||||||
// mongoose mpromise library is being deprecated
|
// routes > pages
|
||||||
mongoose.Promise = Promise
|
server.use(require('./pages'))
|
||||||
|
|
||||||
// connect to mongodb
|
// routes > authentication
|
||||||
mongoose.connect(config.mongo.uri, _.omit(config.mongo, 'uri'), (err) => {
|
server.use('/auth', require('midwest-module-membership/passport/router'))
|
||||||
if (err) {
|
|
||||||
console.error(err)
|
|
||||||
process.exit()
|
|
||||||
}
|
|
||||||
|
|
||||||
console.info('[' + chalk.cyan('INIT') + '] Mongoose is connected.')
|
// routes > api > membership
|
||||||
})
|
server.use('/api/roles', require('midwest-module-membership/services/roles/router'))
|
||||||
|
server.use('/api/permissions', require('midwest-module-membership/services/permissions/router'))
|
||||||
|
server.use('/api/invites', require('midwest-module-membership/services/invites/router'))
|
||||||
|
server.use('/api/users', require('midwest-module-membership/services/users/router'))
|
||||||
|
|
||||||
|
// routes > api > errors
|
||||||
|
server.use('/api/errors', require('midwest-service-errors/router'))
|
||||||
|
|
||||||
|
// routes > api > pomodoro
|
||||||
|
server.use('/api/pomodoros', require('./services/pomodoros/router'))
|
||||||
|
|
||||||
|
// load all postwares
|
||||||
|
server.use(...postwares)
|
||||||
|
|
||||||
server.listen(config.port, () => {
|
server.listen(config.port, () => {
|
||||||
console.info('[' + chalk.cyan('INIT') + '] HTTP Server listening on port ' + chalk.magenta('%s') + ' (' + chalk.yellow('%s') + ')', config.port, ENV)
|
console.info('[' + chalk.cyan('INIT') + '] HTTP Server listening on port ' + chalk.magenta('%s') + ' (' + chalk.yellow('%s') + ')', config.port, ENV)
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const _ = require('lodash')
|
const rest = require('midwest/middleware/rest')
|
||||||
|
const formatQuery = require('midwest/middleware/format-query')
|
||||||
const formatQuery = require('warepot/format-query')
|
const paginate = require('midwest/middleware/paginate')
|
||||||
|
|
||||||
const Pomodoro = require('./model')
|
const Pomodoro = require('./model')
|
||||||
|
|
||||||
@ -18,23 +18,6 @@ function create(req, res, next) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function find(req, res, next) {
|
|
||||||
const limit = Math.max(0, req.query.limit) || res.locals.limit
|
|
||||||
|
|
||||||
const query = Pomodoro.find(_.omit(req.query, 'limit', 'sort', 'page'),
|
|
||||||
null,
|
|
||||||
{ sort: req.query.sort || '-startDate', lean: true })
|
|
||||||
|
|
||||||
if (limit)
|
|
||||||
query.limit(limit)
|
|
||||||
|
|
||||||
query.exec((err, pomodoros) => {
|
|
||||||
res.locals.pomodoros = pomodoros
|
|
||||||
|
|
||||||
next(err)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function end(req, res, next) {
|
function end(req, res, next) {
|
||||||
Pomodoro.findByIdAndUpdate(req.params.id, { endDate: new Date() }, (err, pomodoro) => {
|
Pomodoro.findByIdAndUpdate(req.params.id, { endDate: new Date() }, (err, pomodoro) => {
|
||||||
if (err) return next(err)
|
if (err) return next(err)
|
||||||
@ -53,24 +36,13 @@ function getActive(req, res, next) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function patch(req, res, next) {
|
module.exports = Object.assign(rest(Pomodoro), {
|
||||||
Pomodoro.findByIdAndUpdate(req.params.id, req.body, (err, pomodoro) => {
|
|
||||||
if (err) return next(err)
|
|
||||||
|
|
||||||
res.locals.pomodoro = pomodoro
|
|
||||||
|
|
||||||
next()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
create,
|
create,
|
||||||
end,
|
end,
|
||||||
find,
|
|
||||||
formatQuery: formatQuery([ 'limit', 'sort' ], {
|
formatQuery: formatQuery([ 'limit', 'sort' ], {
|
||||||
endDate: 'exists'
|
endDate: 'exists'
|
||||||
}),
|
}),
|
||||||
getActive,
|
getActive,
|
||||||
patch
|
paginate: paginate(Pomodoro, 50)
|
||||||
}
|
})
|
||||||
|
|
||||||
|
|||||||
19
server/services/pomodoros/router.js
Normal file
19
server/services/pomodoros/router.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const router = new (require('express')).Router()
|
||||||
|
|
||||||
|
const mw = require('./middleware')
|
||||||
|
|
||||||
|
const { isAuthenticated } = require('midwest-module-membership/passport/authorization-middleware')
|
||||||
|
|
||||||
|
router.route('/')
|
||||||
|
.get(isAuthenticated, mw.formatQuery, mw.paginate, mw.query)
|
||||||
|
.post(isAuthenticated, mw.create)
|
||||||
|
|
||||||
|
router.route('/:id')
|
||||||
|
.get(isAuthenticated, mw.findById)
|
||||||
|
.patch(isAuthenticated, mw.update)
|
||||||
|
.put(isAuthenticated, mw.replace)
|
||||||
|
.delete(isAuthenticated, mw.remove)
|
||||||
|
|
||||||
|
module.exports = router
|
||||||
@ -1,13 +0,0 @@
|
|||||||
'use strict'
|
|
||||||
|
|
||||||
//const isAuthenticated = require('express-module-membership/passport/authorization-middleware').isAuthenticated;
|
|
||||||
const isAuthenticated = (req, res, next) => next()
|
|
||||||
|
|
||||||
const mw = require('./middleware')
|
|
||||||
|
|
||||||
module.exports = [
|
|
||||||
[ '/api/pomodoros', 'post', [ isAuthenticated, mw.create ]],
|
|
||||||
[ '/api/pomodoros/:id', 'patch', [ isAuthenticated, mw.patch ]],
|
|
||||||
//[ '/api/pomodoros', 'get', [ mw.authorization.isAuthenticated, mw.pomodoros.getActive ]]
|
|
||||||
[ '/api/pomodoros', 'get', [ isAuthenticated, mw.formatQuery, mw.find ]]
|
|
||||||
]
|
|
||||||
Loading…
Reference in New Issue
Block a user