forked from streetmix/streetmix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
94 lines (75 loc) · 3.12 KB
/
app.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
var compression = require('compression')
var cookieParser = require('cookie-parser')
var cookieSession = require('cookie-session')
var express = require('express')
var assets = require('connect-assets')
var bodyParser = require('body-parser')
var config = require('config')
var path = require('path')
var controllers = require('./app/controllers')
var resources = require('./app/resources')
var requestHandlers = require('./lib/request_handlers')
var middleware = require('./lib/middleware')
var logger = require('./lib/logger')()
var exec = require('child_process').exec
if (process.env.NEW_RELIC_LICENSE_KEY) {
require('newrelic')
}
var app = module.exports = express()
app.locals.config = config
app.use(bodyParser.json())
app.use(compression())
app.use(cookieParser())
app.use(cookieSession({ secret: config.cookie_session_secret }))
app.use(requestHandlers.login_token_parser)
app.use(requestHandlers.request_log)
app.use(requestHandlers.request_id_echo)
app.set('view engine', 'jade')
app.set('views', __dirname + '/app/views')
// Redirect to environment-appropriate domain, if necessary
app.all('*', function (req, res, next) {
if (config.header_host_port !== req.headers.host && app.locals.config.env !== 'development') {
var redirectUrl = 'http://' + config.header_host_port + req.url
console.log('req.hostname = %s but config.header_host_port = %s; redirecting to %s...', req.hostname, config.header_host_port, redirectUrl)
res.redirect(301, redirectUrl)
} else {
next('route')
}
})
app.get('/twitter-sign-in', controllers.twitter_sign_in.get)
app.get(config.twitter.oauth_callback_uri, controllers.twitter_sign_in_callback.get)
app.post('/api/v1/users', resources.v1.users.post)
app.get('/api/v1/users/:user_id', resources.v1.users.get)
app.put('/api/v1/users/:user_id', resources.v1.users.put)
app.delete('/api/v1/users/:user_id/login-token', resources.v1.users.delete)
app.get('/api/v1/users/:user_id/streets', resources.v1.users_streets.get)
app.post('/api/v1/streets', resources.v1.streets.post)
app.get('/api/v1/streets', resources.v1.streets.find)
app.head('/api/v1/streets', resources.v1.streets.find)
app.delete('/api/v1/streets/:street_id', resources.v1.streets.delete)
app.head('/api/v1/streets/:street_id', resources.v1.streets.get)
app.get('/api/v1/streets/:street_id', resources.v1.streets.get)
app.put('/api/v1/streets/:street_id', resources.v1.streets.put)
app.post('/api/v1/feedback', resources.v1.feedback.post)
app.get('/api/v1/translate/:locale_code', resources.v1.translate.get)
app.get('/.well-known/status', resources.well_known_status.get)
// Process stylesheets via Sass and PostCSS / Autoprefixer
app.use('/assets/css/styles.css', middleware.styles)
app.use(assets({
paths: ['assets/js'],
precompile: ['app.js']
}))
app.use(express.static(path.join(__dirname, '/public')))
// Catch-all
app.use(function (req, res) {
res.render('main', {})
})
// Provide a message after a Ctrl-C
// Note: various sources tell us that this does not work on Windows
process.on('SIGINT', function () {
if (app.locals.config.env === 'development') {
console.log('Stopping Streetmix!')
exec('npm stop')
}
process.exit()
})