This repository has been archived by the owner on Apr 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.coffee
73 lines (54 loc) · 1.63 KB
/
app.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
{renderer} = require 'cirru-html'
express = require 'express'
fs = require 'fs'
path = require 'path'
mime = require 'mime'
marked = require 'marked'
marked.setOptions
gfm: yes
breaks: yes
NodeCache = require 'node-cache'
pages = new NodeCache stdTTL: 100, checkperiod: 120
env = process.env.NODE_ENV or 'develop'
app = express()
indexFile = 'cirru/index.cirru'
indexCode = fs.readFileSync indexFile, 'utf8'
indexPage = renderer indexCode, '@filename': indexFile
app.get '/', (req, res) ->
res.send indexPage()
postFile = 'cirru/post.cirru'
postCode = fs.readFileSync postFile, 'utf8'
postPage = renderer postCode, '@filename': postFile
app.get '/posts/:year/:month/:name', (req, res) ->
p = req.params
name = decodeURIComponent p.name
file = path.join 'posts', p.year, p.month, name
res.writeHeader 200, 'content-type': 'text/html'
pages.get file, (err, item) ->
if item[file]?
res.end item[file]
return
fs.readFile file, 'utf8', (err, content) ->
if err?
res.end '<i>No such page, thanks.</i>'
return
html = postPage
title: name
content: marked content
res.end html
return if env is 'develop'
pages.set file, html
app.get '/css/:file', (req, res) ->
file = path.join 'css', req.params.file
pages.get file, (err, item) ->
if item[file]?
res.writeHeader 200, 'content-type': 'text/css'
res.end item[file]
return
res.sendfile file
fs.readFile file, 'utf8', (err, content) ->
return if env is 'develop'
pages.set file, content
port = process.env.PORT or 3000
app.listen port
console.log 'Running blog at:', port