
// Load WebServer
import express from "express";

// Load Templating Engine
import Nunjucks from "nunjucks";

// import WebServer Routes
import {
    Routes as PrinterAPI
} from "./routes/api.printer.mjs";

import {
    Routes as TemplateAPI
} from "./routes/api.templates.mjs";

import {
    Routes as MediaApi
} from "./routes/api.media.mjs";

import {
    Routes as QueueApi
} from "./routes/api.queue.mjs";

import {
    Routes as UIRouter
} from "./routes/ui.settings.mjs";

// Import Database Models & start Database-Connection
import './sequelize/db_conn.mjs';

// Initialize WebServer
const app = express();
const port = 3000;

// Middleware, um CORS zu aktivieren
app.use((req, res, next) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
    res.header("Access-Control-Allow-Headers", "Content-Type, Authorization");
    next();
});

// Configure Templating Engine
Nunjucks.configure('./', {
    autoescape: false,
    express: app,
    noCache: true
});

// Mount Middlewares to WebServer
app.use(express.json());
app.use(express.static('./assets/'))

// Mount Routes to WebServer
app.use('/', UIRouter);
app.use('/api', PrinterAPI);
app.use('/api', MediaApi);
app.use('/api', TemplateAPI);
app.use('/api', QueueApi);

// Server starten
app.listen(port, () => {
    console.log(`Server is listening on port ${port}`);
});