Skip to content

Commit 7945586

Browse files
committed
dev: ip geolocation driver (initial development)
1 parent 8ff2e93 commit 7945586

4 files changed

Lines changed: 77 additions & 0 deletions

File tree

src/backend/exports.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const { CaptchaModule } = require("./src/modules/captcha/CaptchaModule.js");
4343
const { EntityStoreModule } = require("./src/modules/entitystore/EntityStoreModule.js");
4444
const { AnalyticsModule } = require("./src/modules/analytics/AnalyticsModule.js");
4545
const { KVStoreModule } = require("./src/modules/kvstore/KVStoreModule.js");
46+
const { ExternalExtrasModule } = require("./src/modules/external-extras/ExternalExtrasModule.js");
4647

4748
module.exports = {
4849
helloworld: () => {
@@ -82,6 +83,7 @@ module.exports = {
8283
PuterExecModule,
8384
BroadcastModule,
8485
InternetModule,
86+
ExternalExtrasModule,
8587
MailModule,
8688
ConvertModule,
8789
CaptchaModule,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { AdvancedBase } = require("@heyputer/putility");
2+
const config = require("../../config");
3+
4+
class ExternalExtrasModule extends AdvancedBase {
5+
async install (context) {
6+
const services = context.get('services');
7+
8+
if ( !! config?.services?.['ip-geo'] ) {
9+
const { IPGeoService } = require('./IPGeoService');
10+
services.registerService('ip-geo', IPGeoService);
11+
}
12+
}
13+
}
14+
15+
module.exports = {
16+
ExternalExtrasModule,
17+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const BaseService = require("../../services/BaseService");
2+
3+
class IPGeoService extends BaseService {
4+
async ['__on_driver.register.interfaces'] () {
5+
const svc_registry = this.services.get('registry');
6+
const col_interfaces = svc_registry.get('interfaces');
7+
8+
col_interfaces.set('ip-geo', {
9+
description: 'IP Geolocation',
10+
methods: {
11+
ipgeo: {
12+
description: 'Report geolocation information',
13+
parameters: {
14+
ip: {
15+
type: 'string',
16+
},
17+
},
18+
result: {
19+
type: 'json'
20+
},
21+
},
22+
}
23+
});
24+
}
25+
26+
static IMPLEMENTS = {
27+
['ip-geo']: {
28+
async ipgeo ({ ip }) {
29+
// doing this makes vscode recognize what's being required
30+
const require = this.require;
31+
32+
const axios = require('axios');
33+
const querystring = require('querystring');
34+
35+
const qstr = querystring.stringify({
36+
// Yep, API key reall does go in the query string.
37+
// This is what the docs say to do.
38+
apiKey: this.config.apiKey,
39+
40+
ip,
41+
});
42+
43+
const resp = await axios.request({
44+
method: 'GET',
45+
url: 'https://api.ipgeolocation.io/ipgeo?' + qstr,
46+
});
47+
48+
return resp.data;
49+
}
50+
}
51+
}
52+
}
53+
54+
module.exports = {
55+
IPGeoService,
56+
};

tools/run-selfhosted.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const main = async () => {
8989
PuterAIModule,
9090
PuterExecModule,
9191
InternetModule,
92+
ExternalExtrasModule,
9293
MailModule,
9394
ConvertModule,
9495
DevelopmentModule,
@@ -108,6 +109,7 @@ const main = async () => {
108109
k.add_module(new PuterAIModule());
109110
k.add_module(new PuterExecModule());
110111
k.add_module(new InternetModule());
112+
k.add_module(new ExternalExtrasModule());
111113
k.add_module(new MailModule());
112114
k.add_module(new ConvertModule());
113115
if ( process.env.UNSAFE_PUTER_DEV ) {

0 commit comments

Comments
 (0)