Skip to content

Commit

Permalink
Finish hot-reload work
Browse files Browse the repository at this point in the history
  • Loading branch information
damassi committed Aug 15, 2017
1 parent dd2e092 commit d7e040e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
24 changes: 16 additions & 8 deletions boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,32 @@ const app = module.exports = express()
app.use(
IpFilter([process.env.IP_BLACKLIST.split(',')], { log: false, mode: 'deny' })
)

// Get an xapp token
artsyXapp.init({
const xappConfig = {
url: process.env.ARTSY_URL,
id: process.env.ARTSY_ID,
secret: process.env.ARTSY_SECRET
}, () => {
}

artsyXapp.init(xappConfig, () => {
app.use(newrelic)

if (isDevelopment) {
const reload = createReloadable(app)
const reloadPaths = ['api', 'client', 'test-reload']
reloadPaths.forEach(reloadPath => reload(path.resolve(__dirname, reloadPath)))
const reloadAndMount = createReloadable(app)

// Enable server-side code hot-swapping on change
app.use('/api', reloadAndMount(path.resolve(__dirname, 'api'), {
mountPoint: '/api'
}))

invalidateUserMiddleware(app)
reloadAndMount(path.resolve(__dirname, 'client'))

// Staging, Prod
} else {
app.use('/api', require('./api'))
doNotShareUserHack(app)
invalidateUserMiddleware(app)
app.use(require('./client'))
}

Expand All @@ -53,8 +62,7 @@ artsyXapp.on('error', (error) => {
process.exit(1)
})

// TODO: Possibly a terrible hack to not share `req.user` between both.
const doNotShareUserHack = (app) => {
const invalidateUserMiddleware = (app) => {
app.use((req, rest, next) => {
req.user = null
next()
Expand Down
2 changes: 1 addition & 1 deletion client/apps/articles_list/routes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ renderArticles = (res, req, result, published) ->
res.locals.sd.HAS_PUBLISHED = published
res.render 'index',
articles: result.articles || []
current_channel: req.user?.get('current_channel')
current_channel: req.user?.get('current_channel')
2 changes: 1 addition & 1 deletion client/apps/edit/routes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ render = (req, res, article) ->
setChannelIndexable = (channel, article) ->
noIndex = sd.NO_INDEX_CHANNELS.split '|'
if noIndex.includes channel.get('id')
article.set 'indexable', false
article.set 'indexable', false
10 changes: 7 additions & 3 deletions lib/reloadable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
export const isDevelopment = process.env.NODE_ENV === 'development'

export function createReloadable (app) {
return (folderPath) => {
return (folderPath, options = {}) => {
if (isDevelopment) {
const {
mountPoint = '/'
} = options

const onReload = (req, res, next) => require(folderPath)(req, res, next)

if (typeof onReload !== 'function') {
Expand Down Expand Up @@ -59,11 +63,11 @@ export function createReloadable (app) {
}
})

app.use((req, res, next) => {
app.use(mountPoint, (req, res, next) => {
onReload(req, res, next)
})

return app
return onReload

// Node env not 'development', exit
} else {
Expand Down

0 comments on commit d7e040e

Please sign in to comment.