Skip to content

Commit

Permalink
Merge branch 'backend' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcossIC authored Nov 24, 2024
2 parents 806ebce + 93d5589 commit 61c31c6
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 6 deletions.
3 changes: 2 additions & 1 deletion server/apps/gateway/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Module } from '@nestjs/common';
import { AuthModule } from './auth/auth.module';
import { UploadModule } from './upload/upload.module';
import { CoursesModule } from './courses/courses.module';
import { PubSubModule } from './pubsub/pubsub.module';
@Module({
imports: [AuthModule, UploadModule, CoursesModule],
imports: [AuthModule, UploadModule, CoursesModule,PubSubModule],
})
export class AppModule {}
14 changes: 13 additions & 1 deletion server/apps/gateway/src/proto/upload.proto
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
syntax = "proto3";

package googlecloudstorage;
package googlecloudstorage; // Este será el paquete común para ambos servicios

// Servicio PubSub
service PubSubService {
rpc Test (TestRequest) returns (TestResponse);
}

message TestRequest {}

message TestResponse {
string message = 1;
}

// Servicio GoogleCloudStorage
service GoogleCloudStorageService {
rpc UploadFile (UploadFileRequest) returns (UploadFileResponse);
}
Expand Down
17 changes: 17 additions & 0 deletions server/apps/gateway/src/pubsub/pubsub.gateway.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Controller, Get, Param } from '@nestjs/common';
import { PubSubGatewayService } from './pubsub.gateway.service';

@Controller('pubsub')
export class PubSubGatewayController {
constructor(private readonly pubSubGatewayService: PubSubGatewayService) {}

@Get('test')
async test() {
return this.pubSubGatewayService.test();
}

@Get('listen/:subscriptionName')
async listenForMessages(@Param('subscriptionName') subscriptionName: string) {
return this.pubSubGatewayService.listenForMessages(subscriptionName);
}
}
30 changes: 30 additions & 0 deletions server/apps/gateway/src/pubsub/pubsub.gateway.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Injectable } from '@nestjs/common';
import { Inject } from '@nestjs/common';
import { ClientGrpc } from '@nestjs/microservices';
import { Observable } from 'rxjs';

interface PubSubServiceClient {
test(request: {}): Observable<{ message: string }>;
listenForMessages(request: { subscriptionName: string }): Observable<{ status: string, message: string }>;
}

@Injectable()
export class PubSubGatewayService {
private pubSubService: PubSubServiceClient;

constructor(@Inject('PUBSUB_SERVICE') private readonly client: ClientGrpc) {}

onModuleInit() {
this.pubSubService = this.client.getService<PubSubServiceClient>('PubSubService');
}
t
async test() {
const result = await this.pubSubService.test({}).toPromise();
return result.message;
}

async listenForMessages(subscriptionName: string) {
const result = await this.pubSubService.listenForMessages({ subscriptionName }).toPromise();
return result;
}
}
23 changes: 23 additions & 0 deletions server/apps/gateway/src/pubsub/pubsub.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Module } from '@nestjs/common';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { PubSubGatewayController } from './pubsub.gateway.controller';
import { PubSubGatewayService } from './pubsub.gateway.service';

@Module({
imports: [
ClientsModule.register([
{
name: 'PUBSUB_SERVICE',
transport: Transport.GRPC,
options: {
package: 'googlecloudstorage',
protoPath: 'src/proto/upload.proto',
url: '0.0.0.0:50051',
},
},
]),
],
controllers: [PubSubGatewayController],
providers: [PubSubGatewayService],
})
export class PubSubModule {}
140 changes: 140 additions & 0 deletions server/apps/uploadGCould/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/apps/uploadGCould/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@google-cloud/pubsub": "4.9.0",
"@google-cloud/storage": "7.14.0",
"@grpc/grpc-js": "1.8.17",
"@grpc/proto-loader": "0.7.7",
Expand Down
14 changes: 13 additions & 1 deletion server/apps/uploadGCould/protos/upload.proto
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
syntax = "proto3";

package googlecloudstorage;
package googlecloudstorage; // Este será el paquete común para ambos servicios

// Servicio PubSub
service PubSubService {
rpc Test (TestRequest) returns (TestResponse);
}

message TestRequest {}

message TestResponse {
string message = 1;
}

// Servicio GoogleCloudStorage
service GoogleCloudStorageService {
rpc UploadFile (UploadFileRequest) returns (UploadFileResponse);
}
Expand Down
7 changes: 4 additions & 3 deletions server/apps/uploadGCould/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { GoogleCloudStorageService } from './storage.service';
import { StorageController } from './storage.controller';

import { PubSubController } from './pubsub.controller';
import { PubSubService } from './pubsub.service';
@Module({
imports: [ConfigModule.forRoot()],
controllers: [StorageController],
providers: [GoogleCloudStorageService],
controllers: [StorageController,PubSubController],
providers: [GoogleCloudStorageService,PubSubService],
})
export class GoogleCloudStorageModule {}
23 changes: 23 additions & 0 deletions server/apps/uploadGCould/src/pubsub.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Controller } from '@nestjs/common';
import { GrpcMethod } from '@nestjs/microservices';
import { PubSubService } from './pubsub.service';

@Controller()
export class PubSubController {
constructor(private readonly pubSubService: PubSubService) {}

@GrpcMethod('PubSubService', 'Test')
test(): { message: string } {
return { message: 'PubSub service is active!' };
}

@GrpcMethod('PubSubService', 'ListenForMessages')
async listenForMessages(data: { subscriptionName: string }): Promise<{ status: string, message: string }> {
try {
const result = await this.pubSubService.listenForMessages(data.subscriptionName);
return { status: 'success', message: result as string };
} catch (error) {
return { status: 'error', message: error.message };
}
}
}
Loading

0 comments on commit 61c31c6

Please sign in to comment.