jaune-web
Version:
286 lines (206 loc) • 6.24 kB
text/coffeescript
###*
* Source code Koa application
* Alvaro Juste
###
'use strict'
# 3rd
lodash = require 'lodash'
{extend} = lodash
I18n = require 'i18next'
koa = require 'koa'
http = require 'http'
Https = require 'https'
# jaune
{evaluateName} = require('jaune-util').Reflection
register = require '../context'
# constants
AppName = 'appName'
ConfigHttpSection = 'jaune.http'
ConfigInitSection = 'jaune.init'
ConfigLocaleSection = 'jaune.locale'
ConfigErrorSection = 'jaune.error'
EnvType = 'type'
EnvTypeDev = 'development'
###*
* Represents the server. Handles initialization and finalize.
* {Object} configuration Configuration
###
class KoaServer
constructor: (, ) ->
= .getEnvProperty ConfigHttpSection
= .getEnvProperty ConfigInitSection
= .getEnvProperty ConfigLocaleSection
= .getEnvProperty ConfigErrorSection
= koa()
###*
* Set up session
###
setupSession: ->
enabled = store = storeArgs = null
if ?.session?
{enabled, store, storeArgs} = ?.session
return unless enabled is yes
.use evaluateName store, storeArgs, store.context, {}
###*
* Set up static resource serving
###
setupStatic: ->
{enabled, path, maxAge} = .static if ?.static?
return unless enabled is yes
send = require 'koa-send'
.use -> yield send this, , {maxAge, root: path}
###*
* Set up logger
###
setupLogger: ->
return unless .getProcessProperty(EnvType) isnt EnvTypeDev
.use require('koa-logger')()
###*
* Set up custom middlewares
###
setupMiddlewares: ->
.use register.coreMiddleware ,
return unless ?.middlewares?
.middlewares ,
###*
* Set up post middlewares
###
setupPostMiddlewares: ->
return unless ?.postMiddlewares?
.postMiddlewares ,
###*
* Set up body parse
###
setupBody: ->
parse = multipart = strict = uploadPath = null
if ?.body?
{parse, multipart, strict, uploadPath} = .body
return unless parse
koaBody = require 'koa-body'
settings = {multipart, strict}
if multipart and uploadPath
extend settings, formidable: uploadDir: uploadPath
.use koaBody settings
###*
* Set up sockets
###
setupSockets: ->
{sockets} = if ?
return unless sockets
#needs to be installed by client
.sockets = new (require('koa-socket'))()
.sockets.attach
###*
* Set up web serving
###
setupWeb: ->
{enabled, html, https} = .web if ?.web?
return unless enabled
{engine, args, context} = html if html?
if html?
.use evaluateName engine, args, context, {}
###*
* Set up http serving
###
setupHttp: ->
return unless ?
{port, https} =
appName = .getEnvProperty AppName
port = parseInt port ? 3000, 10
if https?
{key, cert} = https
.server = Https.createServer {key, cert}, .callback()
else
.server = http.createServer .callback()
.server.listen port, -> console.log "#{appName} web server started"
###*
* Set up internazionalization
###
setupI18n: ->
{enabled} = .i18n if ?.i18n?
return unless enabled is yes
koaLocale = require 'koa-locale'
httpUtil = .Http.Util
localeManager = .Locale.Manager
koaLocale
.use (next) ->
locale = ?
?
?
localeManager.getDefaultCountry()
if locale?
locale = yield localeManager.setLocale locale
.locale = locale if
= locale
httpUtil.setCookieValue this, 'locale', locale.locale, '1year'
yield next
I18n.init
###*
* Set up routing
###
setupRouting: ->
{webRoutes} = if ?
return unless webRoutes?
.use require('koa-routing')()
webRoutes ,
###*
* Set up routing
###
initDaemons: ->
return unless ?.initDeamons?
.initDeamons()
###*
* extendNamespace
###
extendNamespace: ->
return unless ?.extendNamespace?
.extendNamespace
###*
* Sets up error handling
###
setupErrorHandling: ->
customErrorHandling = ?.listener
env =
.use (next) ->
try
yield next
catch error
return if customErrorHandling? and
yield customErrorHandling error, this
yield handleError this, env, error
###*
* Sets up modules to be used by the server.
###
setup: ->
.keys = ["some secret hurr"] # TODO: make it configurable
.use register.respondersMiddleware ,
,
handleError = (ctx, env, err) ->
if env.getProcessProperty(EnvType) is EnvTypeDev
console.log err
if err.stack
stack = err
.stack
.replace /^[^\(]+?[\n$]/gm, ''
.replace /^\s+at\s+/gm, ''
.replace /^Object.<anonymous>\s*\(/gm, '{anonymous}()@'
.split '\n'
console.log stack
switch ctx.accepts 'html', 'json'
when 'json' then yield ctx.jaune.responder.json.sendError err
when 'html' then yield ctx.jaune.responder.page.sendError err
else yield ctx.jaune.responder.http.error err
module.exports = App: KoaServer