added wpa needed files. WPA is still WIP.

This commit is contained in:
Dobromir Popov
2024-04-06 19:22:35 +03:00
parent 239207e1d3
commit b669e71146
10 changed files with 95 additions and 32 deletions

View File

@ -3,10 +3,13 @@ const multer = require('multer');
const next = require("next");
const path = require("path");
const fs = require("fs");
// const { promises: fs } = require('fs');
const dotenv = require("dotenv");
const data = require("./src/helpers/data");
const sharp = require('sharp');
const https = require('https');
const { parse } = require('url');
const { join } = require('path');
//const getPort = require("get-port");
@ -18,7 +21,7 @@ import('get-port').then(module => {
process.env.TZ = 'Europe/Sofia';
// Global variable to store the base URL
let baseUrlGlobal;
// let baseUrlGlobal;
console.log("initial process.env.APP_ENV = ", process.env.APP_ENV);
console.log("initial process.env.NODE_ENV = ", process.env.NODE_ENV); //NODE_ENV can be passed as docker param
@ -84,25 +87,43 @@ else {
console.log(`> Ready on ${PROTOCOL}://${HOST}:${PORT}`);
});
}
// handlers
nextApp
.prepare()
.then(() => {
// Add the middleware to set 'x-forwarded-host' header
// Middleware to handle the 'x-forwarded-host' header
server.use((req, res, next) => {
req.headers['x-forwarded-host'] = req.headers['x-forwarded-host'] || req.headers.host;
// ---------------
if (!baseUrlGlobal) {
const protocol = req.headers['x-forwarded-proto'] || 'http';
const host = req.headers.host;
const baseUrl = `${protocol}://${host}`;
baseUrlGlobal = baseUrl;
fs.writeFileSync(path.join(__dirname, 'baseUrl.txt'), baseUrlGlobal, 'utf8');
console.log("baseUrlGlobal set to: " + baseUrlGlobal);
}
next();
});
// Middleware to handle service worker and specific js files
server.use(async (req, res, next) => {
const parsedUrl = parse(req.url, true);
const { pathname } = parsedUrl;
// Check if the request is for a service worker or a specific js file
// if (pathname === '/sw.js' || /^\/(workbox|worker|fallback)-\w+\.js$/.test(pathname)) {
// const filePath =
// join(__dirname, '.next', pathname);
// // Use Next.js's serveStatic method to serve the file from the .next directory
// try {
// const fileContent = fs.readFileSync(filePath);
// res.setHeader('Content-Type', 'application/javascript');
// res.send(fileContent);
// } catch (err) {
// // If there's an error (file not found, etc.), pass to the next handler
// next(err);
// }
// } else {
// // For all other requests, proceed to the next middleware
// next();
// }
});
server.use("/favicon.ico", express.static("styles/favicon_io/favicon.ico"));
// server.use("/robots.txt", express.static("styles/favicon_io/robots.txt"));
// server.use("/sitemap.xml", express.static("styles/favicon_io/sitemap.xml"));
@ -659,5 +680,5 @@ async function Stat() {
Stat();
exports.baseUrlGlobal = baseUrlGlobal;
exports.default = nextApp;
// exports.baseUrlGlobal = baseUrlGlobal;
// exports.default = nextApp;