{"size": 1194, "ext": "js", "max_stars_repo_path": "src/server/index.js", "max_stars_repo_name": "rolspace/react-d3-webapp", "max_stars_repo_stars_event_min_datetime": "2019-04-23T02:33:23.000Z", "max_stars_repo_stars_event_max_datetime": "2019-04-23T02:33:23.000Z", "max_issues_repo_path": "src/server/index.js", "max_issues_repo_name": "crazy-me-dev/react-d3-connectin", "max_issues_repo_issues_event_min_datetime": "2021-01-24T12:39:35.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-20T15:39:57.000Z", "max_forks_repo_path": "src/server/index.js", "max_forks_repo_name": "crazy-me-dev/react-d3-connectin", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 12, "max_forks_count": null, "avg_line_length": 25.4042553191, "max_line_length": 84, "alphanum_fraction": 0.6775544389}
const bodyParser = require('body-parser')
const cors = require('cors')
const express = require('express')
const path = require('path')
const constants = require('./common/constants')
const logger = require('./common/logger')
const queries = require('./common/queries')
const allIncoming = require('./middleware/allIncoming')
const catchErrors = require('./middleware/catchErrors')
const repo = require('./routes/repo')
const token = require('./routes/token')

const app = express()
const ns = path.relative(process.cwd(), __filename)
const { status } = constants

const init = () => {
	queries.loadQueries()

	app.use(bodyParser.json())
	app.use(cors(constants.cors))
	app.use(allIncoming)

	app.options('/api/token')
	app.post('/api/token', token.post)
	app.post('/api/repo/:owner/:name', repo.post)

	app.use((req, res) => {
		logger.warn({ ns: `${ns}:init` }, 'Resource not found')
		res.status(status.notFound).send({ 'message': 'Resource not found' })
	})

	app.use(catchErrors)

	const port = process.env.PORT || 9000

	app.set('port', port)

	app.listen(port, () => {
		logger.info({ ns: `${ns}:init` }, `Server started and listening on port: ${port}`)
	})
}

module.exports = {
	init
}
