This repository has been archived by the owner on Oct 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.coffee
196 lines (129 loc) · 4.7 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
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
express = require 'express'
# Basic Express stuff
path = require 'path'
favicon = require 'serve-favicon'
logger = require 'morgan'
cookieParser = require 'cookie-parser'
bodyParser = require 'body-parser'
session = require 'express-session'
#Database, user login stuff
mongoose = require 'mongoose'
urls = require './config/urls.coffee'
#Connect to the DB
mongoose.connect urls.mongoDB
#Define our models
require './models/host.coffee'
require './models/user.coffee'
require './models/volunteer.coffee'
require './models/volunteeringOpportunity.coffee'
require './models/settings.coffee'
require './models/message.coffee'
User = mongoose.model 'User'
#Mailin server
#require './mailin/server.coffee'
#Cron-job for auto renewal messages
require './routes/renewal.coffee'
app = express()
#Configuring Passport stuff for login
passport = require('passport')
LocalStrategy = require('passport-local').Strategy
app.all '*', (req, res, next) ->
res.header 'Access-Control-Allow-Origin', '*'
res.header 'Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS'
res.header 'Access-Control-Allow-Headers', 'Content-Type'
next()
#view engine setup
#for ejs which allows us to use ejs files as templates
app.set 'views', path.join(__dirname, 'views')
app.set 'view engine', 'ejs'
#uncomment after placing your favicon in /static
#app.use favicon __dirname + '/static/favicon.ico'
#PASSPORT SECURITY THINGS
passport.serializeUser (user, done)->
done null, user
passport.deserializeUser (user, done)->
done null, user
passport.use new LocalStrategy (username, password, done)->
console.log 'Checking stuff'
process.nextTick ->
User.findOne({ username: username }, (err, staff)->
if err
return done(err)
if !staff || !(staff.validPassword password, staff.hash)
console.log "Incorrect user details"
return done null, false, { message: 'Incorrect username/password.' }
return done null, staff
)
app.use session
secret: urls.secret,
resave: true,
saveUninitialized: true
app.use logger('dev')
app.use bodyParser.json()
app.use bodyParser.urlencoded({ extended: false })
app.use cookieParser()
app.use express.static('static')
app.use passport.initialize()
app.use passport.session()
#Login - no authentication to get here
app.use '/',require('./routes/login.coffee')
#Allows Volunteer signup form to access volunteeringOpportunity data without auth
app.use '/api/volunteeringOpportunityUnauth',require('./routes/api/volunteeringOpportunityUnauth.coffee')
#Allows Volunteer signup form to create new volunteers without auth
app.use '/api/volunteerUnauth',require('./routes/api/volunteerUnauth.coffee')
#app.use '/jsonpdfgen', require('./routes/pdf/unauthquotepdf.coffee') #another un-auth route
app.use '/logout', require('./routes/logout.coffee')
app.post '/loginauth', passport.authenticate('local',
{
failureRedirect: '/login',
passReqToCallback : true
}),(req, res)->
console.log 'Passed login Middleware'
res.redirect '/'
app.use '/webhook', require('./routes/webhook.coffee')
app.all '*', (req,res,next)->
#Here we sort out the method if it's been botched on the client side via the inclusion of a _method key in the body:
#Deals with PATCH methods
if req.body&&req.body._method
req.method = req.body._method.toUpperCase()
delete req.body._method
else if req.params&&req.params._method
req.method = req.params._method.toUpperCase()
delete req.params._method
#Here we do our authentication
if req.isAuthenticated() ####### DON'T COMMIT
return next()
res.redirect '/login'
Volunteer = mongoose.model 'Volunteer'
app.use '/', require('./routes/index.coffee') #routing file for HTML
#MAIN ROUTES for API
app.use '/api/user', require('./routes/api/user.coffee')
app.use '/api/schemas', require('./routes/api/schemas.coffee')
app.use '/api/volunteer', require('./routes/api/volunteer.coffee')
app.use '/api/volunteeringOpportunity', require('./routes/api/volunteeringOpportunity.coffee')
app.use '/api/message', require('./routes/api/message.coffee')
app.use '/gridfs', require('./routes/gridfs.coffee')
app.use '/email', require('./routes/communicationRoutes.coffee')
app.use '/api/backup', require('./routes/api/backup.coffee')
# catch 404 and forward to error handler
app.use (req, res, next) ->
err = new Error 'Not Found'
err.status = 404
next err
#error handlers
# development error handler
#will print stacktrace
if app.get('env') == 'development'
app.use (err, req, res, next) ->
res.status err.status || 500
message: err.message,
error: err
# production error handler
# no stacktraces leaked to user
#app.use (err, req, res, next) ->
#res.redirect '/404.htm'
#res.status err.status || 500
#res.render 'error',
# message: err.message,
# error: {}
module.exports = app