forked from heroku/node-js-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
94 lines (85 loc) · 2.49 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const pg = require('pg');
const conf = {
local: {
user:"postgres",
database:"nodeex",
port:5433,
host:"localhost",
password:"12345678"
},
server: {
user:"khxbijdhccsjkc",
database:"d77n0qu161p9b3",
port:5432,
host:"ec2-54-204-40-248.compute-1.amazonaws.com",
password:"80fbc29fa8a5273e15db91d3b704ca76e7d7c8a024bb128b0d8342afaeb18a7f",
ssl:true
}};
// Connect
const client = new pg.Client(conf.server);
client.connect(function (err) {
if (err) {writeRes({status: "C Error", statusText: err});client.end();}
});
// checkEmty then getConfig
exports.getConfig = function(clientRes, user_name) {
checkEmty(clientRes);
queryString = "SELECT id, user_name, host, port, path, version FROM config WHERE user_name='"+user_name+"' LIMIT 1;";
client.query(queryString, function (err, res) {
if (err) {writeRes(clientRes, err);
} else {
if (res.rows.length <=0) {
writeRes(clientRes,{err: "Null"});
} else {
writeRes(clientRes,res.rows);
}
}
});
}
// checkEmty then setConfig
exports.setConfig = function(clientRes, config) {
checkEmty(clientRes);
queryString = "UPDATE config SET host='"+config.host
+"', port="+config.port
+", path='"+config.path
+"', version='"+config.version
+"' WHERE user_name='"+config.user_name+"';";
console.log(queryString);
client.query(queryString, function (err, res) {
if (err) {writeRes(clientRes, err);
} else {
writeRes(clientRes,{status:"success"});
}
});
}
function checkEmty(clientRes){
var queryString =
"CREATE TABLE IF NOT EXISTS config ("+
"id serial, user_name VARCHAR (25) UNIQUE,host VARCHAR (25),port integer,path VARCHAR (255),version VARCHAR (10),"+
"CONSTRAINT config_pkey PRIMARY KEY (id)"+
");";
client.query(queryString, function (err, res) {
if (err) {
writeRes(clientRes,{status: "Q1 Error",statusText: err});
client.end();
} else {console.log("OK");}
});
queryString = "SELECT * FROM config LIMIT 1;";
client.query(queryString, function (err, res) {
if (err) {
writeRes(err);client.end();
} else if (res.rows.length <= 0) {
queryString =
"INSERT INTO config(user_name, host, port, path, version)"+
"VALUES ('ncteamvn', 'ncteamvn.com', 8080, 'control', '1.0');";
client.query(queryString, function (err, res) {
if (err) {writeRes(err);client.end();}
});
} else {console.log("RRE");}
});
}
function writeRes(clientRes,obj) {
clientRes.writeHead(200,{'Content-Type':'text/json'});
clientRes.write(JSON.stringify(obj));
clientRes.end();
}
// console.log(body);