#Miroservice Custom Transporter

14 messages · Page 1 of 1 (latest)

signal ingot
#
Hi everyone, I'm currently implementing a custom transporter by extending CustomTransportStrategy.
In my bindHandlers function, I'm successfully extracting message and event patterns 
along with their relevant callbacks. However, I'm having trouble retrieving the 
controller route (e.g., /controller-route-ABC).

I've tried using reflector.get<string[]>('path', handler); to get the controller path,
but I'm getting undefined. Is there a recommended way to extract the controller route 
from the handler? Any insights would be greatly appreciated!
#
export class ABC extends Server implements CustomTransportStrategy {

  public bindHandlers() {
    this.messageHandlers.forEach((handler, pattern) => {
 // tried this , but not working
      const controllerPath = reflector.get<string[]>('path', handler);
      console.log('Controller Path:', controllerPath); // returns undefined


      // In this version (`part3`) we add the handler for events
      if (handler.isEventHandler) {

        this.fayeClient.subscribe(pattern, async (rawPacket: ReadPacket) => {
          const fayeCtx = new FayeContext([pattern]);
          const packet = this.parsePacket(rawPacket);
          const message = this.deserializer.deserialize(packet, {
            channel: pattern,
          });
          await handler(message.data, fayeCtx);
        });
      } else {
        this.fayeClient.subscribe(
          `${pattern}_ack`,
          this.getMessageHandler(pattern, handler),
        );
      }
    });
  }

}
#
// some microservice which use ABC CustomTransportStrategy
@Controller('/controller-route-ABC')
export class AppController {
  logger = new Logger('AppController');

  constructor(private readonly workService: WorkService) { }

  @MessagePattern('/get-customers')
  async getCustomers(data: any, @Ctx() context: FayeContext): Promise<any> {
    this.logger.log(`Faye Context: ${JSON.stringify(context)}`);
    // return { customers };
  }

  @EventPattern('/add-customer')
  addCustomer(customer: Customer) {
    customerList.push({
      id: lastId + 1,
      name: customer.name,
    });
    lastId++;
    this.logger.log(`Customer list:\n${JSON.stringify(customerList, null, 2)}`);
  }

  @MessagePattern('/jobs-promise')
  doPromiseWork(duration): Promise<any> {
    return this.workService.doThreeSteps(duration);
  }
  @MessagePattern('/jobs-observable')
  doObservableWork(duration): Observable<any> {
    return from(this.workService.doThreeSteps(duration));
  }

  @MessagePattern('/jobs-stream1')
  doStream1(duration): Observable<any> {
    return new Observable(observer => {
      const jobs = [1, 2, 3].map(job => this.workService.doStep(job, duration));
      Promise.mapSeries(jobs, jobResult => {
        // promise has resolved (job has completed)
        observer.next(jobResult);
      }).then(() => observer.complete());
    });
  }
}
signal ingot
#

@slate ravine can u please take a look at this one

slate ravine
#

To be honest, this is the first time I'm seeing someone attempt using a controller prefix on a microservice

#

I don't think this is a supported feature

#

The reflector code you shown extracts the metadata from the handler method.

#

You'd need to read the metadata off of the class, not the method

#

But I'm not sure if it's possible to retrieve the class based on the handler. My guess it not

signal ingot
signal ingot
signal ingot
heady lintel
signal ingot