{"size": 2155, "ext": "js", "max_stars_repo_path": "server.js", "max_stars_repo_name": "dotkim/cleanup-dgi", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server.js", "max_issues_repo_name": "dotkim/cleanup-dgi", "max_issues_repo_issues_event_min_datetime": "2019-04-02T20:33:15.000Z", "max_issues_repo_issues_event_max_datetime": "2021-06-29T07:45:25.000Z", "max_forks_repo_path": "server.js", "max_forks_repo_name": "Digitale-Gardermoen/cleanup", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 6, "max_forks_count": null, "avg_line_length": 44.8958333333, "max_line_length": 120, "alphanum_fraction": 0.679350348}
require('dotenv').config();
const express = require('express');
const basicAuth = require('express-basic-auth');
const https = require('https');
const fs = require('fs');
const dateString = require('./components/dateString.js');
const getUnauthorizedResponse = require('./components/getUnauthorizedResponse.js');
const jsonParser = require('body-parser').json({ type: 'application/json' });

console.log('############### WEB SERVER START UP ###############');
console.log(dateString(), '- starting https server')

const app = new express();
app.disable('x-powered-by');

// get config data from env.
const serverCert = process.env.HTTPS_CERT;        // gets the path for the certificate
const serverKey = process.env.HTTPS_KEY;          // gets the path for the key
const httpsPort = process.env.HTTPS_PORT || 443;  // check if the port is set in .env, if not just use default.
const authUser = process.env.AUTH_USER;
const authPass = process.env.AUTH_PASS;

const options = {
  key: fs.readFileSync(serverKey),
  cert: fs.readFileSync(serverCert)
};

let basicAuthOptions = {
  users: {},                                      // we create the user later, so we can keep the user in .env
  unauthorizedResponse: getUnauthorizedResponse,  // generic 401 error when username/pw is wrong
  challenge: true                                 // challenge the user to authenticate
};

basicAuthOptions.users[authUser] = authPass;      // insert the authorization user into the users object.

app.use(jsonParser);                              // use a jsonparser bodyparser, this way we dont have to do JSON.parse
app.use(basicAuth(basicAuthOptions));             // setup basic auth

// require routes for the API paths
app.use('/insert', require('./routes/insert.js'));
app.use('/fetch', require('./routes/fetch.js'));
app.use('/deleteone', require('./routes/deleteOne.js'));
app.use('/eadmin', require('./routes/eadmin.js'));
app.use('/addserver', require('./routes/addServer.js'));

// finally setup the https server with the current configuration
https.createServer(options, app).listen(httpsPort);
console.log(dateString(), '- listening on port', httpsPort);