1 | (function() {
|
2 | var express, form, helpers, path;
|
3 | require('sugar');
|
4 | express = require('express');
|
5 | path = require('path');
|
6 | form = require('connect-form');
|
7 | helpers = require('./helpers');
|
8 | module.exports = {
|
9 | start: function(port) {
|
10 | return app.listen(port);
|
11 | },
|
12 | init: function(app_dir, conf) {
|
13 | var uri;
|
14 | global.app = express.createServer(form({
|
15 | keepExtensions: true
|
16 | }));
|
17 | app.conf = conf;
|
18 | app.conf.templates_path = path.join(app_dir, conf.templates_dir);
|
19 | uri = process.env["MONGOHQ_URL"];
|
20 | if (!uri) {
|
21 | uri = "mongodb://localhost/" + conf.database_name;
|
22 | }
|
23 | console.log("Use database: " + uri);
|
24 | app.mongoose = require('mongoose');
|
25 | app.db = app.mongoose.connect(uri);
|
26 | app.configure(function() {
|
27 | app.use(express.methodOverride());
|
28 | app.use(express.bodyParser());
|
29 | app.use(express.cookieParser());
|
30 | app.use(express.session({
|
31 | secret: "this_is_super_secret_string",
|
32 | cookie: {
|
33 | maxAge: 86400000
|
34 | }
|
35 | }));
|
36 | app.use(app.router);
|
37 | app.use(express.static(path.join(app_dir, 'static')));
|
38 | return app.use(express.errorHandler({
|
39 | showStack: true,
|
40 | dumpExceptions: true
|
41 | }));
|
42 | });
|
43 | if (conf.redirect_www) {
|
44 | app.get('/*', function(req, res, next) {
|
45 | if (req.headers.host.match(/^www\./)) {
|
46 | return res.redirect("http://" + (req.headers.host.replace(/^www\./, '')) + req.url, 301);
|
47 | } else {
|
48 | return next();
|
49 | }
|
50 | });
|
51 | }
|
52 | app.get('/*', function(req, res, next) {
|
53 | req.conf = app.conf;
|
54 | return next();
|
55 | });
|
56 | app.templates = require('./templates');
|
57 | return require('./posts');
|
58 | }
|
59 | };
|
60 | }).call(this);
|