Home Reference Source Repository

src/controllers/IndexController.js

/* eslint-disable no-bitwise */
// Import the neccesary modules.
import Beer from '../models/beer';
import { executeCommand } from '../utils';
import { server } from '../config/constants';
import {
  repository,
  version
} from '../../package.json';

/** Class for displaying information about the server the API is running on. */
export default class IndexController {

  /**
   * Get general information about the server.
   * @param {Request} req - The express request object.
   * @param {Response} res - The express response object.
   * @returns {JSON} - General information about the server.
   */
  getIndex(req, res) {
    return executeCommand('git rev-parse --short HEAD').then(commit => {
      return Beer.count().exec(totalBeers => {
        return res.json({
          repo: repository.url,
          server,
          totalBeers: totalBeers | 0,
          uptime: process.uptime() | 0,
          version,
          commit
        });
      });
    }).catch(err => res.status(500).json(err));
  }

}