|
| 1 | +// Copyright 2015-2016, Google, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +// Activate Google Cloud Trace and Debug when in production |
| 17 | +if (process.env.NODE_ENV === 'production') { |
| 18 | + require('@google/cloud-trace').start(); |
| 19 | + require('@google/cloud-debug'); |
| 20 | +} |
| 21 | + |
| 22 | +var path = require('path'); |
| 23 | +var express = require('express'); |
| 24 | +var session = require('express-session'); |
| 25 | +var MemcachedStore = require('connect-memcached')(session); |
| 26 | +var passport = require('passport'); |
| 27 | +var config = require('./config'); |
| 28 | +var logging = require('./lib/logging'); |
| 29 | + |
| 30 | +var app = express(); |
| 31 | + |
| 32 | +app.disable('etag'); |
| 33 | +app.set('views', path.join(__dirname, 'views')); |
| 34 | +app.set('view engine', 'jade'); |
| 35 | +app.set('trust proxy', true); |
| 36 | + |
| 37 | +// Add the request logger before anything else so that it can |
| 38 | +// accurately log requests. |
| 39 | +app.use(logging.requestLogger); |
| 40 | + |
| 41 | +// Configure the session and session storage. |
| 42 | +var sessionConfig = { |
| 43 | + resave: false, |
| 44 | + saveUninitialized: false, |
| 45 | + secret: config.get('SECRET'), |
| 46 | + signed: true |
| 47 | +}; |
| 48 | + |
| 49 | +// In production use the App Engine Memcache instance to store session data, |
| 50 | +// otherwise fallback to the default MemoryStore in development. |
| 51 | +if (config.get('NODE_ENV') === 'production') { |
| 52 | + sessionConfig.store = new MemcachedStore({ |
| 53 | + hosts: [config.get('MEMCACHE_URL')] |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +app.use(session(sessionConfig)); |
| 58 | + |
| 59 | +// OAuth2 |
| 60 | +app.use(passport.initialize()); |
| 61 | +app.use(passport.session()); |
| 62 | +app.use(require('./lib/oauth2').router); |
| 63 | + |
| 64 | +// Books |
| 65 | +app.use('/books', require('./books/crud')); |
| 66 | +app.use('/api/books', require('./books/api')); |
| 67 | + |
| 68 | +// Redirect root to /books |
| 69 | +app.get('/', function (req, res) { |
| 70 | + res.redirect('/books'); |
| 71 | +}); |
| 72 | + |
| 73 | +// Our application will need to respond to health checks when running on |
| 74 | +// Compute Engine with Managed Instance Groups. |
| 75 | +app.get('/_ah/health', function (req, res) { |
| 76 | + res.status(200).send('ok'); |
| 77 | +}); |
| 78 | + |
| 79 | +// Add the error logger after all middleware and routes so that |
| 80 | +// it can log errors from the whole application. Any custom error |
| 81 | +// handlers should go after this. |
| 82 | +app.use(logging.errorLogger); |
| 83 | + |
| 84 | +// Basic 404 handler |
| 85 | +app.use(function (req, res) { |
| 86 | + res.status(404).send('Not Found'); |
| 87 | +}); |
| 88 | + |
| 89 | +// Basic error handler |
| 90 | +app.use(function (err, req, res, next) { |
| 91 | + /* jshint unused:false */ |
| 92 | + // If our routes specified a specific response, then send that. Otherwise, |
| 93 | + // send a generic message so as not to leak anything. |
| 94 | + res.status(500).send(err.response || 'Something broke!'); |
| 95 | +}); |
| 96 | + |
| 97 | +if (module === require.main) { |
| 98 | + // Start the server |
| 99 | + var server = app.listen(config.get('PORT'), function () { |
| 100 | + var port = server.address().port; |
| 101 | + console.log('App listening on port %s', port); |
| 102 | + }); |
| 103 | +} |
| 104 | + |
| 105 | +module.exports = app; |
0 commit comments