From 8779fee2aff6deb895a1ac16728fb54e5f4c1467 Mon Sep 17 00:00:00 2001 From: adb-sh Date: Mon, 14 Sep 2020 22:56:34 +0200 Subject: [PATCH] adding docker-compose --- .gitignore | 7 + README.md | 10 +- docker-compose.yml | 31 +++ index.js => node_app/index.js | 25 +-- node_app/package-lock.json | 57 ++++++ package.json => node_app/package.json | 4 +- {public => node_app/public}/index.html | 180 +++++++++--------- {public => node_app/public}/manifest.json | 0 {public => node_app/public}/sym/dark.css | 0 .../public}/sym/ic_close_white_24px.svg | 6 +- {public => node_app/public}/sym/script.js | 0 11 files changed, 209 insertions(+), 111 deletions(-) create mode 100644 .gitignore create mode 100644 docker-compose.yml rename index.js => node_app/index.js (85%) create mode 100644 node_app/package-lock.json rename package.json => node_app/package.json (73%) rename {public => node_app/public}/index.html (97%) rename {public => node_app/public}/manifest.json (100%) rename {public => node_app/public}/sym/dark.css (100%) rename {public => node_app/public}/sym/ic_close_white_24px.svg (96%) rename {public => node_app/public}/sym/script.js (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2fe4feb --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.idea/ +./redis_conf/ +./redis_data/ +./node_logs/ +./node_app./ +node_modules/ +./node_app/package-lock.json diff --git a/README.md b/README.md index 80a8d20..31b6ae4 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # short_url This is a simple application to short you urls with your own web server. It is based on Nodejs and Redis. -You will have to change the IPs in the `index.js` and `./public/index.html` files to your public IP or domain. -To start the server simply execute `index.js`: +You can change the public ports `8080` (web UI) and `8081` (websocket) in the `docker-compose.yml` file +To start the server simply use `docker-compose`: ``` -screen -A -m -d -S short_url nodejs index.js +docker-compose -p surl up -d ``` -It's recommended to use a reverse proxy like nginx to manage SSL certificates etc. +It's recommended to use a reverse proxy like nginx to manage SSL certificates. -A test instance is running under [s.adb.sh](https://s.adb.sh). +A test instance is running at [s.adb.sh](https://s.adb.sh). //good luck diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..72ff25b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,31 @@ +version: '3' + +services: + node: + image: node + container_name: surl_node + restart: always + ports: + - "8080:8080" + - "8081:8081" + volumes: + - ./node_app/:/home/node/app/ + # - ./node_modules/:/home/node/app/node_modules/ + - ./node_logs/:/var/log/ + working_dir: /home/node/app/ + environment: + - NODE_ENV=production + command: sh -c 'npm i && nodejs index.js' + links: + - redis + redis: + image: redis + container_name: surl_redis + expose: + - 6379 + restart: always + volumes: + - ./redis_data/:/var/lib/redis/ + - ./redis_conf/:/usr/local/etc/redis/ + environment: + - REDIS_REPLICATION_MODE=master diff --git a/index.js b/node_app/index.js similarity index 85% rename from index.js rename to node_app/index.js index 30dc41c..384f212 100644 --- a/index.js +++ b/node_app/index.js @@ -5,30 +5,34 @@ const ws = require('ws') const redis = require('redis') const mime = require('mime') const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest + const host = 'http://127.0.0.1:8080/' +const outpath = ['sym', '', 'manifest.json', 'admin', 'stats'] //redis client -const redis_cli = redis.createClient() +const redis_cli = redis.createClient({ + host: 'redis', + port: 6379 +}) redis_cli.on("error", function (error) { console.error(error) }) -const outpath = ['sym', '', 'manifest.json'] - //HTTP server http.createServer(function (req, res) { const q = url.parse(req.url, true); let filename = "./public" + q.pathname; - if (filename === "./public/") filename = "./public/index.html"; + let path_split = q.pathname.split("/", 3); + if (path_split[path_split.length - 1] === "") filename += "/index.html"; let file_type = mime.getType(filename) - //q.pathname.split("/", 2)[1] === "sym" || ) - if (function valid_path(){// - for (let i = 0; i < outpath.length; i++) if (q.pathname.split("/", 2)[1] === outpath[i]) return true + if (function valid_path(){ + //outpath.forEach( path => {if (request_path === path) return true}) + for (let i = 0; i < outpath.length; i++) if (path_split[1] === outpath[i]) return true return false }() === true){ fs.readFile(filename, function(err, data) { if (err) { - res.writeHead(404, {'Content-Type': file_type}); + res.writeHead(404, {'Content-Type': "text/html"}); return res.end("404 Not Found"); } res.writeHead(200, {'Content-Type': file_type}); @@ -42,7 +46,7 @@ http.createServer(function (req, res) { res.writeHead(302, {'Location': obj}); return res.end(); }else{ - res.writeHead(404, {'Content-Type': file_type}); + res.writeHead(404, {'Content-Type': "text/html"}); return res.end("404 this short-url does not exist :/"); } }); @@ -113,14 +117,13 @@ wss.on('connection', ws => { }) //random key -const forbidden_array = ['sym', 'admin', 'stats'] function get_key(length) { let forbidden = false; let output = '' let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' do{ for (let i = 0; i < length; i++ ) output += characters.charAt(Math.floor(Math.random() * characters.length)) - for (let i = 0; i < forbidden_array.length; i++) if (output === forbidden_array[i]) forbidden = true + for (let i = 0; i < output.length; i++) if (output === outpath[i]) forbidden = true redis_cli.hget("surl;"+output, "url", function(err, obj) { if(err) console.log(err) if(obj) forbidden = true diff --git a/node_app/package-lock.json b/node_app/package-lock.json new file mode 100644 index 0000000..76a5e96 --- /dev/null +++ b/node_app/package-lock.json @@ -0,0 +1,57 @@ +{ + "name": "surl", + "version": "1.1.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "denque": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/denque/-/denque-1.4.1.tgz", + "integrity": "sha512-OfzPuSZKGcgr96rf1oODnfjqBFmr1DVoc/TrItj3Ohe0Ah1C5WX5Baquw/9U9KovnQ88EqmJbD66rKYUQYN1tQ==" + }, + "mime": { + "version": "2.4.6", + "resolved": "https://registry.npmjs.org/mime/-/mime-2.4.6.tgz", + "integrity": "sha512-RZKhC3EmpBchfTGBVb8fb+RL2cWyw/32lshnsETttkBAyAUXSGHxbEJWWRXc751DrIxG1q04b8QwMbAwkRPpUA==" + }, + "redis": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/redis/-/redis-3.0.2.tgz", + "integrity": "sha512-PNhLCrjU6vKVuMOyFu7oSP296mwBkcE6lrAjruBYG5LgdSqtRBoVQIylrMyVZD/lkF24RSNNatzvYag6HRBHjQ==", + "requires": { + "denque": "^1.4.1", + "redis-commands": "^1.5.0", + "redis-errors": "^1.2.0", + "redis-parser": "^3.0.0" + } + }, + "redis-commands": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.6.0.tgz", + "integrity": "sha512-2jnZ0IkjZxvguITjFTrGiLyzQZcTvaw8DAaCXxZq/dsHXz7KfMQ3OUJy7Tz9vnRtZRVz6VRCPDvruvU8Ts44wQ==" + }, + "redis-errors": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", + "integrity": "sha1-62LSrbFeTq9GEMBK/hUpOEJQq60=" + }, + "redis-parser": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz", + "integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=", + "requires": { + "redis-errors": "^1.0.0" + } + }, + "ws": { + "version": "7.3.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.3.1.tgz", + "integrity": "sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA==" + }, + "xmlhttprequest": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz", + "integrity": "sha1-Z/4HXFwk/vOfnWX197f+dRcZaPw=" + } + } +} diff --git a/package.json b/node_app/package.json similarity index 73% rename from package.json rename to node_app/package.json index 27d1e37..9032c0f 100644 --- a/package.json +++ b/node_app/package.json @@ -1,6 +1,6 @@ { - "name": "short_url", - "version": "1.0.0", + "name": "surl", + "version": "1.1.0", "dependencies": { "mime": "^2.4.6", "redis": "^3.0.2", diff --git a/public/index.html b/node_app/public/index.html similarity index 97% rename from public/index.html rename to node_app/public/index.html index 697e6c5..692ce5f 100644 --- a/public/index.html +++ b/node_app/public/index.html @@ -1,90 +1,90 @@ - - - sURL - - - - - - - - - - - - -
-
short your url
-
-
- -
- - -
-
short
-
-
-
-
- -
-
-
your sURL:
-
- -
- -
-
copy
-
-
-
- -
-
- -
-
- -
-
-
-
- - + + + sURL + + + + + + + + + + + + +
+
short your url
+
+
+ +
+ + +
+
short
+
+
+
+
+ +
+
+
your sURL:
+
+ +
+ +
+
copy
+
+
+
+ +
+
+ +
+
+ +
+
+
+
+ + diff --git a/public/manifest.json b/node_app/public/manifest.json similarity index 100% rename from public/manifest.json rename to node_app/public/manifest.json diff --git a/public/sym/dark.css b/node_app/public/sym/dark.css similarity index 100% rename from public/sym/dark.css rename to node_app/public/sym/dark.css diff --git a/public/sym/ic_close_white_24px.svg b/node_app/public/sym/ic_close_white_24px.svg similarity index 96% rename from public/sym/ic_close_white_24px.svg rename to node_app/public/sym/ic_close_white_24px.svg index e22ffd5..5704800 100644 --- a/public/sym/ic_close_white_24px.svg +++ b/node_app/public/sym/ic_close_white_24px.svg @@ -1,4 +1,4 @@ - - - + + + \ No newline at end of file diff --git a/public/sym/script.js b/node_app/public/sym/script.js similarity index 100% rename from public/sym/script.js rename to node_app/public/sym/script.js