-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
155 lines (122 loc) · 3.77 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
149
150
151
152
153
154
155
const express = require("express"); // require expressjs
const compression = require("compression"); // compress stream
const bodyparser = require("body-parser"); // bodyparser
const path = require("path"); // and path
// then require our praytimes libary
const praytimes = require("./lib/praytimes.js");
// init app
var app = express();
function calc(config, lat, lng, timezone) {
// init class
var times = new praytimes();
var methods = times.getMethods();
// set default configuration
var params = methods["MWL"];
// get date
var date = new Date();
// select method (params)
if (typeof methods[config.method] === "object") {
params = methods[config.method].params;
} else {
// override method
config.method = "MWL";
}
// parse date if needed
if (config.date) {
var parsed = Date.parse(config.date);
if (isNaN(config.date) && !isNaN(parsed)) {
date = new Date(parsed);
}
}
// configure object
times.adjust(params);
// calculate times
var pt = times.getTimes(date, [lat, lng], timezone);
return {
method: config.method,
date: date,
pt: pt
};
}
// set view engine to pug
app.set("view engine", "pug");
// use static path
app.use(express.static(path.join(__dirname + "/public/")));
// use middleware
app.use(bodyparser.urlencoded({ extended: true }));
app.use(compression({level: 1}));
// set headers and log request
app.use((req, res, next) => {
// set headers
res.header("Server", "Enterprise");
res.header("X-Powered-By", "slowmo");
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// force https
if (req.headers['x-forwarded-proto'] != 'https' && process.env["FORCEHTTPS"]) {
res.redirect(302, 'https://' + req.hostname + req.originalUrl);
}
// and continue
next();
});
// index page
app.get("/", (req, res) => {
// get varibles from url query
var lat = req.query.lat;
var lng = req.query.lng;
var timezone = req.query.timezone;
// check if they're numbers
if (isNaN(lat) || isNaN(lng) || isNaN(timezone)) {
// latitude and longitude are not numbers (or timezone)
// if not set status code to 500
status_code = 500;
}
// else convert them into numbers
lat = Number(lat);
lng = Number(lng);
timezone = Number(timezone);
var {method, date, pt} = calc(req.query, lat, lng, timezone);
res.render("index", pt);
});
// api requests
app.get("/api/:lat/:lng/:timezone", (req, res) => {
// set status code
var status_code = 200;
// get varibles from url string
var lat = req.params.lat;
var lng = req.params.lng;
var timezone = req.params.timezone;
// check if they're numbers
if (isNaN(lat) || isNaN(lng) || isNaN(timezone)) {
// latitude and longitude are not numbers (or timezone)
// if not set status code to 500
status_code = 500;
}
// else convert them into numbers
lat = Number(lat);
lng = Number(lng);
timezone = Number(timezone);
// calculate times
var {method, date, pt} = calc(req.query, lat, lng, timezone);
// check if the times were calculated correctly
if (pt.imsak === "-----") {
status_code = 500;
}
// send response object
res.status(status_code).send({
date: date.toDateString(),
status: status_code,
timezone: timezone,
method: method,
coordinates: {
latitude: lat,
longitude: lng
},
times: pt
});
});
// set port
var port = process.env.PORT || 80;
app.listen(port, () => {
console.log("Listening on port", port);
});