Skip to content

Commit

Permalink
Updated for Express.js v3. Need to update some plugins to.
Browse files Browse the repository at this point in the history
  • Loading branch information
balupton committed Sep 29, 2012
1 parent 750648d commit 433c97d
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 81 deletions.
13 changes: 13 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
## History

- v6.7.0 Unreleased
- Updated [Express.js](http://expressjs.com/) from v2.5 to v3.0
- If you're doing custom routing, you'll want to check the [Express.js Upgrade Guide](https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x)
- There are now two server objects: `serverExpress` and `serverHttp` - get them using `docpadInstance.getServer(true)`, set them using `docpad.setServer({serverExpress,serverHttp})` - `server` in events, and `docpadInstance.getServer()` return the `serverExpress` object for backwards compatibility (however things like socket.io require the `serverHttp` object)
- Abstracted out the different middlewares to `serverMiddlewarePowering`, `serverMiddlewareRouter`, `serverMiddleware404`, and `serverMiddleware500`
- Added the following options to the `server` actions:
- `serverExpress` for a custom express.js server
- `serverHttp` for a custom http server
- `middlewareBody` set it to `false` for us to not add the `bodyParser` middleware
- `middlewareOverride` set it to `false` for us to not add the `methodOverride` middleware
- `middleware404` set it to `false` for us to not add our `404` middleware
- `middleware500` set it to `false` for us to not add our `500` middleware

- v6.6.8 September 29, 2012
- Fixed watching setup not completing under some conditions
- Bumped watchr minimum version to v2.1.5
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "docpad",
"version": "6.6.8",
"version": "6.7.0",
"description": "DocPad is a language agnostic document management system. This means you write your website as documents, in whatever language you wish, and DocPad will handle the compiling, templates and layouts for you. For static documents it will generate static files, for dynamic documents it'll re-render them on each request. You can utilise DocPad by itself, or use it as a module your own custom system. It's pretty cool, and well worth checking out. We love it.",
"homepage": "https://github.com/bevry/docpad",
"keywords": [
Expand Down Expand Up @@ -64,7 +64,7 @@
"coffee-script": "1.3.x",
"commander": "0.6.x",
"cson": "1.2.x",
"express": "2.5.x",
"express": "3.0.x",
"growl": "1.4.x",
"mime": "1.2.x",
"query-engine": ">=1.2.3 <1.3",
Expand Down
212 changes: 134 additions & 78 deletions src/lib/docpad.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,18 @@ class DocPad extends EventEmitterEnhanced
getVersion: ->
@version

# The express server instance bound to docpad
serverInstance: null
getServer: ->
@serverInstance
setServer: (value) ->
@serverInstance = value
@
# The express and http server instances bound to docpad
serverExpress: null
serverHttp: null
getServer: (both) ->
{serverExpress,serverHttp} = @
if both
return {serverExpress,serverHttp}
else
return serverExpress
setServer: (servers) ->
@serverExpress = servers.serverExpress
@serverHttp = servers.serverHttp

# The caterpillar instance bound to docpad
loggerInstance: null
Expand Down Expand Up @@ -533,8 +538,10 @@ class DocPad extends EventEmitterEnhanced
# Server

# Server
# A express server that we want docpad to use
server: null
# The Express.js server that we want docpad to use
serverExpress: null
# The HTTP server that we want docpad to use
serverHttp: null

# Extend Server
# Whether or not we should extend the server with extra middleware and routing
Expand All @@ -551,6 +558,16 @@ class DocPad extends EventEmitterEnhanced
# The caching time limit that is sent to the client
maxAge: 86400000

# Which middlewares would you like us to activate
# The standard bodyParser middleware
middlewareBody: true
# The standard methodOverride middleware
middlewareOverride: true
# Our own 404 middleware
middleware404: true
# Our own 500 middleware
middleware500: true


# -----------------------------
# Logging
Expand Down Expand Up @@ -2101,7 +2118,7 @@ class DocPad extends EventEmitterEnhanced
docpad.notify (new Date()).toLocaleTimeString(), title: 'Website generating...'

# Fire plugins
docpad.emitSync 'generateBefore', server: docpad.getServer(), (err) ->
docpad.emitSync 'generateBefore', server:docpad.getServer(), (err) ->
# Forward
return next(err)

Expand Down Expand Up @@ -2230,7 +2247,7 @@ class DocPad extends EventEmitterEnhanced
docpad = @

# Fire plugins
docpad.emitSync 'generateAfter', server: docpad.getServer(), (err) ->
docpad.emitSync 'generateAfter', server:docpad.getServer(), (err) ->
return next(err) if err

# Log generated
Expand Down Expand Up @@ -2697,31 +2714,97 @@ class DocPad extends EventEmitterEnhanced
# Chain
@

# Server Middelware: Powering
serverMiddlewarePowering: (req,res,next) ->
tools = res.get('X-Powered-By').split(/[,\s]+/g)
tools.push 'DocPad'
tools = tools.join(',')
res.set('X-Powered-By',tools)
next()

# Server Middelware: Router
serverMiddlewareRouter: (req,res,next) =>
# Prepare
docpad = @
database = docpad.getDatabase()

# Check
return next() unless database

# Prepare
pageUrl = req.url.replace(/\?.*/,'')
document = database.findOne(urls: $has: pageUrl)
return next() unless document

# Check if we are the desired url
# if we aren't do a permanent redirect
url = document.get('url')
if url isnt pageUrl
console.log(url, url.indexOf?);
return res.redirect(301,url)

# Serve the document to the user
docpad.serveDocument({document,req,res,next})

# Server Middelware: 404
serverMiddleware404: (req,res,next) =>
# Prepare
docpad = @
database = docpad.getDatabase()

# Check
return res.send(500) unless database

# Serve the document to the user
document = database.findOne(relativeOutPath: '404.html')
docpad.serveDocument({document,req,res,next,statusCode:404})

# Server Middelware: 404
serverMiddleware500: (err,req,res,next) =>
# Prepare
docpad = @
database = docpad.getDatabase()

# Check
return res.send(500) unless database

# Serve the document to the user
document = database.findOne(relativeOutPath: '500.html')
docpad.serveDocument({document,req,res,next,statusCode:500,err})

# Server
server: (opts,next) =>
# Require
express = require('express')
# Requires
http = null
express = null

# Prepare
[opts,next] = balUtil.extractOptsAndCallback(opts,next)
docpad = @
config = @config
server = null
serverExpress = null
serverHttp = null

# Config
opts.middlewareBody ?= config.middlewareBody
opts.middlewareOverride ?= config.middlewareOverride
opts.middleware404 ?= config.middleware404
opts.middleware500 ?= config.middleware500

# Handlers
complete = (err) ->
return next(err) if err
# Plugins
docpad.emitSync 'serverAfter', {server}, (err) ->
docpad.emitSync 'serverAfter', {server:serverExpress,serverExpress,serverHttp,express}, (err) ->
return next(err) if err
# Complete
docpad.log 'debug', 'Server setup'
return next()
startServer = ->
# Start the server
try
server.listen(config.port)
address = server.address()
serverHttp.listen(config.port)
address = serverHttp.address()
unless address?
throw new Error("Could not start the web server, chances are the desired port #{config.port} is already in use")
serverHostname = if address.address is '0.0.0.0' then 'localhost' else address.address
Expand All @@ -2739,81 +2822,54 @@ class DocPad extends EventEmitterEnhanced
return complete(err) if err

# Server
server = docpad.getServer()
unless server
server = express.createServer()
docpad.setServer(server)
{serverExpress,serverHttp} = docpad.getServer(true)
if !serverExpress and !serverHttp
# Require
http ?= require('http')
express ?= require('express')

# Create
serverExpress = opts.serverExpress or express()
serverHttp = opts.serverHttp or http.createServer(serverExpress)
docpad.setServer({serverExpress,serverHttp})

# Extend the server
unless config.extendServer
# Start the Server
startServer()
else
# Configure the server
server.configure ->
# POST Middleware
server.use express.bodyParser()
server.use express.methodOverride()

# DocPad Header
server.use (req,res,next) ->
tools = res.header('X-Powered-By').split /[,\s]+/g
tools.push 'DocPad'
tools = tools.join(',')
res.header('X-Powered-By',tools)
next()

# Emit the serverExtend event
# So plugins can define their routes earlier than the DocPad routes
docpad.emitSync 'serverExtend', {server,express}, (err) ->
return next(err) if err

# Router Middleware
server.use(server.router)
# Require
express ?= require('express')

# Routing
server.use (req,res,next) ->
# Check
database = docpad.getDatabase()
return next() unless database
# POST Middleware
serverExpress.use(express.bodyParser()) if opts.middlewareBody isnt false
serverExpress.use(express.methodOverride()) if opts.middlewareOverride isnt false

# Prepare
pageUrl = req.url.replace(/\?.*/,'')
document = database.findOne(urls: $has: pageUrl)
return next() unless document
# DocPad Header
serverExpress.use(docpad.serverMiddlewarePowering)

# Check if we are the desired url
# if we aren't do a permanent redirect
url = document.get('url')
if url isnt pageUrl
return res.redirect(url,301)

# Serve the document to the user
docpad.serveDocument({document,req,res,next})
# Emit the serverExtend event
# So plugins can define their routes earlier than the DocPad routes
docpad.emitSync 'serverExtend', {server:serverExpress,serverExpress,serverHttp,express}, (err) ->
return next(err) if err

# Static
if config.maxAge
server.use(express.static(config.outPath,{maxAge:config.maxAge}))
else
server.use(express.static(config.outPath))
# Router Middleware
serverExpress.use(serverExpress.router)

# 404 Middleware
server.use (req,res,next) ->
database = docpad.getDatabase()
return res.send(500) unless database
# Routing
serverExpress.use(docpad.serverMiddlewareRouter)

# Serve the document to the user
document = database.findOne(relativeOutPath: '404.html')
docpad.serveDocument({document,req,res,next,statusCode:404})
# Static
if config.maxAge
serverExpress.use(express.static(config.outPath,{maxAge:config.maxAge}))
else
serverExpress.use(express.static(config.outPath))

# 500 Middleware
server.error (err,req,res,next) ->
database = docpad.getDatabase()
return res.send(500) unless database
# 404 Middleware
serverExpress.use(docpad.serverMiddleware404) if opts.middleware404 isnt false

# Serve the document to the user
document = database.findOne(relativeOutPath: '500.html')
docpad.serveDocument({document,req,res,next,statusCode:500,err})
# 500 Middleware
serverExpress.error(docpad.serverMiddleware500) if opts.middleware500 isnt false

# Start the Server
startServer()
Expand Down
3 changes: 2 additions & 1 deletion src/main.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Requires
pathUtil = require('path')
{DocPad,queryEngine,Backbone,createInstance} = require(__dirname+'/lib/docpad')
{DocPad,queryEngine,Backbone,createInstance,createMiddlewareInstance} = require(__dirname+'/lib/docpad')

# Export
module.exports =
Expand All @@ -9,6 +9,7 @@ module.exports =
queryEngine: queryEngine
Backbone: Backbone
createInstance: createInstance
createMiddlewareInstance: createMiddlewareInstance

# Require a local DocPad file
require: (relativePath) ->
Expand Down

0 comments on commit 433c97d

Please sign in to comment.