Skip to content

Commit

Permalink
issue #1019
Browse files Browse the repository at this point in the history
  • Loading branch information
migbash committed Jan 27, 2023
1 parent f0812ed commit 20e8b53
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ COPY . .

EXPOSE 5055
# CMD ["node", "./build"]
CMD ["npm", "start"]
CMD ["npm", "run", "start:docker"]
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: npm run start:heroku
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"docker-dev-up": "docker-compose -f docker-compose.dev.yml up",
"build": "vite build",
"staging": "npm run dev -- --mode staging",
"start": "node my-server.js",
"start:docker": "node server-docker.js",
"start:heroku": "node server-heroku.js",
"preview": "vite preview --host --port 5000",
"check": "svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-check --tsconfig ./tsconfig.json --watch",
Expand Down
22 changes: 0 additions & 22 deletions my-server.js → server-docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,6 @@ const options = {

const app = express();

/**
* [ℹ] add a route that lives separately from the SvelteKit app
*/
// app.get('/healthcheck', (req, res) => {
// res.end('ok');
// });

/**
* [ℹ] enable ssl redirect
* [ℹ] https://www.npmjs.com/package/heroku-ssl-redirect
* => does not seem to be working
*/
// app.use(sslRedirect());

/**
* [ℹ] separate from SvelteKit endpoint in attempts to
* [ℹ] identify clients (IP - address)
Expand Down Expand Up @@ -97,14 +83,6 @@ app.use(compression())
*/
app.use(handler);

/**
* [ℹ] initialize app;
* [ℹ] https://stackoverflow.com/questions/15693192/heroku-node-js-error-web-process-failed-to-bind-to-port-within-60-seconds-o
*/
// app.listen(process.env.PORT, () => {
// console.log(`listening on port ${process.env.PORT}`);
// });

// ~~~~~~~~~~~~~~~~~~~~~~
// APP SPIN-UP [PROD] [V2]
// ~~~~~~~~~~~~~~~~~~~~~~
Expand Down
96 changes: 96 additions & 0 deletions server-heroku.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// DOC: https://github.com/sveltejs/kit/tree/master/packages/adapter-node#custom-server
// DOC: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

import express from 'express';
import { handler } from './build/handler.js';
// import sslRedirect from 'heroku-ssl-redirect';
import compression from 'compression'; // DOC: https://expressjs.com/en/resources/middleware/compression.html
import * as sslify from 'express-sslify';
import * as requestIp from 'request-ip'; // DOC: https://www.npmjs.com/package/request-ip

const app = express();

/**
* [ℹ] add a route that lives separately from the SvelteKit app
*/
// app.get('/healthcheck', (req, res) => {
// res.end('ok');
// });

/**
* [ℹ] enable ssl redirect
* DOC: https://www.npmjs.com/package/heroku-ssl-redirect
* => does not seem to be working
*/
// app.use(sslRedirect());

/**
* [ℹ] separate from SvelteKit endpoint in attempts to
* [ℹ] identify clients (IP - address)
*/
app.get('/getClientIP', (req, res, next) => {

const ip = req.headers['x-forwarded-for'] ||
req.socket.remoteAddress ||
null;
console.log('ip', ip);

const ip2 = req.ip
console.log('ip2', ip2);

const ip3 = requestIp.getClientIp(req);
console.log('ip3', ip3);

let ipAddr = req.headers["x-forwarded-for"];
if (ipAddr){
const list = ipAddr.split(",");
ipAddr = list[list.length-1];
} else {
ipAddr = req.connection.remoteAddress;
}

console.log('ipAddr', ipAddr);

res.json(
{
"ip": ip.toString().replace(/,/g, '')
}
);

res.end()
})

/**
* [ℹ] [FORCE] https-redirect
* DOC: https://jaketrent.com/post/https-redirect-node-heroku
* DOC: https://webdva.github.io/how-to-force-express-https-tutorial
*/
app.use((req, res, next) => {
if (req.header('x-forwarded-proto') !== 'https')
res.redirect(`https://${req.header('host')}${req.url}`);
else
next();
});

/**
* DOC: https://www.npmjs.com/package/express-sslify
* DOC: https://stackoverflow.com/questions/51234023/heroku-nodejs-redirect-http-to-http
*/
app.use(sslify.HTTPS({ trustProtoHeader: true }))

// [ℹ] compress all responses
app.use(compression())

/**
* [ℹ] let SvelteKit handle everything else,
* [ℹ] including serving prerendered pages and static assets
*/
app.use(handler);

/**
* [ℹ] initialize app;
* DOC: https://stackoverflow.com/questions/15693192/heroku-node-js-error-web-process-failed-to-bind-to-port-within-60-seconds-o
*/
app.listen(process.env.PORT || 5000, () => {
console.log('listening on port 5000');
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
"$lib/*": ["src/lib/*"]
}
},
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.ts", "src/**/*.svelte", "my-server.js"]
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.ts", "src/**/*.svelte", "docker-server.js", "heroku-server.js"]
}

0 comments on commit 20e8b53

Please sign in to comment.