11import { v4 as uuidv4 } from 'uuid' ;
2+ import * as os from 'os' ;
23import { MeshConfig , Node , Message } from './shared/types' ;
34import { RedisServiceDiscovery } from './discovery/redis-discovery' ;
45import { H2Communication } from './communication/h2-communication' ;
@@ -28,6 +29,12 @@ export class Mesh implements IMesh {
2829
2930 constructor ( config : MeshConfig ) {
3031 this . config = config ;
32+
33+ if ( ! this . config . host ) {
34+ this . config . host = this . getLocalIP ( ) ;
35+ console . log ( `[Mesh] Auto-detected host IP: ${ this . config . host } ` ) ;
36+ }
37+
3138 this . discovery = new RedisServiceDiscovery ( config . redis , config . service_discovery ) ;
3239 this . communication = new H2Communication ( config ) ;
3340 this . pubsub = new RedisPubSub ( config . redis ) ;
@@ -51,7 +58,7 @@ export class Mesh implements IMesh {
5158 // Register with SD
5259 const node : Node = {
5360 id : this . nodeId ,
54- host : this . config . host ,
61+ host : this . config . host ! , // Detected in constructor
5562 port : this . config . port ,
5663 service_name : serviceName
5764 } ;
@@ -60,6 +67,20 @@ export class Mesh implements IMesh {
6067 console . log ( `[Mesh] Registered service '${ serviceName } ' on ${ node . host } :${ node . port } ` ) ;
6168 }
6269
70+ private getLocalIP ( ) : string {
71+ const interfaces = os . networkInterfaces ( ) ;
72+ for ( const name of Object . keys ( interfaces ) ) {
73+ for ( const iface of interfaces [ name ] ! ) {
74+ // Skip internal and non-IPv4 addresses
75+ if ( 'IPv4' !== iface . family || iface . internal ) {
76+ continue ;
77+ }
78+ return iface . address ;
79+ }
80+ }
81+ return '127.0.0.1' ; // Fallback
82+ }
83+
6384 public service ( name : string ) : IServiceClient {
6485 return new ServiceClient ( name , this . discovery , this . communication ) ;
6586 }
0 commit comments