This repository has been archived by the owner on Oct 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Panagiotis Kosmidis
committed
Feb 12, 2012
1 parent
b153da0
commit 1e681df
Showing
21 changed files
with
550 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,126 +1,113 @@ | ||
// Setup module dependencies | ||
var express = require('express') | ||
, jade = require('jade') | ||
, Settings = require('settings') | ||
, BundleUp = require('bundle-up') | ||
, Gzippo = require('gzippo') | ||
, nib = require('nib') | ||
, stylus = require('stylus') | ||
, i18n = require('i18next') | ||
, chromeframe = require('express-chromeframe') | ||
var | ||
// Modules | ||
Gzippo = require('gzippo') | ||
|
||
// Load System | ||
, System = require('./system') | ||
|
||
// Get application instance | ||
, App = System.createAppInstance() | ||
|
||
// Setup application server | ||
, Server = System.createServerInstance() | ||
|
||
// Get i18n | ||
, i18n = System.getI18nInstance() | ||
|
||
// Get Bundle | ||
, Bundle = System.getBundle() | ||
|
||
// Get Error Handler | ||
, ErrorHandler = System.getErrorHandlerInstance() | ||
|
||
// Get Router | ||
, Router = System.getRouterInstance() | ||
; | ||
|
||
// Load settings | ||
Settings = new Settings(__dirname + '/system/config/environment.js', { globalKey : '$settings' }); | ||
|
||
//Setup application (Server) | ||
var app = module.exports = express.createServer(); | ||
// Bootstrap Application | ||
|
||
//Load i18n | ||
i18n.init({ | ||
lng : 'el-GR' | ||
, fallbackLng : 'en-US' | ||
, resGetPath : $settings.paths.locales | ||
, resSetPath : $settings.paths.locales | ||
}); | ||
|
||
// Bundle assets | ||
BundleUp(app, $settings.paths.config + 'assets', { | ||
staticRoot : $settings.paths.public | ||
, staticUrlRoot : '/' | ||
, bundle : false // Due to a bug with Less files | ||
, compilers: { | ||
stylus: function(str, path) { | ||
return stylus(str) | ||
.set("filename", path) | ||
.set("compress", true) | ||
.use(nib()) | ||
.import("nib") // This imports the nib lib | ||
; | ||
} | ||
} | ||
}); | ||
|
||
// Load System | ||
var System = require('./system'); | ||
|
||
// Get Helpers | ||
var _ErrorHandler = System.loadHelper('error-handler'); | ||
//-Start i18n | ||
i18n.init(); | ||
|
||
//-Development Env configuration | ||
app.configure('development', function() { | ||
//app.use(express.responseTime()); | ||
app.use(express.profiler()); | ||
//app.use(express.logger({ format: ':method :url' })); | ||
//-Start Bundle | ||
Bundle.init({ | ||
bundle : true | ||
}); | ||
|
||
//-Global configuration | ||
app.configure(function(){ | ||
app.enable('case sensitive routes'); | ||
app.enable('strict routing'); | ||
app.set('views', $settings.paths.views); | ||
app.set('view engine', $settings.app.view.engine); | ||
app.use(express.favicon()); | ||
app.use(express.bodyParser()); | ||
app.use(express.methodOverride()); | ||
app.use(express.cookieParser()); | ||
app.use(express.session({ secret: $settings.app.session.secret })); | ||
app.use(i18n.handle); | ||
app.use(express.csrf()); | ||
app.use(require($settings.paths.config + 'modul8')); | ||
//app.use(chromeframe('IE8')) | ||
app.use(app.router); | ||
app.use(Gzippo.staticGzip($settings.paths.public)); | ||
app.use(Gzippo.compress()); | ||
app.use(_ErrorHandler); | ||
}); | ||
|
||
//-Development Env configuration | ||
app.configure('development', function(){ | ||
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | ||
}); | ||
|
||
//-Production Env configuration | ||
app.configure('production', function(){ | ||
app.enable('view cache'); | ||
app.use(express.errorHandler()); | ||
System.configure({ | ||
//-Development Env configuration | ||
development : function () { | ||
|
||
return { | ||
use : [ | ||
App.profiler() | ||
, App.errorHandler({ dumpExceptions: true, showStack: true }) | ||
] | ||
} | ||
}, | ||
|
||
//-Production Env configuration | ||
production : function () { | ||
|
||
return { | ||
enable : [ | ||
'view cache' | ||
], | ||
|
||
use : [ | ||
App.errorHandler() | ||
] | ||
} | ||
}, | ||
|
||
//-Global configuration | ||
global : function () { | ||
|
||
return { | ||
enable : [ | ||
'case sensitive routes' | ||
, 'strict routing' | ||
], | ||
|
||
set : { | ||
'views' : $settings.paths.views | ||
, 'view engine' : $settings.app.view.engine | ||
}, | ||
|
||
use : [ | ||
App.favicon() | ||
, App.bodyParser() | ||
, App.methodOverride() | ||
, App.cookieParser() | ||
, App.session({ secret: $settings.app.session.secret }) | ||
, i18n.getInstance().handle | ||
, App.csrf() | ||
, require($settings.paths.config + 'modul8') | ||
, Server.router | ||
, Gzippo.staticGzip($settings.paths.public) | ||
, Gzippo.compress() | ||
, ErrorHandler.handle | ||
] | ||
} | ||
} | ||
}); | ||
|
||
// Dynamic helpers | ||
i18n.serveClientScript(app) // grab i18next.js in browser | ||
.serveDynamicResources(app) // route which returns all resources in on response | ||
//.serveMissingKeyRoute(app) // route to send missing keys | ||
.registerAppHelper(app) | ||
; | ||
//-Dynamic helpers | ||
i18n.initHelpers(); | ||
|
||
app.dynamicHelpers({ | ||
Server.dynamicHelpers({ | ||
csrf_token : function(req, res) { | ||
return req.session._csrf; | ||
} | ||
}); | ||
|
||
//Setup Socket.IO | ||
// Routes | ||
require($settings.paths.routes)(app); | ||
|
||
//-Error routes | ||
app.all('/404', function(req, res, next){ | ||
throw new _ErrorHandler.NotFound; | ||
}); | ||
//-Routes | ||
Router.init(); | ||
|
||
app.all('/403', function(req, res, next){ | ||
throw new _ErrorHandler.Error403; | ||
}); | ||
|
||
app.all('/500', function(req, res, next){ | ||
next(new Error('keyboard cat!')); | ||
}); | ||
|
||
app.all('/*', function (req, res, next) { | ||
//throw new _ErrorHandler.NotFound; | ||
next(); | ||
}); | ||
//-App Listener | ||
Server.listen($settings.app.port); | ||
console.log("Main application server listening on port %d in %s mode", Server.address().port, Server.settings.env); | ||
|
||
// App Listener | ||
app.listen($settings.app.port); | ||
console.log("Main application server listening on port %d in %s mode", app.address().port, app.settings.env); | ||
module.exports = Server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class Bundle | ||
constructor : (@Server) -> | ||
@bundle = @getInstance() | ||
@_ = require 'underscore' | ||
|
||
# Get bundle instance | ||
getInstance : -> | ||
@bundle = require 'bundle-up' if "undefined" is typeof @bundle | ||
@bundle | ||
|
||
init : (opts) -> | ||
# Private vars | ||
stylus = require 'stylus' | ||
nib = require 'nib' | ||
assets = $settings.paths.config + 'assets' | ||
|
||
# If no object is defined then set an empty object | ||
opts = opts ? {} | ||
|
||
# Set default values | ||
options = | ||
staticRoot : $settings.paths.public | ||
staticUrlRoot : '/' | ||
bundle : false # Due to a bug with Less files | ||
compilers : | ||
stylus : (str, path) -> | ||
stylus(str).set("filename", path).set("compress", true).use(nib()).import "nib" # This imports the nib lib | ||
|
||
|
||
@_.extend options, opts | ||
|
||
# Load bundle and set an instance of bundle | ||
@bundle = @bundle @Server, assets, options | ||
|
||
module.exports = (Server) -> | ||
new Bundle Server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Error 403 | ||
class AccessDenied extends Error | ||
constructor : (msg) -> | ||
@status = 403 | ||
Error.call @msg | ||
|
||
handle : (err, req, res) -> | ||
# respond with html page | ||
if req.accepts("html") | ||
res.render "error/403", | ||
locals: | ||
title: req.i18n.t 'error.NoAccess' | ||
description: "" | ||
author: "" | ||
analyticssiteid: "XXXXXXX" | ||
error: err | ||
status: err.status | ||
layout: false | ||
|
||
return | ||
# respond with json | ||
if req.accepts("json") | ||
res.send error: req.i18n.t 'error.NoAccess' | ||
return | ||
# default to plain-text. send() | ||
res.send req.i18n.t 'error.NoAccess' | ||
|
||
route : (Server, Err) -> | ||
Server.all '/403', (req, res, next) -> | ||
throw new Err() | ||
|
||
module.exports = (msg) -> | ||
new AccessDenied(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Error 404 | ||
class NotFound extends Error | ||
constructor : (msg) -> | ||
Error.call this, msg | ||
Error.captureStackTrace this, arguments.callee | ||
|
||
handle : (err, req, res) -> | ||
# respond with html page | ||
if req.accepts("html") | ||
res.render "error/404", | ||
locals: | ||
title: "404 - Not Found" | ||
description: "" | ||
author: "" | ||
analyticssiteid: "XXXXXXX" | ||
status: 404 | ||
layout: false | ||
|
||
return | ||
# respond with json | ||
if req.accepts("json") | ||
res.send error: "Not found" | ||
return | ||
# default to plain-text. send() | ||
res.send "Not found" | ||
|
||
route : (Server, Err) -> | ||
Server.all '/404', (req, res, next) -> | ||
throw new Err() | ||
|
||
module.exports = (msg) -> | ||
new NotFound(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Dependencies | ||
fs = require 'fs' | ||
path = require 'path' | ||
_ = require 'underscore' | ||
|
||
# Error Handler | ||
class ErrorHandler | ||
constructor : (@Server) -> | ||
loadErrors() | ||
|
||
# Private | ||
|
||
#-Variables | ||
_privateScope = {} | ||
|
||
#-Methods | ||
|
||
# init Errors | ||
loadErrors = -> | ||
_errors = fs.readdirSync "#{__dirname}/Errors/" | ||
_errors.forEach (error) -> | ||
error_name = path.basename error, '.coffee' | ||
_privateScope[error_name] = require "#{__dirname}/Errors/#{error_name}" | ||
|
||
|
||
# Public | ||
|
||
#-Core Handler | ||
handle : (err, req, res, next) -> | ||
if err instanceof Error | ||
if err.handle? | ||
err.handle? err, req, res | ||
else | ||
next err | ||
else | ||
next err | ||
|
||
# ====== Error Routes ======= # | ||
initErrorRoutes : -> | ||
_server = @Server | ||
_.values(_privateScope).forEach (error) -> | ||
error().route _server, error | ||
|
||
module.exports = (Server) -> | ||
new ErrorHandler(Server) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.