-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
149 lines (130 loc) · 5.03 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// import
const BodyParser = require("body-parser");
const dbConfig = require('./dbConfig');
const domainConfig = require('./domainConfig');
const Express = require("express");
const MongoClient = require("mongodb").MongoClient;
const Timer = require("./timer");
const Queryer = require("./queryer");
// constant variable
const timer = new Timer();
const queryer = new Queryer();
const PORT = 5000;
const grant = "*";
// app
var app = Express();
app.use(BodyParser.json());
app.use(BodyParser.urlencoded({ extended: true }));
// package for log
const fs = require('fs');
const util = require('util');
var log_file = fs.createWriteStream(__dirname + "/log" + "/log.log", {flags : "a"}); // 'a' means appending (old data will be preserved)
var log_stdout = process.stdout;
// function to overwrite the console.log, so as to log the console-context to a text file
console.log = function(d) { //
log_file.write(util.format(d) + '\n');
log_stdout.write(util.format(d) + '\n');
};
// SSL cert
const http = require("http");
const https = require("https");
const domainName = domainConfig.DOMAIN;
const cert = fs.readFileSync(__dirname + domainConfig.CERT);
const ca = fs.readFileSync(__dirname + domainConfig.CA);
const key = fs.readFileSync(__dirname + domainConfig.KEY);
const httpsOptions = {
cert: cert,
ca: ca,
key: key
};
const httpsServer = https.createServer(httpsOptions, app);
// kick the server with https
httpsServer.listen(PORT, domainName, () =>{
MongoClient.connect(dbConfig.CONNECTION_URL, {useUnifiedTopology: true},
{useNewUrlParser: true },
(error, client) => {
if(error) {
console.log( timer.getCurrentLocaltimeInIso(true)
+ ": "
+ "Fail to Connect DB `"
+ dbConfig.DATABASE_NAME + "." + dbConfig.COLLECTION
+ "`.");
throw error;
}
database = client.db(dbConfig.DATABASE_NAME);
collection = database.collection(dbConfig.COLLECTION);
console.log( timer.getCurrentLocaltimeInIso(true)
+ ": "
+"Connected to DB `"
+ dbConfig.DATABASE_NAME + "." + dbConfig.COLLECTION
+ "`.");
});
});
// handle: No 'Access-Control-Allow-Origin' - Node / Apache Port Issue
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', grant);
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET') //other option: POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
app.get("/weather/api", (request, response) =>{
try{
let paraRange = request.query.dtRange;
let dtNow = new Date;
console.log( timer.convert2IsoInLocaltimeZone(dtNow)
+ ": "
+ request.ip
+ "; "
+ "visiting /weather, param={" + paraRange + "}"
);
switch(paraRange){
case '1h':
queryer.get1hWeather(request.ip, response, dtNow);
break;
case '12h':
queryer.get12hWeather(request.ip, response, dtNow);
break;
case '1d':
queryer.get1dWeather(request.ip, response, dtNow);
break;
case '7d':
queryer.get7dWeather(request.ip, response, dtNow);
break;
case '1m':
queryer.get1mWeather(request.ip, response, dtNow);
break;
case '6m':
queryer.get6mWeather(request.ip, response, dtNow);
break;
case '1y':
queryer.get1yWeather(request.ip, response, dtNow);
break;
default:
console.log( timer.convert2IsoInLocaltimeZone(dtNow)
+ ": "
+ request.ip
+ "; "
+ "Fail; Get /weather, param={" + paraRange + "}; invalid paramter."
);
return response.status(400).json({
status: "error",
message: "invalid paramter <br/> paramter: dtRange = {1h, 12h, 1d, 7d, 1m, 6m, 1y}"
});
}
}
catch(error){
let dtNow = new Date;
console.log( timer.convert2IsoInLocaltimeZone(dtNow)
+ ": "
+ request.ip
+ "; "
+ "Error; " + error.toString());
}
});