This repository has been archived by the owner on May 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.coffee
99 lines (72 loc) · 2.04 KB
/
server.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require("coffee-script").register()
GLOBAL._ = require("lodash")
# npm modules
express = require("express")
async = require("async")
fs = require("fs")
path = require("path")
moment = require("moment")
glob = require("glob")
connect = require("connect")
# local modules
config = require("./config")
Renderer = require("./renderer")
Page = require("./page")
Post = require("./post")
Site = require("./site")
# merge user config with default config
if fs.existsSync("#{process.cwd()}/config.coffee") or fs.existsSync("#{process.cwd()}/config.js")
config = _.merge(config, require("#{process.cwd()}/config"))
# Setup Express
app = express()
# Hooks for plugins
app.imports = _.templateSettings.imports
app.config = config
# Logging
app.use(connect.logger("tiny"))
# Serving posts
app.use((req, res, next) ->
Site.read(req.path, (err, site) ->
if err
res.send(500, err.toString())
else
Post.match(req.path, site, (err, post) ->
if err
res.send(500, err.toString())
else if not post?
next()
else
Renderer.renderPost(post, site, (err, html) -> res.send(html))
)
)
)
# Serving pages
app.use((req, res, next) ->
Site.read(req.path, (err, site) ->
if err
res.send(500, err.toString())
else
Page.match(req.path, site, (err, page) ->
if err
res.send(500, err.toString())
else if not page?
next()
else
Renderer.renderPage(page, site, (err, html) -> res.send(html))
)
)
)
# Include bundled plugins
if fs.existsSync("#{__dirname}/plugins")
fs.readdirSync("#{__dirname}/plugins").forEach( (path) ->
require("#{__dirname}/plugins/#{path}")(app)
)
# Include user plugins
if fs.existsSync("#{config.path.root}/#{config.path.plugins}")
fs.readdirSync("#{config.path.root}/#{config.path.plugins}").forEach( (path) ->
require("#{config.path.root}/#{config.path.plugins}/#{path}")(app)
)
# Serve all the other files
app.use(express.static(config.path.root))
# Here we go
app.listen(config.port)