Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

add a shortcut json to response.json #123

Closed
wants to merge 7 commits into from

2 participants

Commits on Mar 1, 2012
  1. @imzshh
  2. @imzshh

    enable to pass loaded module into context.include

    imzshh authored
    for supporting nest include like this:
    @include = ->
      @include require './a'
      @include require './b'
Commits on Mar 2, 2012
  1. @imzshh

    add language package support

    imzshh authored
Commits on Mar 10, 2012
  1. @imzshh

    supported passing two or more handlers to routing methods

    imzshh authored
    example: @get '/foo', handler1, handler2
Commits on May 13, 2012
  1. @imzshh
  2. @imzshh
Commits on Jan 25, 2013
  1. @imzshh
This page is out of date. Refresh to see the latest.
Showing with 65 additions and 21 deletions.
  1. +3 −2 package.json
  2. +62 −19 src/zappa.coffee
View
5 package.json
@@ -10,7 +10,8 @@
"socket.io": "0.8.x",
"coffeekup": "0.3.x",
"node-uuid": "1.2.0",
- "uglify-js": "1.1.x"
+ "uglify-js": "1.1.x",
+ "gzippo": "0.1.4"
},
"devDependencies": {
"jsdom": "0.2.10",
@@ -40,4 +41,4 @@
"Kris Molendyke <kris@visibiz.com>",
"Rafael de Oleza Alomar <rafeca@gmail.com>"
]
-}
+}
View
81 src/zappa.coffee
@@ -15,6 +15,7 @@ socketio = require 'socket.io'
jquery = fs.readFileSync(__dirname + '/../vendor/jquery-1.6.4.min.js').toString()
sammy = fs.readFileSync(__dirname + '/../vendor/sammy-0.7.0.min.js').toString()
uglify = require 'uglify-js'
+gzippo = require 'gzippo'
# Soft dependencies:
jsdom = null
@@ -92,8 +93,18 @@ express.View.prototype.__defineGetter__ 'contents', ->
p = @path.replace id + '/', ''
fs.readFileSync p, 'utf8'
+# lang class for loading lang package
+class lang
+ constructor : (@dir, @culture, @context) ->
+
+ get : (name)->
+ require @dir + '/' + name + '.' + @getCulture()
+
+ getCulture : ->
+ @actualCulture ?= if typeof @culture == 'string' then @culture else @culture.call @context, @context
+
# Takes in a function and builds express/socket.io apps based on the rules contained in it.
-zappa.app = (func) ->
+zappa.app = (func, options) ->
context = {id: uuid(), zappa, express}
context.root = path.dirname(module.parent.filename)
@@ -104,7 +115,10 @@ zappa.app = (func) ->
helpers = {}
postrenders = {}
- app = context.app = express.createServer()
+ if options
+ app = context.app = express.createServer options
+ else
+ app = context.app = express.createServer()
io = context.io = socketio.listen(app)
# Reference to the zappa client, the value will be set later.
@@ -124,7 +138,9 @@ zappa.app = (func) ->
for verb in ['get', 'post', 'put', 'del']
do (verb) ->
context[verb] = ->
- if arguments.length > 1
+ if arguments.length > 2
+ route verb: verb, path: arguments[0], handler: Array.prototype.slice.call arguments, 1
+ else if arguments.length > 1
route verb: verb, path: arguments[0], handler: arguments[1]
else
for k, v of arguments[0]
@@ -195,6 +211,8 @@ zappa.app = (func) ->
zappa_middleware =
static: (p = path.join(context.root, '/public')) ->
express.static(p)
+ staticGzip: (p = path.join(context.root, '/public'), options) ->
+ gzippo.staticGzip(p, options)
zappa: ->
(req, res, next) ->
send = (code) ->
@@ -212,7 +230,10 @@ zappa.app = (func) ->
zappa_used = yes if name is 'zappa'
if zappa_middleware[name]
- app.use zappa_middleware[name](arg)
+ if arg instanceof Array
+ app.use zappa_middleware[name].apply null, arg
+ else
+ app.use zappa_middleware[name](arg)
else if typeof express[name] is 'function'
app.use express[name](arg)
@@ -237,17 +258,18 @@ zappa.app = (func) ->
v.apply(context, [context])
context.include = (p) ->
- sub = require path.join(context.root, p)
+ sub = if typeof p == 'string' then require path.join(context.root, p) else p
sub.include.apply(context, [context])
+ context.lang = (culture, dir) ->
+ @_langculture = culture
+ dir ?= path.join(context.root, '/lang')
+ @_langdir = dir
+
# Register a route with express.
route = (r) ->
- if typeof r.handler is 'string'
- app[r.verb] r.path, (req, res) ->
- res.contentType r.contentType if r.contentType?
- res.send r.handler
- else
- app[r.verb] r.path, (req, res, next) ->
+ routeHandler = (handler, contentType) ->
+ (req, res, next) ->
ctx =
app: app
settings: app.settings
@@ -258,6 +280,7 @@ zappa.app = (func) ->
session: req.session
response: res
next: next
+ json: -> res.json.apply res, arguments
send: -> res.send.apply res, arguments
redirect: -> res.redirect.apply res, arguments
render: ->
@@ -267,6 +290,8 @@ zappa.app = (func) ->
for k, v of arguments[0]
render.apply @, [k, v]
+ ctx.lang = new lang context._langdir, context._langculture, ctx
+
render = (args...) ->
# Adds the app id to the view name so that the monkeypatched
# express.View.exists and express.View.contents can lookup
@@ -305,14 +330,24 @@ zappa.app = (func) ->
# Go!
switch app.settings['databag']
- when 'this' then result = r.handler.apply(data, [ctx])
- when 'param' then result = r.handler.apply(ctx, [data])
- else result = r.handler.apply(ctx, [ctx])
+ when 'this' then result = handler.apply(data, [ctx])
+ when 'param' then result = handler.apply(ctx, [data])
+ else result = handler.apply(ctx, [ctx])
- res.contentType(r.contentType) if r.contentType?
+ res.contentType(contentType) if contentType?
if typeof result is 'string' then res.send result
else return result
-
+
+ if typeof r.handler is 'string'
+ app[r.verb] r.path, (req, res) ->
+ res.contentType r.contentType if r.contentType?
+ res.send r.handler
+ else if r.handler instanceof Array
+ for handler in r.handler
+ app[r.verb] r.path, routeHandler handler, r.contentType
+ else
+ app[r.verb] r.path, routeHandler r.handler, r.contentType
+
# Register socket.io handlers.
io.sockets.on 'connection', (socket) ->
c = {}
@@ -409,8 +444,16 @@ zappa.run = ->
else port = (Number) a
when 'number' then port = a
when 'function' then root_function = a
-
- zapp = zappa.app(root_function)
+ when 'object'
+ options = a
+ if options.host then host = options.host
+ if options.port then port = options.port
+ if options.key
+ expressOptions =
+ key : options.key
+ cert : options.cert
+
+ zapp = zappa.app(root_function, expressOptions)
app = zapp.app
if host then app.listen port, host
@@ -453,4 +496,4 @@ module.exports = zappa.run
module.exports.run = zappa.run
module.exports.app = zappa.app
module.exports.adapter = zappa.adapter
-module.exports.version = zappa.version
+module.exports.version = zappa.version
Something went wrong with that request. Please try again.