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
171 lines (148 loc) · 5.45 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# coffeemate
# Copyright(c) 2011 Kadir Pekel.
# MIT Licensed
# version info
VERSION = '0.5.1'
# Module dependencies
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} cnt (container coffeemate instance)
# @param {Object} resp
# @param {Object} resp
# @api public
constructor: (@cnt, @req, @resp, @next) ->
# 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 as default.
# It uses sync file read operation to obtain template contents
#
# @param {String} templateName
# @return {String}
# @api public
include: (templateName) ->
templatePath = path.join process.cwd(), @cnt.options.renderDir, "#{templateName}#{@cnt.options.renderExt}"
template = fs.readFileSync templatePath
@cnt.options.renderFunc "#{template}", @
# This method renders the template that read from given templateName
# and writes the output to the client socket stream
#
# You can explicitly set 'layoutName' if want to override the default
# 'renderLayoutName' option value
#
# @param {String} templateName
# @param {String} layoutName
# @api public
render: (templateName, layoutName=@cnt.options.renderLayoutName, layout=@cnt.options.renderLayout) ->
@[@cnt.options.renderLayoutPlaceholder] = templateName
templateName = layout and layoutName or templateName
@resp.end @include templateName
# 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: (@version=VERSION) ->
@options = renderFunc: eco.render, renderDir: '', renderExt: '.eco', renderLayout: yes, renderLayoutName: 'layout', renderLayoutPlaceholder: 'body'
@routeMap = {}
@baseUrl = '/'
connect.HTTPServer.call @, []
# enable nowjs if available
try @now = require('now').initialize(@).now
# This method helps you define sub applications under given base path.
# The context of callback is coffeemate instance itself and any router definition
# in this context will be constructed based on the baseUrl
#
# @param {String} baseUrl
# @param {String} callback
# @api public
sub: (baseUrl, callback) ->
previousBaseUrl = @baseUrl
@baseUrl = baseUrl
callback.call @
@baseUrl = previousBaseUrl
# 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
# Enable special coffeekup templating magic!
#
# @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: ->
self = @
for root, routes of @routeMap
@use root, @middleware.router (app) ->
for route in routes
do (route) ->
app[route.method] route.pattern, (req, resp, next) ->
route.callback.apply new CoffeemateContext(self, req, resp, next)
# 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) ->
@routeMap[@baseUrl] ?= []
@routeMap[@baseUrl].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 err instanceof Error and err.stack or err
# Export Coffeemate as a pre-instantiated instance
# use 'newInstance' method to create another ones
module.exports = new Coffeemate