Browse Source

Initial commit

master
Kim Grytøyr 1 year ago
parent
commit
f1471de286
Signed by: Kim Grytøyr <kim@grytoyr.io> GPG Key ID: 759EF8C94E0B45B7
6 changed files with 207 additions and 0 deletions
  1. 35
    0
      Dockerfile
  2. 16
    0
      docker-compose.yml
  3. 17
    0
      src/app.js
  4. 90
    0
      src/bin/www
  5. 34
    0
      src/package-lock.json
  6. 15
    0
      src/package.json

+ 35
- 0
Dockerfile View File

@@ -0,0 +1,35 @@
FROM node:9.5.0 as builder

ARG NODE_ENV=production
ENV NODE_ENV $NODE_ENV

WORKDIR /home/node/app

COPY src/package*.json ./

RUN npm install && npm cache clean --force

FROM node:9.5.0-alpine as app

ARG PORT=3060
ENV PORT $PORT
EXPOSE $PORT

ARG NODE_ENV=production
ENV NODE_ENV $NODE_ENV

ARG NPM_CONFIG_PREFIX=/home/node/.npm-global
ENV NPM_CONFIG_PREFIX=$NPM_CONFIG_PREFIX
ENV PATH /home/node/.npm-global/bin:$PATH

WORKDIR /home/node/app

RUN npm install nodemon -g && mkdir -p /home/node/app && chown -R node:node /home/node/

COPY --chown=node:node --from=builder /home/node/app/node_modules node_modules/
COPY --chown=node:node ./src ./src

USER node:node

WORKDIR /home/node/app/src
CMD ["node", "bin/www"]

+ 16
- 0
docker-compose.yml View File

@@ -0,0 +1,16 @@
version: '3.1'

services:
node:
build:
context: .
args:
- NODE_ENV=development
- PORT=3060
volumes:
- ./src:/home/node/app/src
ports:
- "3060:3060"
environment:
- "NODE_ENV=development"
command: ["nodemon", "bin/www"]

+ 17
- 0
src/app.js View File

@@ -0,0 +1,17 @@
const requestIp = require('request-ip')

const requestHandler = (request, response) => {
const clientIp = requestIp.getClientIp(request)
const reply = `${clientIp}`

console.log(`ip: ${clientIp}`)

response.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Length': Buffer.byteLength(reply)
})
//response.write(clientIp)
response.end(reply, 'utf8')
}

module.exports = requestHandler;

+ 90
- 0
src/bin/www View File

@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('ip:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3060');

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}


+ 34
- 0
src/package-lock.json View File

@@ -0,0 +1,34 @@
{
"name": "ip",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"requires": {
"ms": "2.0.0"
}
},
"is_js": {
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/is_js/-/is_js-0.9.0.tgz",
"integrity": "sha1-CrlFQFArp6+iTIVqqYVWFmnpxS0="
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
},
"request-ip": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/request-ip/-/request-ip-2.1.3.tgz",
"integrity": "sha512-J3qdE/IhVM3BXkwMIVO4yFrvhJlU3H7JH16+6yHucadT4fePnR8dyh+vEs6FIx0S2x5TCt2ptiPfHcn0sqhbYQ==",
"requires": {
"is_js": "^0.9.0"
}
}
}
}

+ 15
- 0
src/package.json View File

@@ -0,0 +1,15 @@
{
"name": "ip",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Kim Grytøyr",
"license": "ISC",
"dependencies": {
"debug": "~2.6.9",
"request-ip": "^2.0.2"
}
}

Loading…
Cancel
Save