'use strict';

const fs = require('fs');
const http = require('http');
const path = require('path');

const PORT = process.env.PORT || 8080;

const staticAssets = {
  '/bootstrap.min.css': {
    contentType: 'text/css; charset=utf-8',
    data: fs.readFileSync(path.join(__dirname, 'bootstrap.min.css'))
  },
  '/bootstrap.bundle.min.js': {
    contentType: 'application/javascript',
    data: fs.readFileSync(path.join(__dirname, 'bootstrap.bundle.min.js'))
  },
  '/': {
    contentType: 'text/html; charset=utf-8',
    data: Buffer.from('<html><head><link rel="stylesheet" href="/bootstrap.min.css"><script src="/bootstrap.bundle.min.js"></script></head><body><h1>Test</h1></body></html>')
  }
};

const server = http.createServer((req, res) => {
  const { method, url } = req;
  console.log(`${method} ${url}`);

  if (method === 'GET' && url in staticAssets) {
    const { contentType, data } = staticAssets[url];
    res.writeHead(200, 'OK', {
      'Content-Type': contentType,
      'Content-Length': data.length
    });
    res.end(data);
  } else {
    res.writeHead(404, 'Not Found');
    res.end('Not Found');
  }
});

server.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
});
