forked from ChakshuGautam/geoquery.in
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
87 lines (75 loc) · 3.13 KB
/
app.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
import { Reader } from '@maxmind/geoip2-node';
import { Router } from '@stricjs/router';
import * as fs from 'fs';
import Bun from 'bun';
import express from 'express';
import swagger from './util/swagger';
const buffer = fs.readFileSync('./db.mmdb');
const reader = Reader.openBuffer(buffer);
const swaggerApp = express();
swagger(swaggerApp);
// format the success response data
const formatSuccessResponse = (data) => {
return {
status: 'success',
continent: data.continent && data.continent.names ? data.continent.names.en : '',
continentCode: data.continent && data.continent.code ? data.continent.code : '',
country: data.country && data.country.names ? data.country.names.en : '',
countryCode: data.country && data.country.code ? data.country.code : '',
region: data.subdivisions && data.subdivisions[0] ? data.subdivisions[0].isoCode : '',
regionName: data.subdivisions && data.subdivisions[0] && data.subdivisions[0].names ? data.subdivisions[0].names.en : '',
city: data.city && data.city.names ? data.city.names.en : '',
zip:data.postal ? data.postal.code : '',
lat: data.location && data.location.latitude ? data.location.latitude : '',
lon: data.location && data.location.longitude ? data.location.longitude : '',
timezone: data.location && data.location.timeZone ? data.location.timeZone : '',
proxy: data.traits ? (data.traits.isAnonymousProxy || data.traits.isAnonymousVpn || data.traits.isTorExitNode) : '',
hosting: data.traits ? data.traits.isHostingProvider : '',
query: data.traits && data.traits.ipAddress ? data.traits.ipAddress : ''
};
};
// format the error response data
const formatErrorResponse = (error,ip) => {
return {
status:"fail",
message:error.name,
query:ip
}
}
const app = new Router()
.get('/', () => new Response(Bun.file(__dirname + '/www/index.html')))
.get('/city/:ip', (ctx) => {
try {
const resp = reader.city(ctx.params.ip);
return Response.json(formatSuccessResponse(resp));
} catch (error) {
return Response.json(formatErrorResponse(error,ctx.params.ip));
}
})
.post('/city/batch', async (req) => {
try {
const { ips } = await req.json(); // Extract the 'ips' array from the request body
// Create an array of promises, each promise resolves to the city corresponding to the IP address
const promises = ips.map(async (ip) => {
let response;
try {
response = reader.city(ip);
return formatSuccessResponse(response);
} catch (error) {
return formatErrorResponse(error,ip);
}
});
// Wait for all promises to settle and collect the results
const results = await Promise.all(promises);
return Response.json(results, { status: 200 });
} catch (error) {
return new Response('Error processing IP addresses', { status: 500 });
}
});
app.use(404, () => {
return new Response(Bun.file(__dirname + '/www/404.html'))
});
app.port = (process.env.PORT || 3000);
app.hostname = '0.0.0.0';
swaggerApp.listen(3001, () => console.log('Swagger listening on port 3000'))
app.listen();