app.set('view engine', 'hbs');
app.set('views', viewsDir);

// Declare any Express [middleware](http://expressjs.com/api.html#middleware) you'd like to use here
// Log requests, using an appropriate formatter by env
app.use(morgan(devEnv ? 'dev' : 'combined'));
// Include request parsers
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
// Gzip responses when appropriate
app.use(compression());
// Enable the ACE global middleware (populates res.locals with add-on related stuff)
app.use(addon.middleware());
// Enable static resource fingerprinting for far future expires caching in production
app.use(expiry(app, {dir: staticDir, debug: devEnv}));
// Add an hbs helper to fingerprint static resource urls
hbs.registerHelper('furl', function(url){ return app.locals.furl(url); });
// Mount the static resource dir
app.use(express.static(staticDir));

// Show nicer errors when in dev mode
if (devEnv) app.use(errorHandler());

// Wire up your routes using the express and `atlassian-connect-express` objects
routes(app, addon);
// require('ace-abotars')(addon, app);

// Boot the damn thing
http.createServer(app).listen(port, function(){
  console.log()
Example #2
0
import errorHandler from 'errorhandler';
import {normalize}  from 'path';

import React  from 'react';
import App    from './components/App';

const app         = express();
const port        = process.env.PORT || 3000;
const live        = app.get('env') === 'production';
const publicPath  = normalize(__dirname + '/../public');

app.set('view engine', 'ejs');

app.use(compression());
app.use(serveFavicon(publicPath + '/favicon.ico'));
app.use(expiry(app, { dir: publicPath, location: 'postfile' }));
app.use(express.static(publicPath, { index: false }));
app.use(logger(live ? 'combined' : 'dev'));

app.get('/', (req, res, next) => {
  const html = React.renderToString(<App />);
  res.render('layout', { html });
});

if (!live) {
  app.use(errorHandler());
}

app.listen(port, () => {
  console.log(`Running in ${app.get('env')} environment`);
  console.log(`Listening at http://localhost:${port}`);
Example #3
0
  app.configure(function(){
    app.get('/', function (req, res) {
      api_url = (app.settings.env == 'development') ? ('http://localhost:'+ api_port) : ''

      var gist;

      var render = function (err, data) {
        if (err) {
          console.log("Error loading graphgist", err);
          res.send(404,"Error loading graphgist: "+ err)
        } else {
          res.render(__dirname + '/dist/index.html.jade', {api_port: api_port,
                                                           api_url: api_url,
                                                           gist: data,
                                                           environment: process.env.NODE_ENV,
                                                           rollbar_token: process.env.ROLLBAR_CLIENT_TOKEN,
                                                           git_head_sha: process.env.GIT_HEAD_SHA});
        }
      }

      if (typeof(req.query._escaped_fragment_) === 'string' &&
          (match = req.query._escaped_fragment_.toString().match(/^\/gists\/([^\/]+)/))) {

          if (match[1] == 'all') {
            load_gist.get_all_gists(function (err, data) {
              res.render(__dirname + '/dist/assets/partials/gist-all-crawl.html.jade', {gists: data.results});
            });
          } else if (match[1] == 'about') {
            res.render(__dirname + '/dist/about-escaped-fragment.html.jade');
          } else {
            var gist;
            var id = match[1];
            var cache = req.query.skip_cache ? {} : app.locals.load_cache;

            load_gist.get_gist(id, function(data) {
              gist = data;

              load_gist.load_gist(id, cache, {}, function(err, data, from_db) {
                if (err) {
                    console.log("Error loading graphgist", id, err);
                    res.send(404,"Error loading graphgist from: "+ id +" "+ err)
                } else {
                    console.log("finding gist with id: " + id);
                    var item = load_gist.findGist(app.locals, id);
                    res.set('Content-Type', 'text/html');

                    var options = opal.hash2([], {})
                    if (!from_db) {
                      var options = opal.hash2(
                          ['attributes'],
                          {attributes: ['showtitle']});
                    }

                    if (gist === null) gist = {}

                    gist.html = asciidoctor_processor.$convert(load_gist.preProcessHTML(data), options)
                    res.render(__dirname + '/dist/gist-escaped-fragment.html.jade', {gist: gist});
                }
              });
            });
          }
      } else {
        render(null, {});
      }

    });

    app.get('/error', function (req, res) {
      throw 'unhandled!'
    });

    short_urls = {
      '/submit': '/#!/gists/submit',
      '/about': '/#!/gists/about',
      '/challenge': '/#!/gists/challenge'
    }

    app_redirect = function(app, source, target) {
      app.get(source, function (req, res) {
        res.redirect(301, target);
      });
    }

    for ( var source in short_urls ) {
      var target = short_urls[source];
      app_redirect(app, source, short_urls[source]);
    }

    app.get('/templates/:template', function (req, res) {
      if (!req.params.template.match(/[a-z0-9\-_]/i)) throw 'Invalid template'

      res.render(__dirname + '/dist/assets/partials/'+ req.params.template +'.html.jade', {});
    });

    app.get('/gists/:id.:format?',function (req, res) {
        var id = req.params.id;

        if ( req.params.format !== 'html' ) {
          id = id + '.' + req.params.format;
          req.params.format = null;
        }

        var cache = req.query.skip_cache ? {} : app.locals.load_cache;

        var http_headers = {};
        if (req.headers['authorization']) http_headers['Authorization'] = req.headers['authorization']
        load_gist.load_gist(id, cache, {http_headers: http_headers}, function(err, data, from_db) {
            if (err) {
                if (err.statusCode === 401) {
                  res.statusCode = 401;
                  res.setHeader('WWW-Authenticate', 'Basic realm="GraphGist Portal"');
                  res.send('');
                } else {
                  console.log("Error loading graphgist", id, err);
                  res.send(404,"Error loading graphgist from: "+ id +" "+ err)
                }
            } else {
                var item = load_gist.findGist(app.locals, id);

                res.set('Content-Type', 'text/plain');
                if (item) {
                    function setHeader(key,prop) {
                        if (item[prop]) res.set("GraphGist-" + key,item[prop]);
                    }
                    setHeader("Title","title");
                    setHeader("Author","name");
                    setHeader("Twitter","twitter");
                    setHeader("Description","introText");
                    setHeader("Image","img");
                    setHeader("Category","Category");
                    res.set("Url","http://graphgist.neo4j.com/#!/gists/"+ id);
                }

                if (req.params.format === 'html') {
                  var options = opal.hash2([], {})
                  if (!from_db) {
                    var options = opal.hash2(
                        ['attributes'],
                        {attributes: ['showtitle']});
                  }
                                    
                  data = asciidoctor_processor.$convert(load_gist.preProcessHTML(data), options)
                }

                res.send(200,data);
            }
        });
    });

    var expiry = require('static-expiry');
    var path = require('path');
    app.use(expiry(app, { dir: path.join(__dirname, '/dist/assets') }));
    app.use(express.static(__dirname + '/dist/assets'));

    app.use(express.static(__dirname + '/dist'));

  });