mirror of
https://github.com/PreMiD/PreMiD.git
synced 2026-04-05 20:31:58 +02:00
feat: ip data
This commit is contained in:
4
apps/api-master/environment.d.ts
vendored
4
apps/api-master/environment.d.ts
vendored
@@ -5,8 +5,8 @@ declare module "ip-location-api" {
|
||||
country: string;
|
||||
} | null>;
|
||||
|
||||
export function updateDb(options: { fields?: string[]; dataDir?: string; tmpDataDir?: string; smallMemory?: boolean }): Promise<void>;
|
||||
export function reload(options: { fields?: string[]; dataDir?: string; tmpDataDir?: string; smallMemory?: boolean }): Promise<void>;
|
||||
export function updateDb(options: { fields?: string[]; dataDir?: string; tmpDataDir?: string; smallMemory?: boolean; autoUpdate?: string }): Promise<void>;
|
||||
export function reload(options: { fields?: string[]; dataDir?: string; tmpDataDir?: string; smallMemory?: boolean; autoUpdate?: string }): Promise<void>;
|
||||
}
|
||||
|
||||
declare namespace NodeJS {
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
"debug": "^4.3.6",
|
||||
"drizzle-orm": "^0.33.0",
|
||||
"ioredis": "^5.3.2",
|
||||
"ip-location-api": "^1.0.0",
|
||||
"ip-location-api": "^2.0.1",
|
||||
"ky": "^1.7.2",
|
||||
"p-limit": "^6.1.0",
|
||||
"postgres": "^3.4.4",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { join } from "node:path";
|
||||
import process from "node:process";
|
||||
import { lookup, reload, updateDb } from "ip-location-api";
|
||||
import { lookup, reload } from "ip-location-api";
|
||||
import { mainLog } from "../index.js";
|
||||
|
||||
const fields = ["latitude", "longitude", "country"];
|
||||
@@ -24,7 +24,7 @@ export async function lookupIp(ip: string): Promise<{ latitude: number; longitud
|
||||
}
|
||||
}
|
||||
|
||||
let reloading: Promise<void> | undefined = Promise.resolve();
|
||||
let reloading: Promise<void> | undefined;
|
||||
let log: debug.Debugger | undefined;
|
||||
|
||||
export async function reloadIpLocationApi() {
|
||||
@@ -35,8 +35,7 @@ export async function reloadIpLocationApi() {
|
||||
|
||||
reloading = new Promise((resolve, reject) => {
|
||||
log?.("Reloading IP location API");
|
||||
updateDb({ fields, dataDir, tmpDataDir, smallMemory }).then(async () => {
|
||||
await reload({ fields, dataDir, tmpDataDir, smallMemory });
|
||||
reload({ fields, dataDir, tmpDataDir, smallMemory, autoUpdate: "0 9 * * *" }).then(() => {
|
||||
log?.("IP location API reloaded");
|
||||
initialized = true;
|
||||
reloading = undefined;
|
||||
|
||||
@@ -4,6 +4,14 @@ import { register } from "../tracing.js";
|
||||
|
||||
export function setupServer() {
|
||||
const server = http.createServer(async (req, res) => {
|
||||
//* If it's a head request, just return 200
|
||||
if (req.method === "HEAD")
|
||||
return res.writeHead(200).end();
|
||||
|
||||
//* If it's a favicon request, just return 404
|
||||
if (req.url === "/favicon.ico")
|
||||
return res.writeHead(404).end();
|
||||
|
||||
//* Basic routing logic
|
||||
res.writeHead(200, { "Content-Type": "text/plain" });
|
||||
res.end(await register.metrics());
|
||||
|
||||
@@ -4,7 +4,7 @@ import debug from "debug";
|
||||
import { clearOldSessions } from "./functions/clearOldSessions.js";
|
||||
import createRedis from "./functions/createRedis.js";
|
||||
import "./tracing.js";
|
||||
// import { reloadIpLocationApi } from "./functions/lookupIp.js";
|
||||
import { reloadIpLocationApi } from "./functions/lookupIp.js";
|
||||
import { cleanupOldUserData } from "./functions/cleanupOldUserData.js";
|
||||
import { setupServer } from "./functions/setupServer.js";
|
||||
|
||||
@@ -16,6 +16,8 @@ export const mainLog = debug("api-master");
|
||||
|
||||
debug("Starting cron jobs");
|
||||
|
||||
void reloadIpLocationApi();
|
||||
|
||||
void new CronJob(
|
||||
// Every 5 seconds
|
||||
"*/5 * * * * *",
|
||||
@@ -28,19 +30,6 @@ void new CronJob(
|
||||
true,
|
||||
);
|
||||
|
||||
// void new CronJob(
|
||||
// // Every day at 9am
|
||||
// "0 9 * * *",
|
||||
// () => {
|
||||
// reloadIpLocationApi();
|
||||
// },
|
||||
// undefined,
|
||||
// true,
|
||||
// undefined,
|
||||
// undefined,
|
||||
// true,
|
||||
// );
|
||||
|
||||
void new CronJob(
|
||||
// Every day at 1am
|
||||
"0 1 * * *",
|
||||
|
||||
@@ -38,10 +38,5 @@
|
||||
"prettier": "^3.2.5",
|
||||
"typescript": "^5.5.4",
|
||||
"vitest": "^2.0.2"
|
||||
},
|
||||
"pnpm": {
|
||||
"patchedDependencies": {
|
||||
"ip-location-api@1.0.0": "patches/ip-location-api@1.0.0.patch"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
diff --git a/browser/country/README.md b/browser/country/README.md
|
||||
deleted file mode 100644
|
||||
index ac8fc934b4998f2a2cb7a92bf68bbdadd9e3d36d..0000000000000000000000000000000000000000
|
||||
diff --git a/browser/country-extra/README.md b/browser/country-extra/README.md
|
||||
deleted file mode 100644
|
||||
index 71e7237722915b2697b56ccb14171524eb4b40fb..0000000000000000000000000000000000000000
|
||||
diff --git a/browser/geocode/README.md b/browser/geocode/README.md
|
||||
deleted file mode 100644
|
||||
index 9d9a2205061f332b363b82d7561a0e3829d5bf2c..0000000000000000000000000000000000000000
|
||||
diff --git a/browser/geocode-extra/README.md b/browser/geocode-extra/README.md
|
||||
deleted file mode 100644
|
||||
index 38e17eebdd8532d07b460fbe7f385f36625ece9d..0000000000000000000000000000000000000000
|
||||
diff --git a/src/db.mjs b/src/db.mjs
|
||||
index 378b8a22084f860cc89720d1783a235c034717b2..cbffe1eaa84a94df536059d4b4af3f8f5ceb0ca7 100644
|
||||
--- a/src/db.mjs
|
||||
+++ b/src/db.mjs
|
||||
@@ -33,7 +33,12 @@ export const update = async () => {
|
||||
if(refreshTmpDir || !fsSync.existsSync(setting.tmpDataDir)){
|
||||
// refresh tmp folder
|
||||
await rimraf(setting.tmpDataDir)
|
||||
- await fs.mkdir(setting.tmpDataDir)
|
||||
+ await fs.mkdir(setting.tmpDataDir, {recursive: true})
|
||||
+ }
|
||||
+
|
||||
+ // When specifying a custom dataDir, it doesn't always exists
|
||||
+ if (!fsSync.existsSync(setting.dataDir)){
|
||||
+ await fs.mkdir(setting.dataDir, {recursive: true})
|
||||
}
|
||||
|
||||
console.log('Downloading database')
|
||||
diff --git a/src/main.mjs b/src/main.mjs
|
||||
index d001aca60902bc7fe41271c6fa7a0b6648607b15..5b2c125d8e7590afee82c794bf771accd656b2b7 100644
|
||||
--- a/src/main.mjs
|
||||
+++ b/src/main.mjs
|
||||
@@ -3,7 +3,7 @@ import fs from 'fs/promises'
|
||||
import fsSync from 'fs'
|
||||
import path from 'path'
|
||||
import { exec } from 'child_process'
|
||||
-
|
||||
+import { fileURLToPath } from "url"
|
||||
import { countries, continents } from 'countries-list'
|
||||
|
||||
import { setting, setSetting, getSettingCmd } from './setting.mjs'
|
||||
@@ -14,6 +14,9 @@ const v6db = setting.v6
|
||||
const locFieldHash = setting.locFieldHash
|
||||
const mainFieldHash = setting.mainFieldHash
|
||||
|
||||
+const __filename = fileURLToPath(import.meta.url)
|
||||
+const __dirname = path.dirname(__filename)
|
||||
+
|
||||
//---------------------------------------
|
||||
// Database lookup
|
||||
//---------------------------------------
|
||||
@@ -235,7 +238,7 @@ export const updateDb = (_setting) => {
|
||||
// However, db.js import many external modules, it makes slow down the startup time and uses more memory.
|
||||
// Therefore, we use exec() to run the script in the other process.
|
||||
return new Promise((resolve, reject) => {
|
||||
- var cmd = 'node ' + path.resolve(__dirname, '..', 'script', 'updatedb.js')
|
||||
+ var cmd = 'node ' + path.resolve(__dirname, '..', 'script', 'updatedb.mjs')
|
||||
var arg
|
||||
if(_setting){
|
||||
var oldSetting = Object.assign({}, setting)
|
||||
600
pnpm-lock.yaml
generated
600
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user