-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
249 lines (221 loc) · 6.18 KB
/
index.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
var fs = require('fs')
var express = require('express')
var app = express()
var http = require('http')
var server = http.Server(app)
var io = require('socket.io')(server)
var gaikan = require('gaikan')
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser')
var multer = require('multer')
var upload = multer({dest: 'uploads/'})
var PCM = require('./public/js/pcm.js')
var db = require('./src/db.js')
var importer = require('./src/importer.js')
var isMail = require('./public/js/typeDetection.js').isMail
var editSessionManager = require('./src/editSessionManager.js')(db)
// params
var params = {}
for (var i = 0, li = process.argv.length; i < li; i++) {
var param = process.argv[i]
if (param.startsWith('-')) {
var value = i < li - 1
? process.argv[++i]
: null
if (value) {
param = /-{1,2}(.+)/.exec(param)[1]
params[param] = value
}
}
}
// Const
const ENVIRONEMENT = params['env'] || 'dev'
console.log('Environement: ' + ENVIRONEMENT)
const PORT = ENVIRONEMENT === 'prod'
? 9009 // Change to 80 or anything else if needed
: 9009
const COOKIE_MAX_AGE = 1000 * 60 * 60 * 24 * 14 // cookie expire in 2 weeks
// app settings
app.set('views', './views')
app.engine('html', gaikan)
app.set('view engine', '.html')
app.use(express.static(__dirname + '/public'))
app.use(express.static(__dirname + '/node_modules/ace-builds'))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(cookieParser())
// Socket
io.on('connection', function (socket) {
editSessionManager.newUser(socket)
})
// Routing
app.get('/base64.js', function (req, res) {
res.sendFile(__dirname + '/node_modules/js-base64/base64.js')
})
app.get('/', function (req, res) {
res.render('index', {test: 'hello'})
})
app.get('/search', function (req, res) {
db.searchPCM(null, function (err, pcms) {
if (err) {
res.render('error', {error: err})
} else {
res.render('search', {search: '', pcms: pcms})
}
})
})
app.get('/search*', function (req, res) {
var reg = /search\/(.*)/.exec(req.url)
var search = reg
? decodeURIComponent(reg[1]) || null
: null
db.searchPCM(search, function (err, pcms) {
if (err) {
res.render('error', {error: err})
} else {
res.render('search', {search: search, pcms: pcms})
}
})
})
app.get('/api/:id', function (req, res) {
db.getPCM(req.params.id, function (err, pcm) {
res.setHeader('Content-Type', 'application/json')
if (err) {
res.send(JSON.stringify({error: typeof err == 'string' ? err : err.message || 'Unknown error'}))
} else {
res.send(JSON.stringify(pcm))
}
})
})
app.get('/pcm/:id', function (req, res) {
res.render('editor', {id: req.params.id})
})
app.post('/create', function (req, res) {
var err = ''
var pcmData = {
name: req.body.name,
source: req.body.source,
author: req.body.author,
license: req.body.license,
description: req.body.description
}
if (typeof pcmData.name !== 'string' || pcmData.name.length === 0) err += 'name missing'
if (err.length > 0) res.send({error: err})
else {
db.savePCM(new PCM(pcmData), function (err2, res2) {
if (err2) {
res.send({error: err2})
} else {
res.send({pcm: res2.insertedId})
}
})
}
})
app.post('/import', upload.single('file'), function (req, res) {
importer({
file: req.file,
name: req.body.name,
source: req.body.source,
author: req.body.author,
license: req.body.license,
description: req.body.description
}, function (err, pcm) {
fs.unlinkSync(req.file.path)
if (err) {
res.send({error: typeof err == 'string' ? err : err.message || 'Unknown error'})
} else {
db.savePCM(pcm, function (err, res2) {
if (err) {
res.send({error: typeof err == 'string' ? err : err.message || 'Unknown error'})
} else {
res.send({pcm: res2.insertedId})
}
})
}
})
})
app.get('/import*', function (req, res) {
var reg = /import\/(.*)/.exec(req.url)
var src = reg
? decodeURIComponent(reg[1]) || null
: null
importer(src, function (err, pcm, source) {
if (err) {
res.render('error', {error: err})
} else {
if (Array.isArray(pcm)) {
db.savePCM(pcm, function (err, res2) {
if (err) {
res.render('error', {error: err})
} else {
res.redirect('/search/' + source)
}
})
} else {
db.savePCM(pcm, function (err, res2) {
if (err) {
res.render('error', {error: err})
} else {
res.redirect('/pcm/' + res2.insertedId)
}
})
}
}
})
})
app.post('/login', function (req, res) {
var err = ''
var mail = req.body.mail
var password = req.body.password
if (!mail) err += 'mail missing'
if (!password) err += err.length > 0 ? ', password missing' : 'password missing'
if (err.length > 0) res.send({error: err})
else {
db.connectUser(mail, password, function (err, token) {
if (!err) res.cookie('token', token, {maxAge: COOKIE_MAX_AGE})
res.send({error: err})
})
}
})
app.get('/logout', function (req, res) {
if (req.cookies && req.cookies.token) {
res.clearCookie('token')
}
res.send('logout')
})
app.post('/signup', function (req, res) {
var err = ''
var mail = req.body.mail
var pseudo = req.body.pseudo
var password = req.body.password
if (!mail || mail.length === 0 || !isMail(mail)) err += 'invalid mail'
if (!pseudo || pseudo.length < 3 || pseudo.length > 20) err += err.length > 0 ? ', invalid pseudo (3 to 20 characters)' : 'invalid pseudo (3 to 20 characters)'
if (!password || password.length < 5) err += err.length > 0 ? ', invalid password (min 5 characters)' : 'invalid password (min 5 characters)'
if (err.length > 0) res.send({error: err})
else {
db.newUser(mail, pseudo, password, function (err) {
res.send({error: err})
})
}
})
app.get('/user', function (req, res) {
if (req.cookies && req.cookies.token) {
db.getUser(req.cookies.token, function (err, user) {
res.send({error: err, user: user})
})
} else {
res.send({error: 'not connected', user: null})
}
})
app.get('*', function (req, res) {
res.status(404)
res.render('404', {url: req.url})
})
// start server
server.listen(PORT, function () {
console.log('Started OpenCompare2 on port ' + PORT)
}).on('error', function (err) {
console.error(err)
console.error('Check if port ' + PORT + ' is free, and run it as admin.')
process.exit(-1)
})