128 lines
3.4 KiB
JavaScript
128 lines
3.4 KiB
JavaScript
import express from 'express';
|
|
import bodyParser from 'body-parser';
|
|
import cors from 'cors';
|
|
import dotenv from 'dotenv';
|
|
import { expressSession, keycloak } from './keycloak.js';
|
|
import { dbConnect } from './database/mongo.js';
|
|
import {
|
|
authRoutes,
|
|
userRoutes,
|
|
printerRoutes,
|
|
jobRoutes,
|
|
subJobRoutes,
|
|
gcodeFileRoutes,
|
|
filamentRoutes,
|
|
spotlightRoutes,
|
|
partRoutes,
|
|
productRoutes,
|
|
vendorRoutes,
|
|
materialRoutes,
|
|
partStockRoutes,
|
|
filamentStockRoutes,
|
|
stockAuditRoutes,
|
|
stockEventRoutes,
|
|
auditLogRoutes,
|
|
noteTypeRoutes,
|
|
noteRoutes,
|
|
hostRoutes,
|
|
documentSizesRoutes,
|
|
documentTemplatesRoutes,
|
|
documentPrintersRoutes,
|
|
documentJobsRoutes,
|
|
} from './routes/index.js';
|
|
import path from 'path';
|
|
import * as fs from 'fs';
|
|
import cron from 'node-cron';
|
|
import ReseedAction from './database/ReseedAction.js';
|
|
import log4js from 'log4js';
|
|
import { etcdServer } from './database/etcd.js';
|
|
import { populateUserMiddleware } from './services/misc/auth.js';
|
|
import { natsServer } from './database/nats.js';
|
|
|
|
dotenv.config();
|
|
|
|
const PORT = process.env.PORT || 8080;
|
|
const app = express();
|
|
|
|
const logger = log4js.getLogger('App');
|
|
logger.level = process.env.LOG_LEVEL;
|
|
|
|
app.use(log4js.connectLogger(logger, { level: 'trace' }));
|
|
|
|
const whitelist = [process.env.APP_URL_CLIENT, process.env.APP_URL_ELECTRON_CLIENT];
|
|
const corsOptions = {
|
|
origin: function (origin, callback) {
|
|
if (!origin || whitelist.indexOf(origin) !== -1) {
|
|
callback(null, true);
|
|
} else {
|
|
callback(new Error('Not allowed by CORS'));
|
|
}
|
|
},
|
|
credentials: true,
|
|
};
|
|
|
|
dbConnect();
|
|
|
|
// Connect to Etcd (await)
|
|
try {
|
|
etcdServer.connect();
|
|
logger.info('Connected to Etcd');
|
|
} catch (err) {
|
|
logger.error('Failed to connect to Etcd:', err);
|
|
throw err;
|
|
}
|
|
|
|
// Connect to NATS (await)
|
|
try {
|
|
natsServer.connect();
|
|
logger.info('Connected to NATS');
|
|
} catch (err) {
|
|
logger.error('Failed to connect to NATS:', err);
|
|
throw err;
|
|
}
|
|
|
|
app.use(cors(corsOptions));
|
|
app.use(bodyParser.json({ type: 'application/json', strict: false, limit: '50mb' }));
|
|
app.use(express.json());
|
|
app.use(expressSession);
|
|
app.use(keycloak.middleware());
|
|
app.use(populateUserMiddleware);
|
|
|
|
app.get('/', function (req, res) {
|
|
const __dirname = fs.realpathSync('.');
|
|
res.sendFile(path.join(__dirname, '/src/landing/index.html'));
|
|
});
|
|
|
|
app.use('/auth', authRoutes);
|
|
app.use('/users', userRoutes);
|
|
app.use('/spotlight', spotlightRoutes);
|
|
app.use('/printers', printerRoutes);
|
|
app.use('/hosts', hostRoutes);
|
|
app.use('/jobs', jobRoutes);
|
|
app.use('/subjobs', subJobRoutes);
|
|
app.use('/gcodefiles', gcodeFileRoutes);
|
|
app.use('/filaments', filamentRoutes);
|
|
app.use('/parts', partRoutes);
|
|
app.use('/products', productRoutes);
|
|
app.use('/vendors', vendorRoutes);
|
|
app.use('/materials', materialRoutes);
|
|
app.use('/partstocks', partStockRoutes);
|
|
app.use('/filamentstocks', filamentStockRoutes);
|
|
app.use('/stockevents', stockEventRoutes);
|
|
app.use('/stockaudits', stockAuditRoutes);
|
|
app.use('/auditlogs', auditLogRoutes);
|
|
app.use('/notetypes', noteTypeRoutes);
|
|
app.use('/documentsizes', documentSizesRoutes);
|
|
app.use('/documenttemplates', documentTemplatesRoutes);
|
|
app.use('/documentprinters', documentPrintersRoutes);
|
|
app.use('/documentjobs', documentJobsRoutes);
|
|
app.use('/notes', noteRoutes);
|
|
|
|
if (process.env.SCHEDULE_HOUR) {
|
|
cron.schedule(`0 */${process.env.SCHEDULE_HOUR} * * *'`, () => {
|
|
ReseedAction();
|
|
});
|
|
}
|
|
|
|
app.listen(PORT, () => logger.info(`Server listening to port ${PORT}`));
|