'use strict'

import { defaultRoute, user, api, savePostAPI, listPostsAPI, getPostAPI, getPostView, updatePostAPI, deletePostAPI, loginPage, signupPage, logout } from './routes'

const express = require('express')
const config = require('./config')
const engine = require('consolidate')
const root = require('./util')
const passport = require('passport')
const flash = require('connect-flash')
const Logger = require('./logger').Logger
const bodyParser = require('body-parser')
const methodOverride = require('method-override')
const session = require('express-session')
const cookieParser = require('cookie-parser')
const favicon = require('express-favicon')

const pring = express()
const routerAPI = express.Router()

require('./middleware/auth/passport')(passport)

function isLoggedin (req, res, next) {
  if (req.isAuthenticated()) return next()
  res.redirect('/')
}

pring.use(bodyParser.urlencoded({ extended: true }))
pring.use(bodyParser.json())
pring.use(cookieParser())
pring.use(methodOverride())
pring.use(favicon(`${root.path()}/public/favicon.ico`))

pring.use(session({
  secret: 'pagedekisasimplestaticgenerator',
  resave: true,
  saveUninitialized: true
}))

pring.use(passport.initialize())
pring.use(passport.session())
pring.use(flash())
pring.use(express.static(`${root.path()}/public`))
pring.set('views', `${root.path()}/views`)
pring.engine('html', engine.handlebars)
pring.set('view engine', 'html')

config.get('passport:authentication') ? pring.get('/', defaultRoute) : pring.get('/', user)

routerAPI.route('/').get(api)
routerAPI.route('/posts')
  .post(savePostAPI)
  .get(listPostsAPI)
routerAPI.route('/posts/:id')
  .get(getPostAPI)
  .put(updatePostAPI)
  .delete(deletePostAPI)

pring.use('/api', routerAPI)

pring.get('/posts/:id', getPostView)

pring.post('/login', passport.authenticate('local-login', {
  successRedirect: '/user',
  failureRedirect: '/login',
  failureFlash: true
}))
pring.get('/login', loginPage)

pring.post('/signup', passport.authenticate('local-signup', {
  successRedirect: '/user',
  failureRedirect: '/signup',
  failureFlash: true
}))
pring.get('/signup', signupPage)

pring.get('/logout', logout)
pring.get('/user', isLoggedin, user)

pring.listen(config.get('express:port'), () => Logger.info(`Blogel is running on port ${config.get('express:port')}`))

process.on('unhandledRejection', (err) => Logger.error(err.stack))

module.exports = { express, pring }

@@ -18,309 +18,69 @@
 ort 
-%7B defaultRoute, user, api, savePostAPI, listPostsAPI, getPostAPI, getPostView, updatePostAPI, deletePostAPI, loginPage, signupPage, logout %7D from './routes'%0A%0Aconst express = require('express')%0Aconst config = require('./config')%0Aconst engine = require('consolidate')%0Aconst root = require('./util')%0Acons
+express from 'express'%0Aimport engine from 'consolidate'%0Aimpor
 t pa
@@ -86,26 +86,21 @@
 assport 
-= require(
+from 
 'passpor
@@ -105,22 +105,22 @@
 ort'
-)%0Acons
+%0Aimpor
 t flash 
 = re
@@ -115,26 +115,21 @@
 t flash 
-= require(
+from 
 'connect
@@ -139,56 +139,14 @@
 ash'
-)%0Aconst Logger = require('./logger').Logger%0Acons
+%0Aimpor
 t bo
@@ -154,26 +154,21 @@
 yParser 
-= require(
+from 
 'body-pa
@@ -172,22 +172,22 @@
 -parser'
-)%0Acons
+%0Aimpor
 t method
@@ -195,26 +195,21 @@
 verride 
-= require(
+from 
 'method-
@@ -217,22 +217,22 @@
 verride'
-)%0Acons
+%0Aimpor
 t sessio
@@ -233,26 +233,21 @@
 session 
-= require(
+from 
 'express
@@ -255,22 +255,22 @@
 session'
-)%0Acons
+%0Aimpor
 t cookie
@@ -276,26 +276,21 @@
 eParser 
-= require(
+from 
 'cookie-
@@ -296,22 +296,22 @@
 -parser'
-)%0Acons
+%0Aimpor
 t favico
@@ -316,18 +316,13 @@
 con 
-= require(
+from 
 'exp
@@ -334,18 +334,182 @@
 favicon'
-)
 %0A
+%0Aimport * as r from './routes'%0Aimport Api from './routes/api'%0Aimport config from './config'%0A%0Aconst root = require('./util')%0Aconst Logger = require('./logger').Logger
 %0Aconst p
@@ -561,16 +561,43 @@
 uter()%0A%0A
+let blogelApp = new Api()%0A%0A
 require(
@@ -1352,16 +1352,18 @@
 et('/', 
+r.
 defaultR
@@ -1385,16 +1385,18 @@
 et('/', 
+r.
 user)%0A%0Ar
@@ -1423,12 +1423,24 @@
 get(
-api)
+blogelApp.info)%0A
 %0Arou
@@ -1470,16 +1470,26 @@
   .post(
+blogelApp.
 savePost
@@ -1500,16 +1500,18 @@
 %0A  .get(
+r.
 listPost
@@ -1553,16 +1553,18 @@
 %0A  .get(
+r.
 getPostA
@@ -1574,16 +1574,26 @@
 %0A  .put(
+blogelApp.
 updatePo
@@ -1609,16 +1609,18 @@
 .delete(
+r.
 deletePo
@@ -1681,16 +1681,18 @@
 s/:id', 
+r.
 getPostV
@@ -1859,16 +1859,18 @@
 login', 
+r.
 loginPag
@@ -2039,16 +2039,18 @@
 ignup', 
+r.
 signupPa
@@ -2075,16 +2075,18 @@
 ogout', 
+r.
 logout)%0A
@@ -2116,16 +2116,18 @@
 ggedin, 
+r.
 user)%0A%0Ap

Use better import