let Path = require('path');
let Hapi = require('hapi');
var Good = require('good');
var blogPosts = require('./blogPostManifest.js');
var fs = require('fs');
var _ = require('underscore');

blogPosts.forEach(blogPost =>
  blogPost.body = fs.readFileSync(`${__dirname}/public/markdown/blog/${blogPost.id}.markdown`, 'utf-8'));

let blogPostsByYear = _(blogPosts).groupBy(blogPost =>
  blogPost.created.getFullYear());

let server = new Hapi.Server({
  connections: {
    routes: {
      files: {
        relativeTo: Path.join(__dirname, 'public')
      }
    }
  }
});
server.connection({ port: 3000 });

server.route({
  method: 'GET',
  path: '/',
  handler: function (request, reply) {
    reply.file('index.html');
  }
});

server.route({
  method: 'GET',
  path: '/{name}',
  handler: function (request, reply) {
    reply.file('index.html');
  }
});

server.route({
  method: 'GET',
  path: '/other/{name}',
  handler: function (request, reply) {
    reply.file('index.html');
  }
});

server.route({
  method: 'GET',
  path: '/blog/{name}',
  handler: function (request, reply) {
    reply.file('index.html');
  }
});

server.route({
  method: 'GET',
  path: '/blog/posted/{year}',
  handler: function (request, reply) {
    reply.file('index.html');
  }
});

server.route({
  method: 'GET',
  path: '/api/blog/posted/{year}',
  handler: function (request, reply) {
    reply(blogPostsByYear[request.params.year] || []).code(200);
  }
});

server.route({
  method: 'GET',
  path: '/api/blog/{id}',
  handler: function (request, reply) {
    let blogPost = _(blogPosts).find(post =>
      post.id === Number(request.params.id));
    reply(blogPost).code(200);
  }
});

server.route({
  method: 'GET',
  path: '/api/blog',
  handler: function (request, reply) {
    reply(blogPosts).code(200);
  }
});

// Serve everythign else from the public folder
server.route({
  method: 'GET',
  path: '/{path*}',
  handler: {
    directory: {
      path: './'
    }
  }
});

server.register({
  register: Good,
  options: {
    reporters: [{
      reporter: require('good-console'),
      events: {
        response: '*',
        log: '*'
      }
    }]
  }
}, function (err) {
  if (err) {
    throw err; // something bad happened loading the plugin
  }

  server.start(function () {
    server.log('info', 'Server running at: ' + server.info.uri);
  });
});

@@ -85,26 +85,153 @@
 var 
-blogPosts = requir
+fs = require('fs');%0Avar _ = require('underscore');%0Avar blogPosts, blogPostsByYear;%0A%0Afunction reloadData() %7B%0A  delete require.cache%5Brequire.resolv
 e('.
@@ -252,23 +252,29 @@
 est.js')
+%5D
 ;%0A
-var f
+  blogPost
 s = requ
@@ -282,46 +282,35 @@
 re('
-fs');%0Avar _ = require('underscore
+./blogPostManifest.js
 ');%0A
-%0A
+  
 blog
@@ -327,32 +327,34 @@
 ach(blogPost =%3E%0A
+  
   blogPost.body 
@@ -443,20 +443,17 @@
 f-8'));%0A
-%0Alet
+ 
  blogPos
@@ -496,16 +496,18 @@
 Post =%3E%0A
+  
   blogPo
@@ -532,16 +532,33 @@
 Year());
+%0A%7D%0A%0AreloadData();
 %0A%0Alet se
@@ -1116,32 +1116,175 @@
 tml');%0A  %7D%0A%7D);%0A%0A
+server.route(%7B%0A  method: 'GET',%0A  path: '/refresh',%0A  handler: function (request, reply) %7B%0A    reloadData();%0A    reply.redirect('/');%0A  %7D%0A%7D);%0A%0A
 server.route(%7B%0A 

Add way to reload data on release deploy