const liveServer = require('live-server');
const path = require('path');
const fs = require('fs');
const spdy = require('spdy');

const options = {
  port: 5000,
  root: '../bizmatch-server/pictures',
  open: false,
  https: {
    cert: fs.readFileSync('certs/cert.pem'),
    key: fs.readFileSync('certs/key.pem'),
  },
  middleware: []
};

spdy.createServer(options.https, (req, res) => {
    liveServer.middleware(options.middleware)(req, res, () => {
      const filePath = path.join(options.root, req.url);
      fs.readFile(filePath, (err, data) => {
        if (err) {
          res.writeHead(404);
          res.end();
        } else {
          res.writeHead(200);
          res.end(data);
        }
      });
    });
  }).listen(options.port, (err) => {
    if (err) {
      console.error(err);
    } else {
      console.log(`Live server is running on https://localhost:${options.port}`);
    }
  });