Skip to content

Commit d62aa60

Browse files
committed
feat: add getInternalApiOrigin method to ExpressHttpServer interface for internal API calls
1 parent 03dbdc4 commit d62aa60

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

adminforth/servers/express.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { WebSocketServer } from 'ws';
1010
import { WebSocketClient } from './common.js';
1111
import { AdminUser } from '../types/Common.js';
1212
import http from 'http';
13+
import type { AddressInfo } from 'net';
1314
import { randomUUID } from 'crypto';
1415
import { listify } from '../modules/utils.js';
1516
import { afLogger } from '../modules/logger.js';
@@ -285,6 +286,16 @@ class ExpressServer implements IExpressHttpServer {
285286
this.server.listen(...args);
286287
}
287288

289+
getInternalApiOrigin(): string | undefined {
290+
const address = this.server?.address();
291+
292+
if (!address || typeof address === 'string') {
293+
return undefined;
294+
}
295+
296+
return `http://127.0.0.1:${(address as AddressInfo).port}`;
297+
}
298+
288299
async processAuthorizeCallbacks(adminUser: AdminUser, toReturn: { error?: string, allowed: boolean }, response: Response, extra: HttpExtra) {
289300
const adminUserAuthorize = this.adminforth.config.auth.adminUserAuthorize as (AdminUserAuthorizeFunction[] | undefined);
290301

adminforth/types/Back.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ export interface IExpressHttpServer extends IHttpServer {
153153
listen(port: number, callback: Function): void;
154154
listen(port: number, host: string, callback: Function): void;
155155

156+
/**
157+
* Returns an internal HTTP origin for same-process/server API calls.
158+
*/
159+
getInternalApiOrigin(): string | undefined;
160+
156161
/**
157162
* Method (middleware) to wrap express endpoints with authorization check.
158163
* Adds adminUser to request object if user is authorized. Drops request with 401 status if user is not authorized.
@@ -2328,4 +2333,4 @@ export interface PluginsCommonOptions {
23282333
* It is required if you want to log custom actions, otherwise it is optional.
23292334
*/
23302335
id?: string;
2331-
}
2336+
}

0 commit comments

Comments
 (0)