import logger from 'debug';
import Koa from 'koa';
import bodyParser from 'koa-better-body';
import compress from 'koa-compress';
import favicon from 'koa-favicon';
import mount from 'koa-mount';
import serve from 'koa-static';
import convert from 'koa-convert';
import config from './config';
import catcher from './middleware/catcher';
import render from './middleware/render';
import markdown from './routes/markdown';
import hooks from './routes/hooks';

const app = new Koa();

// Middleware
/*app.use(serverpush({
    manifest: path.join(__dirname, '../../push_manifest.json'),
    singleheader: false
}))*/
app.use(favicon(config.http.favicon));
app.use(compress());
app.use(convert(bodyParser({
  formLimit: '200kb',
  jsonLimit: '200kb',
  bufferLimit: '4mb'
})));

app.use(catcher);
app.use(markdown.routes());
app.use(hooks.routes());

// Serve static files
for (const [k, v] of Object.entries(config.static)) {
  logger('inferno:static')(v);
  app.use(mount(k, serve(v, { index: false })));
}

// Serve service worker
// app.use(serve(require('path').join(__dirname, '../assets/service')))
app.use(serve(require('path').join(__dirname, '../../public/assets/service')));

// Render inferno app
app.use(render);

//if (process.env.DEV) {
// Without HTTP2 support
app.listen(config.http.port, function() {
  logger('inferno:start')('Listening on port ' + config.http.port);
});
// } else {
//     // With HTTP2 support
//     const server = http2.createServer({
//         key: fs.readFileSync(config.server.certificate_key),
//         cert: fs.readFileSync(config.server.certificate)
//     }, app.callback())
//
//     server.listen(config.http.port, (error) => {
//         if (error) {
//             logger('inferno:error')(error)
//             process.exit(1)
//         } else {
//             logger('inferno:start')('Running with HTTP/2 enabled')
//             logger('inferno:start')('Listening on port ' + config.http.port)
//         }
//     })
// }

@@ -491,17 +491,18 @@
 leware%0A/
-*
+/ 
 app.use(
@@ -514,17 +514,18 @@
 rpush(%7B%0A
- 
+//
    manif
@@ -579,17 +579,18 @@
 json'),%0A
- 
+//
    singl
@@ -608,13 +608,15 @@
 lse%0A
+// 
 %7D))
-*/
+;
 %0Aapp
@@ -1223,16 +1223,17 @@
 er);%0A%0A//
+ 
 if (proc
@@ -1249,16 +1249,21 @@
 EV) %7B%0A//
+   //
  Without
@@ -1277,16 +1277,21 @@
 support%0A
+//   
 app.list
@@ -1320,24 +1320,29 @@
 unction() %7B%0A
+//   
   logger('in
@@ -1397,16 +1397,21 @@
 .port);%0A
+//   
 %7D);%0A// %7D
@@ -1420,18 +1420,16 @@
 lse %7B%0A//
-  
    // Wi
@@ -1450,18 +1450,16 @@
 rt%0A//   
- 
- 
 const se
@@ -1484,26 +1484,24 @@
 eServer(%7B%0A//
-  
        key: 
@@ -1557,18 +1557,16 @@
 /       
-  
 cert: fs
@@ -1611,18 +1611,16 @@
 e)%0A//   
- 
- 
 %7D, app.c
@@ -1632,21 +1632,16 @@
 ck())%0A//
-%0A//  
    serve
@@ -1689,18 +1689,16 @@
 /       
-  
 if (erro
@@ -1700,26 +1700,24 @@
 (error) %7B%0A//
-  
            l
@@ -1744,26 +1744,24 @@
 ')(error)%0A//
-  
            p
@@ -1784,18 +1784,16 @@
 /       
-  
 %7D else %7B
@@ -1791,26 +1791,24 @@
  %7D else %7B%0A//
-  
            l
@@ -1866,34 +1866,32 @@
 ')%0A//           
- 
- 
 logger('inferno:
@@ -1951,24 +1951,345 @@
     
+ %7D%0A//
    %7D
+)
 %0A// 
-    %7D)%0A// %7D
+%7D%0A%0A/**%0A * only listen in dev env %0A * could also check with process.env.DEV%0A */%0Aif (process.env.IS_NOW === undefined) %7B%0A  console.log('NOT on NOW')%0A  app.listen(config.http.port, function() %7B%0A    logger('inferno:start')('Listening on port ' + config.http.port);%0A  %7D);%0A%7D else %7B%0A  console.log('IS on NOW')%0A%7D%0A%0Amodule.exports = app
 %0A

format server/server and support serverless