Create NodeJS configuration script (#2)
This commit is contained in:
12
api.setup
Executable file
12
api.setup
Executable file
@@ -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
|
||||||
109
api/srv.consult.js
Normal file
109
api/srv.consult.js
Normal file
@@ -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(`<html>
|
||||||
|
<head><title>404 Not Found</title></head>
|
||||||
|
<body>
|
||||||
|
<center><h1>404 Not Found</h1></center>
|
||||||
|
<hr><center>nginx/1.24.0 (Ubuntu)</center>
|
||||||
|
</body>
|
||||||
|
</html>`);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
3
api/test
Executable file
3
api/test
Executable file
@@ -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
|
||||||
2
api/test-nginx
Executable file
2
api/test-nginx
Executable file
@@ -0,0 +1,2 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
curl -X POST -d '{"name":"test-name","phone":"+79001002030"}' http://167.17.178.89/api/consult
|
||||||
5
nginx.setup
Executable file
5
nginx.setup
Executable file
@@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
apt install nginx
|
||||||
|
cp nginx/cfg /etc/nginx/sites-enabled/default
|
||||||
|
systemctl restart nginx
|
||||||
46
nginx/cfg
Normal file
46
nginx/cfg
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user