-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
177 lines (153 loc) · 6.39 KB
/
index.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const http = require('http');
const https=require('https')
const fetch = require('node-fetch');
require('dotenv').config();
const { IPinfoWrapper } = require("node-ipinfo");
const googleMapsApiKey = process.env.GOOGLE_MAPS_API_KEY;
const openWeatherApiKey = process.env.OPEN_WEATHER_API_KEY;
const ipInfoApiKey = process.env.IP_INFO_API_KEY;
const ipinfo = new IPinfoWrapper(ipInfoApiKey);
async function fetchStartData(latitude, longitude) {
try {
const openWeatherApiResponse = await fetch(`https://api.openweathermap.org/data/3.0/onecall?lat=${latitude}&lon=${longitude}&exclude=minutely&appid=${openWeatherApiKey}`);
const data = await openWeatherApiResponse.json();
if (!openWeatherApiResponse.ok) {
throw new Error(`HTTP error! status: ${openWeatherApiResponse.status}`);
}
return data;
} catch (error) {
console.error('An error occurred while fetching the start data:', error);
throw error; // re-throw the error to be handled by the caller
}
}
function geocode(address) {
return new Promise((resolve, reject) => {
const url = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(address)}&key=${googleMapsApiKey}`;
https.get(url, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
const apiResponse = JSON.parse(data);
if (apiResponse.status == 'OK') {
const lat = apiResponse.results[0].geometry.location.lat;
const lng = apiResponse.results[0].geometry.location.lng;
resolve({lat, lng});
} else {
console.error(`Geocoding error: ${apiResponse.status}`);
reject(null);
}
});
}).on('error', (error) => {
console.error(`Geocoding error: ${error}`);
reject(null);
});
})
}
async function getCoordinates(address) {
try {
const coordinates = await geocode(address);
const latitude = coordinates.lat;
const longitude = coordinates.lng;
const openWeatherApiResponse = await fetch(`https://api.openweathermap.org/data/3.0/onecall?lat=${latitude}&lon=${longitude}&exclude=minutely&appid=${openWeatherApiKey}`);
return openWeatherApiResponse.json();
} catch (error) {
console.error('Error:', error);
return;
}
}
async function fetchWeatherData(address, res) {
try {
const weatherData = await getCoordinates(address);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(weatherData));
} catch (error) {
console.error('An error occurred while fetching the weather data:', error);
res.writeHead(500, {'Content-Type': 'application/json'});
res.end(JSON.stringify({ error: 'An error occurred while fetching the weather data' }));
}
}
async function handleWeatherDataRequest(req, res) {
if (req.url.startsWith('/start-weather-data')) {
try {
const coords = new URL(req.url, `http://${req.headers.host}`).searchParams.get('coords');
const [latitude, longitude] = coords.split(',');
const weatherData = await fetchStartData(latitude, longitude);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(weatherData));
} catch (error) {
console.error('An error occurred while handling the weather data request:', error);
res.writeHead(500, {'Content-Type': 'application/json'});
res.end(JSON.stringify({ error: 'An error occurred while fetching the weather data' }));
}
}
}
async function handleIpDataRequest(req, res, ip) {
if (req.url.startsWith('/ip-weather-data')) {
if (!ip) {
console.error('No IP address provided');
res.writeHead(400, {'Content-Type': 'application/json'});
res.end(JSON.stringify({ error: 'No IP address provided' }));
return;
}
try {
const response = await fetch(`https://ipinfo.io/${ip}?token=${ipInfoApiKey}`);
const responseData = await response.json();
console.log(responseData);
const coords = responseData.loc;
console.log(coords);
const [latitude, longitude] = coords.split(",");
console.log(latitude, longitude);
const openWeatherApiResponse = await fetch(`https://api.openweathermap.org/data/3.0/onecall?lat=${latitude}&lon=${longitude}&exclude=minutely&appid=${openWeatherApiKey}`);
const weatherData = await openWeatherApiResponse.json();
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(weatherData));
} catch (error) {
console.error('Failed to fetch IP data:', error);
res.writeHead(500, {'Content-Type': 'application/json'});
res.end(JSON.stringify({ error: 'Failed to fetch IP data' }));
}
}
}
const server = http.createServer((req, res) => {
// Set for production
res.setHeader('Access-Control-Allow-Origin', 'https://wilsons-weather.com');
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.setHeader('Access-Control-Allow-Credentials', 'true');
const ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
console.log(ip);
if (req.url.startsWith('/search-locations')) {
const userInput = new URL(req.url, `http://${req.headers.host}`).searchParams.get('input');
const googleApiUrl = `https://maps.googleapis.com/maps/api/place/autocomplete/json?input=${encodeURIComponent(userInput)}&types=(cities)&key=${googleMapsApiKey}`;
https.get(googleApiUrl, (apiRes) => {
let data = '';
apiRes.on('data', (chunk) => {
data += chunk;
});
apiRes.on('end', () => {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(data);
});
}).on('error', (error) => {
console.error(error);
res.writeHead(500);
res.end('Error contacting Google API');
});
}
if (req.url.startsWith('/weather-data')) {
const address = new URL(req.url, `http://${req.headers.host}`).searchParams.get('input');
fetchWeatherData(address, res);
}
if (req.url.startsWith('/start-weather-data')) {
handleWeatherDataRequest(req, res);
}
if (req.url.startsWith('/ip-weather-data')){
handleIpDataRequest(req, res, ip);
}
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server is running on port: ${PORT}`);
});