fix: fix library path load problem

This commit is contained in:
moonrailgun 2024-01-29 02:17:10 +08:00
parent f5f0a560f1
commit c049a62493
3 changed files with 10 additions and 8 deletions

View File

@ -2,13 +2,11 @@ import { Router } from 'express';
import { body, header, param, validate } from '../middleware/validate'; import { body, header, param, validate } from '../middleware/validate';
import { recordServerStatus } from '../model/serverStatus'; import { recordServerStatus } from '../model/serverStatus';
import fs from 'fs-extra'; import fs from 'fs-extra';
import path from 'path'; import { libraryPath } from '../utils/lib';
export const serverStatusRouter = Router(); export const serverStatusRouter = Router();
const installScript = fs.readFileSync( const installScript = fs.readFileSync(libraryPath.installScript);
path.resolve(process.cwd(), '../../scripts/install.sh')
);
serverStatusRouter.post( serverStatusRouter.post(
'/report', '/report',

View File

@ -11,8 +11,8 @@ import {
MOBILE_OS, MOBILE_OS,
MOBILE_SCREEN_WIDTH, MOBILE_SCREEN_WIDTH,
} from './const'; } from './const';
import path from 'path';
import maxmind, { Reader, CityResponse } from 'maxmind'; import maxmind, { Reader, CityResponse } from 'maxmind';
import { libraryPath } from './lib';
let lookup: Reader<CityResponse>; let lookup: Reader<CityResponse>;
@ -104,9 +104,7 @@ export async function getLocation(ip: string, req: Request) {
// Database lookup // Database lookup
if (!lookup) { if (!lookup) {
const dir = path.join(process.cwd(), 'geo'); lookup = await maxmind.open(libraryPath.geoPath);
lookup = await maxmind.open(path.resolve(dir, 'GeoLite2-City.mmdb'));
} }
const result = lookup.get(ip); const result = lookup.get(ip);

6
src/server/utils/lib.ts Normal file
View File

@ -0,0 +1,6 @@
import path from 'path';
export const libraryPath = {
installScript: path.resolve(process.cwd(), '../../scripts/install.sh'),
geoPath: path.resolve(process.cwd(), '../../geo/GeoLite2-City.mmdb'),
};