Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stongo committed Jan 14, 2014
0 parents commit 86848d9
Show file tree
Hide file tree
Showing 15 changed files with 422 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

node_modules
npm-debug.log

test/out/
out/

.idea
*.iml
.DS_Store
14 changes: 14 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.travis*
Makefile
Cakefile
History.md

src/
out/*.tester.*
out/*.test.*
out/test/
test/

.idea
*.iml
.DS_Store
125 changes: 125 additions & 0 deletions Cakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# This file was originally created by Benjamin Lupton <[email protected]> (http://balupton.com)
# and is currently licensed under the Creative Commons Zero (http://creativecommons.org/publicdomain/zero/1.0/)
# making it public domain so you can do whatever you wish with it without worry (you can even remove this notice!)
#
# If you change something here, be sure to reflect the changes in:
# - the scripts section of the package.json file
# - the .travis.yml file


# -----------------
# Variables

WINDOWS = process.platform.indexOf('win') is 0
NODE = process.execPath
NPM = if WINDOWS then process.execPath.replace('node.exe','npm.cmd') else 'npm'
EXT = (if WINDOWS then '.cmd' else '')
APP = process.cwd()
BIN = "#{APP}/node_modules/.bin"
CAKE = "#{BIN}/cake#{EXT}"
COFFEE = "#{BIN}/coffee#{EXT}"
OUT = "#{APP}/out"
SRC = "#{APP}/src"
TEST = "#{APP}/test"


# -----------------
# Requires

pathUtil = require('path')
{exec,spawn} = require('child_process')
safe = (next,fn) ->
return (err) ->
return next(err) if err
return fn()


# -----------------
# Actions

clean = (opts,next) ->
(next = opts; opts = {}) unless next?
args = [
'-Rf'
OUT
pathUtil.join(APP,'node_modules')
pathUtil.join(APP,'*out')
pathUtil.join(APP,'*log')
pathUtil.join(TEST,'node_modules')
pathUtil.join(TEST,'*out')
pathUtil.join(TEST,'*log')
]
spawn('rm', args, {stdio:'inherit',cwd:APP}).on('exit',next)

compile = (opts,next) ->
(next = opts; opts = {}) unless next?
spawn(COFFEE, ['-bco', OUT, SRC], {stdio:'inherit',cwd:APP}).on('exit',next)

watch = (opts,next) ->
(next = opts; opts = {}) unless next?
spawn(COFFEE, ['-bwco', OUT, SRC], {stdio:'inherit',cwd:APP}).on('exit',next)

install = (opts,next) ->
(next = opts; opts = {}) unless next?
spawn(NPM, ['install'], {stdio:'inherit',cwd:APP}).on 'exit', safe next, ->
spawn(NPM, ['install'], {stdio:'inherit',cwd:TEST}).on('exit',next)

reset = (opts,next) ->
(next = opts; opts = {}) unless next?
clean opts, safe next, -> install opts, safe next, -> compile opts, next

setup = (opts,next) ->
(next = opts; opts = {}) unless next?
install opts, safe next, ->
compile opts, next

test = (opts,next) ->
(next = opts; opts = {}) unless next?
spawn(NPM, ['test'], {stdio:'inherit',cwd:APP}).on('exit',next)

finish = (err) ->
throw err if err
console.log('OK')


# -----------------
# Commands

# clean
task 'clean', 'clean up instance', ->
clean finish

# compile
task 'compile', 'compile our files', ->
compile finish

# dev/watch
task 'dev', 'watch and recompile our files', ->
watch finish
task 'watch', 'watch and recompile our files', ->
watch finish

# install
task 'install', 'install dependencies', ->
install finish

# reset
task 'reset', 'reset instance', ->
reset finish

# setup
task 'setup', 'setup for development', ->
setup finish

# test
task 'test', 'run our tests', ->
test finish

# test-debug
task 'test-debug', 'run our tests in debug mode', ->
test {debug:true}, finish

# test-prepare
task 'test-prepare', 'prepare out tests', ->
setup finish

4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## History

v2.0.0 August 13, 2013
- Initial commit
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2013 Sparks Creative Limited

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Hapi Server Plugin for [DocPad](http://docpad.org)

A fully customizable Hapi server for docpad.

## Usage (more coming soon)

## License
Licensed under the incredibly [permissive](http://en.wikipedia.org/wiki/Permissive_free_software_licence) [MIT License](http://creativecommons.org/licenses/MIT/)
123 changes: 123 additions & 0 deletions lib/hapi-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
module.exports = function(docpad, settings, port, hostname) {

var Hapi = require('hapi');

// Declare internals

var internals = {};

internals.settings = settings || {};

internals.port = port || 9778;

internals.hostname = hostname || '0.0.0.0';

internals.serverConfig = {
files: {
relativeTo: process.cwd()
},
cache: {
engine: 'memory'
}
};

// Initiate Server

var server = new Hapi.Server(internals.port, internals.serverConfig);

// Pre Handler
// Query .docpad.db before serving static file

var readDatabase = function(request, reply) {

// Query DB

// Serve Document

// If no document, then exit early

internals.serveDocument = function(document) {
if (!document) {
return reply('foo');
}

// Content Type

var contentType = document.outContentType || document.contentType;

// Return content from DB

var content = document.getOutContent();

if (content) {
return reply.file(content);
}
else {
return reply();
}
};

if (request.params.file.indexOf('/') !== 0) {
var filePath = '/' + request.params.file;
}

docpad.getFileByRoute(filePath, function(err,file) {

// Check

if (err || !file) {
return reply();
}

// Check if we are the desired url
// if we aren't do a permanent redirect

var url = file.get('url');
var cleanUrl = docpad.getUrlPathname(url);

if ((url !== cleanUrl) && (url !== request.params.file)) {
return reply.redirect(url);
}

// Serve the file to the user

internals.serveDocument(file);
});

};


// Main route for serving site

internals.routes = [];

var staticRoute = {
method: 'GET',
path: '/{file*}',
handler: {
directory: {
path: 'public_html/'
}
},
config: {
cache: {
expiresIn: 60000 * 60 * 3
},
pre: [{
method: readDatabase,
assign: 'queryEngine'
}]
}
};

internals.routes.push(staticRoute);

if (typeof internals.settings.routes === 'array' && internals.settings.routes.length > 0) {
internals.routes.push.apply(internals.routes, internals.settings.routes);
}

server.route(internals.routes);

return server;

};
43 changes: 43 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "docpad-plugin-hapi",
"version": "2.0.1",
"description": "A fully customizable Hapi server for docpad.",
"homepage": "https://github.com/stongo/docpad-plugin-hapi",
"keywords": [
"docpad",
"docpad-plugin",
"server",
"hapi"
],
"author": "Marcus Stong <[email protected]> (http://github.com/stongo)",
"maintainers": [
"Marcus Stong <[email protected]> (http://github.com/stongo)"
],
"contributors": [
"Marcus Stong <[email protected]> (http://github.com/stongo)"
],
"bugs": {
"url": "https://github.com/stongo/docpad-plugin-hapi/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/stongo/docpad-plugin-hapi.git"
},
"engines": {
"node": ">=0.8",
"docpad": ">=6"
},
"dependencies": {
"hapi": "2.x",
"wrench": "1.5.x",
"path": "~0.4.9"
},
"devDependencies": {
"coffee-script": "~1.6.2"
},
"main": "./out/hapi.plugin.js",
"scripts": {
"publish": "cake compile",
"test": "node ./out/hapi.test.js"
}
}
Loading

0 comments on commit 86848d9

Please sign in to comment.