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