This repository has been archived by the owner on Apr 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
coffeemate.coffee
153 lines (131 loc) · 4.58 KB
/
coffeemate.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# coffeemate
# Copyright(c) 2011 Kadir Pekel.
# MIT Licensed
# Module dependencies
sys = require 'sys'
fs = require 'fs'
path = require 'path'
connect = require 'connect'
eco = require 'eco'
# Context object that instantiated in every request to
# form router handlers' and templates' @/this reference
class CoffeemateContext
# constructor
#
# @param {Object} container
# @param {Object} resp
# @param {Object} resp
# @api public
constructor: (@container, @req, @resp) ->
# Simple built-in extension that sends http redirect to client
#
# @param {String} location
# @api public
redirect: (location) ->
@resp.writeHead 301, location: location
@resp.end()
# This method renders the template that read from given templateName
# using eco template engine. It uses async file read operation to obtain
# template contents
#
# @param {String} templateName
# @api public
render: (templateName) ->
templatePath = path.join process.cwd(),
@container.options.renderDir,
"#{templateName}#{@container.options.renderExt}"
fs.readFile templatePath, (err, template) =>
if err then throw err
@resp.end @container.options.renderFunc "#{template}", @
# This method renders and includes the partial template that read and rendered
# from given partial template name.
#
# @param {String} partialName
# @return {String}
# @api public
include: (partialName) ->
partialPath = path.join process.cwd(),
@container.options.renderDir,
"#{partialName}#{@container.options.renderExt}"
partial = fs.readFileSync partialPath
@container.options.renderFunc "#{partial}", @
# Coffeemate core object
# Kindly extends connect.HTTPServer and pours some sugar on it.
#
# @api private
class Coffeemate extends connect.HTTPServer
# Expose CoffeemateContext constructor for external access
Context: CoffeemateContext
# Shorthand reference for CoffeemateContext.prototype to simplify the extension mechanism
context: CoffeemateContext::
# Expose connect's built-in middleware stack explicitly for external access
middleware: connect.middleware
# constructor
#
# @api public
constructor: ->
@options = renderFunc: eco.render, renderDir: '', renderExt: ''
@routes = []
connect.HTTPServer.call @, []
# Factory method for creating new Coffeemate instances
#
# @return {Object}
# @api public
newInstance: ->
new Coffeemate
# Override 'connect.HTTPServer.use' method so if any argument is a Coffeemate instance
# build its internal route stack as a self used middleware.
#
# @param {String|Function}
# @param {Function} handle
# @return {Server}
# @api public
use: (args...) ->
arg.buildRouter() for arg in args when arg instanceof Coffeemate
connect.HTTPServer::use.apply @, args
# sugarize coffeekup!
#
# @api public
coffeekup: (locals) ->
renderFunc = require('coffeekup').render
locals ?= {}
locals.include ?= (partialName) ->
text ck_options.context.include partialName
@options.renderFunc = (tmpl, ctx) -> renderFunc tmpl, context: ctx, locals: locals
# Build connect router middleware from internal route stack and automatically use it.
#
# @api private
buildRouter: ->
@use @middleware.router (app) =>
for route in @routes
do (route) =>
app[route.method] route.pattern, (req, resp) =>
route.callback.apply new CoffeemateContext(@, req, resp)
# Override 'connect.HTTPServer.listen' to create a pre-hook space for
# preparing router definitions
#
# @param {Number} port
# @param {String} hostname
# @param {Function} callback
# @api public
listen: (args...) ->
@buildRouter()
connect.HTTPServer::listen.apply @, args
# Create shorthand middleware definitions using connect's built-in middleware stack
for item of Coffeemate::middleware
do (item) ->
Coffeemate::[item] = (args...) ->
@use @middleware[item].apply @, args
# Create shorthand router definitions using connect's router middleware
for method in Coffeemate::middleware.router.methods
do (method) ->
Coffeemate::[method] = (pattern, callback) ->
@routes.push method: method, pattern: pattern, callback: callback
@
# Handle uncaught exceptions explicitly to prevent node exiting
# current process. Exception stack trace is sent to stderr.
process.on 'uncaughtException', (err) ->
console.error(if err instanceof Error then err.stack else err)
# Export Coffeemate as a pre-instantiated instance
# use 'newInstance' method to create another ones
module.exports = new Coffeemate