Browse Source

Initial commit

master
Kim Grytøyr 4 months 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 @@
1
+FROM node:9.5.0 as builder
2
+
3
+ARG NODE_ENV=production
4
+ENV NODE_ENV $NODE_ENV
5
+
6
+WORKDIR /home/node/app
7
+
8
+COPY src/package*.json ./
9
+
10
+RUN npm install && npm cache clean --force
11
+
12
+FROM node:9.5.0-alpine as app
13
+
14
+ARG PORT=3060
15
+ENV PORT $PORT
16
+EXPOSE $PORT
17
+
18
+ARG NODE_ENV=production
19
+ENV NODE_ENV $NODE_ENV
20
+
21
+ARG NPM_CONFIG_PREFIX=/home/node/.npm-global
22
+ENV NPM_CONFIG_PREFIX=$NPM_CONFIG_PREFIX
23
+ENV PATH /home/node/.npm-global/bin:$PATH
24
+
25
+WORKDIR /home/node/app
26
+
27
+RUN npm install nodemon -g && mkdir -p /home/node/app && chown -R node:node /home/node/
28
+
29
+COPY --chown=node:node --from=builder /home/node/app/node_modules node_modules/
30
+COPY --chown=node:node ./src ./src
31
+
32
+USER node:node
33
+
34
+WORKDIR /home/node/app/src
35
+CMD ["node", "bin/www"]

+ 16
- 0
docker-compose.yml View File

@@ -0,0 +1,16 @@
1
+version: '3.1'
2
+
3
+services:
4
+  node:
5
+    build:
6
+      context: .
7
+      args:
8
+        - NODE_ENV=development
9
+        - PORT=3060
10
+    volumes:
11
+      - ./src:/home/node/app/src
12
+    ports:
13
+      - "3060:3060"
14
+    environment:
15
+      - "NODE_ENV=development"
16
+    command: ["nodemon", "bin/www"]

+ 17
- 0
src/app.js View File

@@ -0,0 +1,17 @@
1
+const requestIp = require('request-ip')
2
+
3
+const requestHandler = (request, response) => {
4
+    const clientIp = requestIp.getClientIp(request)
5
+    const reply = `${clientIp}`
6
+
7
+    console.log(`ip: ${clientIp}`)
8
+
9
+    response.writeHead(200, {
10
+        'Content-Type': 'text/plain',
11
+        'Content-Length': Buffer.byteLength(reply)
12
+    })
13
+    //response.write(clientIp)
14
+    response.end(reply, 'utf8')
15
+}
16
+
17
+module.exports = requestHandler;

+ 90
- 0
src/bin/www View File

@@ -0,0 +1,90 @@
1
+#!/usr/bin/env node
2
+
3
+/**
4
+ * Module dependencies.
5
+ */
6
+
7
+var app = require('../app');
8
+var debug = require('debug')('ip:server');
9
+var http = require('http');
10
+
11
+/**
12
+ * Get port from environment and store in Express.
13
+ */
14
+
15
+var port = normalizePort(process.env.PORT || '3060');
16
+
17
+/**
18
+ * Create HTTP server.
19
+ */
20
+
21
+var server = http.createServer(app);
22
+
23
+/**
24
+ * Listen on provided port, on all network interfaces.
25
+ */
26
+
27
+server.listen(port);
28
+server.on('error', onError);
29
+server.on('listening', onListening);
30
+
31
+/**
32
+ * Normalize a port into a number, string, or false.
33
+ */
34
+
35
+function normalizePort(val) {
36
+  var port = parseInt(val, 10);
37
+
38
+  if (isNaN(port)) {
39
+    // named pipe
40
+    return val;
41
+  }
42
+
43
+  if (port >= 0) {
44
+    // port number
45
+    return port;
46
+  }
47
+
48
+  return false;
49
+}
50
+
51
+/**
52
+ * Event listener for HTTP server "error" event.
53
+ */
54
+
55
+function onError(error) {
56
+  if (error.syscall !== 'listen') {
57
+    throw error;
58
+  }
59
+
60
+  var bind = typeof port === 'string'
61
+    ? 'Pipe ' + port
62
+    : 'Port ' + port;
63
+
64
+  // handle specific listen errors with friendly messages
65
+  switch (error.code) {
66
+    case 'EACCES':
67
+      console.error(bind + ' requires elevated privileges');
68
+      process.exit(1);
69
+      break;
70
+    case 'EADDRINUSE':
71
+      console.error(bind + ' is already in use');
72
+      process.exit(1);
73
+      break;
74
+    default:
75
+      throw error;
76
+  }
77
+}
78
+
79
+/**
80
+ * Event listener for HTTP server "listening" event.
81
+ */
82
+
83
+function onListening() {
84
+  var addr = server.address();
85
+  var bind = typeof addr === 'string'
86
+    ? 'pipe ' + addr
87
+    : 'port ' + addr.port;
88
+  debug('Listening on ' + bind);
89
+}
90
+

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

@@ -0,0 +1,34 @@
1
+{
2
+  "name": "ip",
3
+  "version": "1.0.0",
4
+  "lockfileVersion": 1,
5
+  "requires": true,
6
+  "dependencies": {
7
+    "debug": {
8
+      "version": "2.6.9",
9
+      "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
10
+      "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
11
+      "requires": {
12
+        "ms": "2.0.0"
13
+      }
14
+    },
15
+    "is_js": {
16
+      "version": "0.9.0",
17
+      "resolved": "https://registry.npmjs.org/is_js/-/is_js-0.9.0.tgz",
18
+      "integrity": "sha1-CrlFQFArp6+iTIVqqYVWFmnpxS0="
19
+    },
20
+    "ms": {
21
+      "version": "2.0.0",
22
+      "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
23
+      "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
24
+    },
25
+    "request-ip": {
26
+      "version": "2.1.3",
27
+      "resolved": "https://registry.npmjs.org/request-ip/-/request-ip-2.1.3.tgz",
28
+      "integrity": "sha512-J3qdE/IhVM3BXkwMIVO4yFrvhJlU3H7JH16+6yHucadT4fePnR8dyh+vEs6FIx0S2x5TCt2ptiPfHcn0sqhbYQ==",
29
+      "requires": {
30
+        "is_js": "^0.9.0"
31
+      }
32
+    }
33
+  }
34
+}

+ 15
- 0
src/package.json View File

@@ -0,0 +1,15 @@
1
+{
2
+  "name": "ip",
3
+  "version": "1.0.0",
4
+  "description": "",
5
+  "main": "index.js",
6
+  "scripts": {
7
+    "test": "echo \"Error: no test specified\" && exit 1"
8
+  },
9
+  "author": "Kim Grytøyr",
10
+  "license": "ISC",
11
+  "dependencies": {
12
+    "debug": "~2.6.9",
13
+    "request-ip": "^2.0.2"
14
+  }
15
+}

Loading…
Cancel
Save