diff --git a/api.setup b/api.setup new file mode 100755 index 0000000..72b6161 --- /dev/null +++ b/api.setup @@ -0,0 +1,12 @@ +#!/bin/bash + +apt install nodejs npm +mkdir -p /var/log/consult +chgrp www-data /var/log/consult + +npm list -g | grep pm2 || npm install -g pm2 +pm2 stop api/srv.consult.js +pm2 delete api/srv.consult.js +pm2 start api/srv.consult.js +pm2 startup +pm2 save diff --git a/api/srv.consult.js b/api/srv.consult.js new file mode 100644 index 0000000..dc16c7b --- /dev/null +++ b/api/srv.consult.js @@ -0,0 +1,109 @@ +let fs = require("fs"); +let http = require("http"); + +let DST_DIR = "/var/log/consult"; +//let DST_DIR = "/tmp"; +let TEMPLATE_FILE_NAME = "%DIR%/consult_%UUID%"; +let URL_CONSULT = "/api/consult"; + +let srv = http.createServer((req, res) => { + if (req.method == "POST") { + var dat = ""; + req.on("data", (chunk) => { + dat += chunk; + }); + req.on("end", () => { + // /api/consult + if (isConsultationRequestValid(req, dat)) { + saveUserContacts(dat); + returnSuccess(res); + } else { + returnError(res); + } + }); + } else if (req.method == "OPTIONS") { + returnNoCORS(res); + } else { + returnError(res); + } +}); + +function isConsultationRequestValid(req, dat) { + var json = {}; + try { + json = JSON.parse(dat); + } catch (e) { + //console.log("ERR isCRV error:", e); + } + + if ( + req.url == URL_CONSULT && + req.method == "POST" && + "name" in json && + "phone" in json + ) { + return true; + } + + return false; +} + +function returnError(res) { + res.writeHead(404, { "Content-Type": "text/html" }); + res.end(` +404 Not Found + +

404 Not Found

+
nginx/1.24.0 (Ubuntu)
+ +`); +} + +function returnNoCORS(res) { + res.writeHead( + 204, + { + "Access-Control-Allow-Headers": "Content-Type", + "Access-Control-Allow-Origin": "*", + "Content-Length": "0", + "Content-Type": "text/plain;charset=UTF-8", + } + ); + res.end(); +} + +function returnSuccess(res) { + res.writeHead( + 200, + { + "Access-Control-Allow-Origin": "*", + "Content-Type": "application/json;charset=UTF-8", + } + ); + let dat = { code: 4 }; + res.end(JSON.stringify(dat)); +} + +function saveUserContacts(dat) { + let fileName = TEMPLATE_FILE_NAME + .replaceAll("%DIR%", DST_DIR) + .replaceAll("%UUID%", uuidString()); + fs.writeFile(fileName, dat, (err) => { + console.log("ИГР saveUC fileN/err:", fileName, err); + }); +} + +// https://stackoverflow.com/a/2117523 +function uuidString() { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace( + /[xy]/g, + function(c) + { + var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); + return v.toString(16); + } + ); +} + +// Launch. +srv.listen(8001); diff --git a/api/test b/api/test new file mode 100755 index 0000000..50f9a3c --- /dev/null +++ b/api/test @@ -0,0 +1,3 @@ +#!/bin/bash +curl -X POST -d '.' http://localhost:8001/api/consult +curl -X POST -d '{"name":"test-name","phone":"+79001002030"}' http://localhost:8001/api/consult diff --git a/api/test-nginx b/api/test-nginx new file mode 100755 index 0000000..506536f --- /dev/null +++ b/api/test-nginx @@ -0,0 +1,2 @@ +#!/bin/bash +curl -X POST -d '{"name":"test-name","phone":"+79001002030"}' http://167.17.178.89/api/consult diff --git a/nginx.setup b/nginx.setup new file mode 100755 index 0000000..4fc31be --- /dev/null +++ b/nginx.setup @@ -0,0 +1,5 @@ +#!/bin/bash + +apt install nginx +cp nginx/cfg /etc/nginx/sites-enabled/default +systemctl restart nginx diff --git a/nginx/cfg b/nginx/cfg new file mode 100644 index 0000000..079181e --- /dev/null +++ b/nginx/cfg @@ -0,0 +1,46 @@ +server { + listen 80 default_server; + listen [::]:80 default_server; + + # SSL configuration + # + # listen 443 ssl default_server; + # listen [::]:443 ssl default_server; + # + # Note: You should disable gzip for SSL traffic. + # See: https://bugs.debian.org/773332 + # + # Read up on ssl_ciphers to ensure a secure configuration. + # See: https://bugs.debian.org/765782 + # + # Self signed certs generated by the ssl-cert package + # Don't use them in a production server! + # + # include snippets/snakeoil.conf; + + root /var/www/html; + + # Add index.php to the list if you are using PHP + index index.html; + + server_name _; + + location / { + # First attempt to serve request as file, then + # as directory, then fall back to displaying a 404. + try_files $uri $uri/ =404; + add_header "Access-Control-Allow-Origin" "*"; + # Remove CORS. + if ($request_method = "OPTIONS") { + add_header "Access-Control-Allow-Origin" "*"; + add_header "Access-Control-Allow-Headers" "Content-Type"; + add_header "Content-Type" "text/plain;charset=UTF-8"; + add_header "Content-Length" 0; + return 204; + } + } + + location /api { + proxy_pass http://localhost:8001; + } +}