-
Notifications
You must be signed in to change notification settings - Fork 27
/
app.coffee
162 lines (133 loc) · 5.07 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# # profiling code ...
# profiler = require 'nodetime'
# profiler.profile()
# # profiling code ends here...
###
Module dependencies.
###
util = require 'util'
devnoteutil = require './lib/util'
express = require 'express'
http = require 'http'
socket = require 'socket.io'
routes = require './routes'
wikiApp = require './wikiApp'
userApp = require './userApp'
fileApp = require './fileApp'
adminApp = require './adminApp'
workingPage = require './lib/workingpage'
i18n = require './lib/i18n'
wiki = require './lib/wiki'
i18n.configure
locales: ['en', 'ko']
register: global
noop = ->
process.env.uploadDir = uploadDir = __dirname + '/public/attachment'
WIKINAME = 'note'
ROOT_PATH = '/wikis/' + WIKINAME
API_ROOT_PATH = '/api/' + WIKINAME
app = express()
server = http.createServer app
io = socket.listen server
io.sockets.on 'connection', (socket)->
socket.emit 'connected', socket.id
socket.on 'page name changed', (page)->
result = workingPage.update page, page.user
isExistAlready = false
wiki.getPages (err, pages) ->
throw err if err
if pages.length > 0
for existPage in pages
if existPage.name is page.name
isExistAlready = true
socket.emit 'dupped', page
return;
if result is false
socket.emit 'dupped', workingPage.findByPageName page.name
else
socket.emit 'page name is ok'
socket.on 'disconnect', ->
workingPage.remove socket.id
# Configuration
LISTEN_PORT = 3000
app.set 'views', __dirname + '/views'
app.set 'view engine', 'jade'
oneDay = 60*60*1000*24
app.configure ->
app.use express.bodyParser
uploadDir: uploadDir
app.use express.cookieParser 'n4wiki session'
app.use express.session()
app.use express.methodOverride()
app.use i18n.init
# Session-persisted message middleware
app.use (req, res, next) ->
err = req.session.error
loginMessage = req.session.success
delete req.session.error
#delete req.session.success
res.locals.user = req.session.user
res.locals.loginMessage = loginMessage || ''
res.locals.wikiName = 'note'
if err
res.locals.flashMessage = err
if loginMessage
res.locals.loginMessage = loginMessage
res.locals.wikiName = WIKINAME
res.locals.joinPath = devnoteutil.join
res.locals.sprintf = require('./lib/sprintf').sprintf
next()
app.use app.router
app.use express.logger 'dev'
app.configure 'development', ->
app.use express.static __dirname + '/public'
app.use express.errorHandler
dumpExceptions: true,
showStack: true,
app.configure 'production', ->
app.use express.errorHandler()
app.use express.staticCache()
app.use express.static __dirname + '/public', { maxAge: oneDay }
# Routes
app.get '/', wikiApp.getPages
app.get '/test', routes.test
# Wiki
app.get ROOT_PATH + '/pages', wikiApp.getPages # get page list
app.get ROOT_PATH + '/pages/:name', wikiApp.getPage # get a page
app.get ROOT_PATH + '/new', wikiApp.getNew # get a form to post new wikipage
app.post ROOT_PATH + '/pages', wikiApp.postNew # post new wikipage
app.del ROOT_PATH + '/pages/:name', wikiApp.postDelete # delete wikipage
app.put ROOT_PATH + '/subscribes/:name', wikiApp.postSubscribe # subscribe wikipage
app.del ROOT_PATH + '/subscribes/:name', wikiApp.postUnsubscribe # unsubscribe wikipage
app.post API_ROOT_PATH + '/pages/:name', wikiApp.postRollback # wikipage rollback
# Login & Logout
app.get '/login', userApp.login # get login
app.post '/login', userApp.postLogin # post login
app.post '/logout', userApp.postLogout # post logout
# User
app.get ROOT_PATH + '/users', userApp.getUsers # get user list
app.get ROOT_PATH + '/users/new', userApp.getNew # new user page
app.post ROOT_PATH + '/users/new', userApp.postNew # post new user
app.get ROOT_PATH + '/user/:id', userApp.getId # show user information
app.post ROOT_PATH + '/user/:id', userApp.postId # change user information (password change)
app.post ROOT_PATH + '/dropuser', userApp.postDropuser # drop user
# attachment
app.get ROOT_PATH + '/pages/:name/attachment', fileApp.getAttachment # file attachment page
app.get ROOT_PATH + '/pages/:name/attachment.:format', fileApp.getAttachmentList # file attachment list call by json
app.post ROOT_PATH + '/pages/:name/attachment.:format?', fileApp.postAttachment # file attachment
app.del ROOT_PATH + '/pages/:name/attachment/:filename', fileApp.delAttachment # attachment file delete
# admin
app.get '/admin/mail', adminApp.mail
app.post '/admin/mail', adminApp.postMail
app.get '/admin/mailconf', adminApp.mailconf
app.post '/admin/mailconf', adminApp.postMailconf
exports.start = (port, callback) ->
wikiApp.init WIKINAME
server.listen port
console.log "Express server listening on port %d in %s mode", port, app.settings.env
callback() if callback
exports.stop = -> app.close
exports.start LISTEN_PORT if not module.parent
process.on 'uncaughtException', (err) ->
console.log "uncaughtException occurred! ------>", err.message
console.log err