2023-02-09 20:34:53 +00:00
|
|
|
// OwlBoard - © Fred Boniface 2022 - Licensed under GPLv3 (or later)
|
|
|
|
|
|
|
|
// Please see the included LICENSE file. Statically served fonts are
|
|
|
|
// licensed separately, each folder contains a license file where a
|
|
|
|
// different license applies.
|
|
|
|
|
2023-05-06 21:54:49 +01:00
|
|
|
console.log('Initialising OwlBoard');
|
|
|
|
const mode = process.env.NODE_ENV || 'development';
|
2023-02-16 10:39:19 +00:00
|
|
|
|
2023-02-09 20:34:53 +00:00
|
|
|
// External Requires
|
|
|
|
const express = require('express');
|
|
|
|
const app = express();
|
2023-05-06 21:54:49 +01:00
|
|
|
|
|
|
|
// Middleware
|
|
|
|
const compression = require('compression');
|
|
|
|
const rateLimit = require('express-rate-limit');
|
|
|
|
const authenticate= require('./src/middlewares/auth.middlewares');
|
2023-06-13 13:42:38 +01:00
|
|
|
const cors = require('cors');
|
2023-02-09 20:34:53 +00:00
|
|
|
|
|
|
|
// Internal Requires
|
2023-06-07 11:45:48 +01:00
|
|
|
const log = require('./src/utils/log.utils');
|
|
|
|
const version = require('./src/configs/version.configs');
|
|
|
|
const listRtr = require('./src/routes/list.routes');
|
|
|
|
const ldbRtr = require('./src/routes/ldb.routes');
|
2023-06-11 19:42:39 +01:00
|
|
|
const ldbsRtr = require('./src/routes/ldbs.routes');
|
2023-06-07 11:45:48 +01:00
|
|
|
const kubeRtr = require('./src/routes/kube.routes');
|
|
|
|
const findRtr = require('./src/routes/find.routes');
|
|
|
|
const issueRtr = require('./src/routes/issue.routes');
|
|
|
|
const statRtr = require('./src/routes/stats.routes');
|
|
|
|
const regRtr = require('./src/routes/registration.routes');
|
|
|
|
const pisRtr = require('./src/routes/pis.routes');
|
2023-06-07 12:05:35 +01:00
|
|
|
const trainRtr = require('./src/routes/train.routes');
|
2023-02-09 20:34:53 +00:00
|
|
|
|
|
|
|
// Set Server Configurations
|
2023-05-06 21:54:49 +01:00
|
|
|
const srvListen = process.env.OWL_SRV_LISTEN || '0.0.0.0';
|
|
|
|
const srvPort = process.env.OWL_SRV_PORT || 8460;
|
|
|
|
|
|
|
|
const limiter = rateLimit({
|
|
|
|
windowMs: 15 * (60 * 1000), // 15 minutes
|
|
|
|
max: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes)
|
|
|
|
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
|
|
|
|
legacyHeaders: true, // Disable the `X-RateLimit-*` headers
|
|
|
|
});
|
2023-02-09 20:34:53 +00:00
|
|
|
|
|
|
|
// Print version number:
|
2023-05-06 21:54:49 +01:00
|
|
|
log.out(`app: Starting OwlBoard in ${mode} mode`, 'init');
|
2023-06-07 11:45:48 +01:00
|
|
|
log.out(`app: Starting OwlBoard - Backend Version: ${version.app} - ` +
|
|
|
|
`API versions: ${version.api}`, 'init');
|
2023-02-09 20:34:53 +00:00
|
|
|
|
|
|
|
// Test for required vars:
|
2023-05-06 21:54:49 +01:00
|
|
|
// const varTest = require('./src/utils/varTest.utils');
|
|
|
|
// var startTest = await varTest.varTest();
|
|
|
|
//console.log("Required Vars Missing:", startTest.missing_required);
|
|
|
|
//console.log("Desired Vars Missing:", startTest.missing_desired);
|
|
|
|
// if startTest.pass == false
|
|
|
|
// console.log("Unable to start, missing required vars")
|
|
|
|
// exit app
|
2023-02-09 20:34:53 +00:00
|
|
|
|
|
|
|
// Express Error Handling:
|
|
|
|
app.use((err, req, res, next) => {
|
|
|
|
const statusCode = err.statuscode || 500;
|
|
|
|
console.error(err.message, err.stack);
|
|
|
|
res.status(statusCode).json({'message': err.message});
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
|
2023-05-06 21:54:49 +01:00
|
|
|
// Global Middleware:
|
2023-06-17 21:53:36 +01:00
|
|
|
app.use(cors()); // Allow access from any origin
|
2023-02-09 20:34:53 +00:00
|
|
|
app.use(express.json()); //JSON Parsing for POST Requests
|
2023-05-06 21:54:49 +01:00
|
|
|
app.use(compression()); // Compress API Data if supported by client
|
|
|
|
app.use(limiter);
|
2023-02-09 20:34:53 +00:00
|
|
|
|
2023-05-06 21:54:49 +01:00
|
|
|
// Unauthenticated Routes
|
2023-02-09 20:34:53 +00:00
|
|
|
app.use('/api/v1/list', listRtr);
|
|
|
|
app.use('/api/v1/ldb', ldbRtr);
|
|
|
|
app.use('/api/v1/kube', kubeRtr);
|
|
|
|
app.use('/api/v1/find', findRtr);
|
|
|
|
app.use('/api/v1/issue', issueRtr);
|
2023-05-06 21:54:49 +01:00
|
|
|
app.use('/api/v1/stats', statRtr);
|
|
|
|
app.use('/api/v1/register', regRtr);
|
|
|
|
|
|
|
|
// Authented Routes
|
2023-06-11 19:42:39 +01:00
|
|
|
app.use('/api/v1/ldbs', authenticate, ldbsRtr);
|
2023-05-06 21:54:49 +01:00
|
|
|
app.use('/api/v1/pis', authenticate, pisRtr);
|
2023-06-11 19:42:39 +01:00
|
|
|
app.use('/api/v1/auth/test', authenticate, (req, res) => res.status(200).json({
|
|
|
|
status: 'ok',
|
|
|
|
message: 'Authentication successful'
|
|
|
|
})); // Returns 401 if auth failed, 200 if successful.
|
|
|
|
app.use('/api/v1/train', authenticate, trainRtr);
|
2023-05-06 21:54:49 +01:00
|
|
|
|
|
|
|
// Number of proxies:
|
|
|
|
app.set('trust proxy', 4);
|
|
|
|
mode === 'development'
|
|
|
|
? app.get('/api/v1/ip', (req, res) => res.send(req.ip))
|
|
|
|
: null;
|
2023-02-09 20:34:53 +00:00
|
|
|
|
|
|
|
// Start Express
|
|
|
|
app.listen(srvPort, srvListen, (error) =>{
|
|
|
|
if(!error) {
|
2023-05-06 21:54:49 +01:00
|
|
|
log.out(`app.listen: Listening on http://${srvListen}:${srvPort}`, 'init');
|
|
|
|
log.out('app.listen: State - alive', 'init');
|
2023-02-09 20:34:53 +00:00
|
|
|
} else {
|
2023-05-06 21:54:49 +01:00
|
|
|
log.out(`app.listen: Error occurred, server can't start ${error}`, 'err');
|
2023-02-09 20:34:53 +00:00
|
|
|
}
|
|
|
|
});
|