16185 and 16630 language switching
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
const next = require("next");
|
||||
const { createServer } = require("http");
|
||||
const { createServer: createHttpServer } = require("http");
|
||||
const { createServer: createHttpsServer } = require("https");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const appInsights = require("applicationinsights");
|
||||
|
||||
const initAppInsights = (instrumentationKey) => {
|
||||
@@ -26,44 +29,75 @@ const startServer = async (config) => {
|
||||
};
|
||||
|
||||
const app = next(serverOptions);
|
||||
await app.prepare(); // prepare BEFORE starting the server
|
||||
await app.prepare();
|
||||
|
||||
const handleNextRequests = app.getRequestHandler();
|
||||
|
||||
const srv = createServer((req, res) => {
|
||||
const httpsOptions = {
|
||||
key: fs.readFileSync(
|
||||
path.join(__dirname, "certificates", "cert-key.pem")
|
||||
),
|
||||
cert: fs.readFileSync(path.join(__dirname, "certificates", "cert.pem")),
|
||||
};
|
||||
|
||||
// HTTPS server on port 3000
|
||||
const httpsServer = createHttpsServer(httpsOptions, (req, res) => {
|
||||
if (config.useAppInsights) {
|
||||
appInsights.defaultClient.trackNodeHttpRequest({
|
||||
request: req,
|
||||
response: res,
|
||||
});
|
||||
}
|
||||
|
||||
handleNextRequests(req, res);
|
||||
});
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
srv.on("error", reject);
|
||||
srv.on("listening", () => resolve());
|
||||
srv.listen(config.port, config.hostname);
|
||||
// HTTP server on port 3001 for redirect
|
||||
const httpServer = createHttpServer((req, res) => {
|
||||
const host = req.headers.host?.split(":")[0] || "localhost";
|
||||
res.writeHead(301, {
|
||||
Location: `https://${host}:3000${req.url}`,
|
||||
});
|
||||
res.end();
|
||||
});
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
httpsServer.on("error", reject);
|
||||
httpsServer.on("listening", () => {
|
||||
console.log(`HTTPS server listening on port ${config.port}`);
|
||||
resolve();
|
||||
});
|
||||
httpsServer.listen(config.port, config.hostname);
|
||||
});
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
httpServer.on("error", reject);
|
||||
httpServer.on("listening", () => {
|
||||
console.log(
|
||||
`HTTP server listening on port ${config.httpPort} (redirecting to HTTPS)`
|
||||
);
|
||||
resolve();
|
||||
});
|
||||
httpServer.listen(config.httpPort, config.hostname);
|
||||
});
|
||||
|
||||
// Graceful shutdown handling
|
||||
process.on("SIGTERM", () => {
|
||||
console.log("Received SIGTERM, shutting down gracefully...");
|
||||
srv.close(() => {
|
||||
console.log("Server closed.");
|
||||
process.exit(0);
|
||||
httpsServer.close(() => {
|
||||
httpServer.close(() => {
|
||||
console.log("Servers closed.");
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
return app;
|
||||
};
|
||||
|
||||
// App configuration
|
||||
const startTime = Date.now();
|
||||
const serverConfig = {
|
||||
hostname: "0.0.0.0", // Required for Azure App Service
|
||||
port: parseInt(process.env.PORT, 10) || 3000,
|
||||
hostname: "0.0.0.0",
|
||||
port: 3000, // HTTPS port
|
||||
httpPort: 3001, // HTTP redirect port
|
||||
env: process.env.APP_ENV || process.env.NODE_ENV || "production",
|
||||
useAppInsights: initAppInsights(
|
||||
process.env.NEXT_PUBLIC_APPINSIGHTS_INSTRUMENTATIONKEY
|
||||
@@ -73,7 +107,7 @@ const serverConfig = {
|
||||
startServer(serverConfig)
|
||||
.then(() => {
|
||||
console.log(
|
||||
`Server ready on http://${serverConfig.hostname}:${serverConfig.port} [${serverConfig.env}]`
|
||||
`Server ready on https://${serverConfig.hostname}:${serverConfig.port} [${serverConfig.env}]`
|
||||
);
|
||||
|
||||
if (serverConfig.useAppInsights) {
|
||||
|
||||
Reference in New Issue
Block a user