more ENV refactoring
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
NODE_TLS_REJECT_UNAUTHORIZED=0
|
||||
# NODE_EXTRA_CA_CERTS=C:\\Users\\popov\\AppData\\Local\\mkcert
|
||||
PROTOCOL=https
|
||||
NEXT_PUBLIC_PROTOCOL=https
|
||||
NEXT_PUBLIC_HOST=localhost
|
||||
NEXT_PUBLIC_PORT=3003
|
||||
PUBLIC_URL=https://localhost:3003
|
||||
|
@ -12,7 +12,7 @@ module.exports = {
|
||||
pageExtensions: ['ts', 'tsx', 'md', 'mdx'], // Replace `jsx?` with `tsx?`
|
||||
env: {
|
||||
env: process.env.NODE_ENV,
|
||||
server: 'http://' + process.env.NEXT_PUBLIC_HOST + ':' + process.env.NEXT_PUBLIC_PORT + '',
|
||||
server: process.env.NEXT_PUBLIC_PROTOCOL + '://' + process.env.NEXT_PUBLIC_HOST + ':' + process.env.NEXT_PUBLIC_PORT + '',
|
||||
},
|
||||
webpack(config, { isServer }) {
|
||||
|
||||
|
57
server.js
57
server.js
@ -35,15 +35,14 @@ require('dotenv').config({
|
||||
|
||||
console.log("process.env.NODE_ENV = ", process.env.NODE_ENV);
|
||||
|
||||
const PROTOCOL = process.env.PROTOCOL;
|
||||
const PROTOCOL = process.env.NEXT_PUBLIC_PROTOCOL;
|
||||
const PORT = process.env.NEXT_PUBLIC_PORT || 3000;
|
||||
const HOST = process.env.NEXT_PUBLIC_HOST;
|
||||
const LOAD_CERTS = process.env.PROTOCOL === 'https';
|
||||
|
||||
const dev = process.env.NODE_ENV !== "production";
|
||||
const nextApp = next({ dev });
|
||||
const nextHandler = nextApp.getRequestHandler();
|
||||
console.log("process.env.PROTOCOL = ", process.env.PROTOCOL);
|
||||
console.log("process.env.NEXT_PUBLIC_PROTOCOL = ", process.env.NEXT_PUBLIC_PROTOCOL);
|
||||
console.log("process.env.PUBLIC_URL = ", process.env.PUBLIC_URL);
|
||||
console.log("process.env.NEXT_PUBLIC_PORT = ", process.env.NEXT_PUBLIC_PORT);
|
||||
console.log("process.env.TELEGRAM_BOT = ", process.env.TELEGRAM_BOT);
|
||||
@ -64,12 +63,37 @@ const uploadTmp = multer({ storage: storageMem });
|
||||
|
||||
|
||||
const prisma = common.getPrismaClient();
|
||||
const server = express();
|
||||
|
||||
//check if ssl is enabled
|
||||
if (process.env.NEXT_PUBLIC_PROTOCOL === 'https') {
|
||||
console.log("SSL_ENABLED = true");
|
||||
// Redirect from http to https
|
||||
// server.use((req, res, next) => {
|
||||
// if (req.headers['x-forwarded-proto'] !== 'https') {
|
||||
// return res.redirect(`https://${req.headers.host}${req.url}`);
|
||||
// }
|
||||
// next();
|
||||
// });
|
||||
if (process.env.SSL_KEY && process.env.SSL_CERT) {
|
||||
const options = {
|
||||
key: fs.readFileSync(process.env.SSL_KEY),
|
||||
cert: fs.readFileSync(process.env.SSL_CERT),
|
||||
secureProtocol: 'TLSv1_2_method', // Example: Force TLS 1.2
|
||||
};
|
||||
https.createServer(options, server).listen(PORT);
|
||||
}
|
||||
}
|
||||
else {
|
||||
server.listen(PORT, (err) => {
|
||||
if (err) throw err;
|
||||
console.log(`> Ready on ${PROTOCOL}://${HOST}:${PORT}`);
|
||||
});
|
||||
}
|
||||
// handlers
|
||||
nextApp
|
||||
.prepare()
|
||||
.then(() => {
|
||||
const server = express();
|
||||
|
||||
// Add the middleware to set 'x-forwarded-host' header
|
||||
server.use((req, res, next) => {
|
||||
@ -560,31 +584,6 @@ nextApp
|
||||
return nextHandler(req, res);
|
||||
});
|
||||
|
||||
//check if ssl is enabled
|
||||
if (LOAD_CERTS) {
|
||||
console.log("SSL_ENABLED = true");
|
||||
// Redirect from http to https
|
||||
// server.use((req, res, next) => {
|
||||
// if (req.headers['x-forwarded-proto'] !== 'https') {
|
||||
// return res.redirect(`https://${req.headers.host}${req.url}`);
|
||||
// }
|
||||
// next();
|
||||
// });
|
||||
if (process.env.SSL_KEY && process.env.SSL_CERT) {
|
||||
const options = {
|
||||
key: fs.readFileSync(process.env.SSL_KEY),
|
||||
cert: fs.readFileSync(process.env.SSL_CERT),
|
||||
secureProtocol: 'TLSv1_2_method', // Example: Force TLS 1.2
|
||||
};
|
||||
https.createServer(options, server).listen(PORT);
|
||||
}
|
||||
}
|
||||
else {
|
||||
server.listen(PORT, (err) => {
|
||||
if (err) throw err;
|
||||
console.log(`> Ready on ${PROTOCOL}://${HOST}:${PORT}`);
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch((ex) => {
|
||||
console.warn(`Error starting server on ${HOST}:${PORT}`)
|
||||
|
Reference in New Issue
Block a user