Skip to content

Commit

Permalink
[Feat]: Add python gRPC (#41)
Browse files Browse the repository at this point in the history
* add grpc python server

* add api server grpc
  • Loading branch information
Istiopaxx authored May 23, 2023
1 parent e4a145e commit 32834ea
Show file tree
Hide file tree
Showing 20 changed files with 620 additions and 65 deletions.
6 changes: 6 additions & 0 deletions api/apps/api-bundled/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { AuthModule } from '@app/auth/auth.module';
import { ImageModule } from '@app/image/image.module';
import { IngredientModule } from '@app/ingredient/ingredient.module';
import { RecipeModule } from '@app/recipe/recipe.module';
import { AopModule } from '@app/common/aop/aop.module';
import { CacheModule } from '@app/common/cache/cache.module';
import { LogModule } from '@app/common/log/log.module';

@Module({
imports: [
Expand All @@ -20,6 +23,9 @@ import { RecipeModule } from '@app/recipe/recipe.module';
isGlobal: true,
envFilePath: process.env.NODE_ENV === 'test' ? '.env.test.' : '.env.dev',
}),
AopModule,
CacheModule,
LogModule,
UserModule,
AuthModule,
ImageModule,
Expand Down
33 changes: 33 additions & 0 deletions api/apps/api-bundled/src/image-process.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
syntax = "proto3";

package image_process;

service ImageProcess {
rpc GetBarcodeInfoFromUrl(ImageInfo) returns (BarcodeInfos) {}
}

message ImageInfo {
string image_url = 1;
}

message BarcodeInfos {
message BarcodeInfo {
message Rect {
int32 left = 1;
int32 top = 2;
int32 width = 3;
int32 height = 4;
}
message Point {
int32 x = 1;
int32 y = 2;
}
string data = 1;
string type = 2;
Rect rect = 3;
repeated Point polygon = 4;
string orientation = 5;
int32 quality = 6;
}
repeated BarcodeInfo barcode_infos = 1;
}
6 changes: 6 additions & 0 deletions api/apps/api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { AuthModule } from '@app/auth/auth.module';
import { ImageModule } from '@app/image/image.module';
import { IngredientModule } from '@app/ingredient/ingredient.module';
import { RecipeModule } from '@app/recipe/recipe.module';
import { AopModule } from '@app/common/aop/aop.module';
import { CacheModule } from '@app/common/cache/cache.module';
import { LogModule } from '@app/common/log/log.module';

@Module({
imports: [
Expand All @@ -20,6 +23,9 @@ import { RecipeModule } from '@app/recipe/recipe.module';
isGlobal: true,
envFilePath: process.env.NODE_ENV === 'test' ? '.env.test.' : '.env.dev',
}),
AopModule,
CacheModule,
LogModule,
UserModule,
AuthModule,
ImageModule,
Expand Down
33 changes: 33 additions & 0 deletions api/apps/api/src/image-process.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
syntax = "proto3";

package image_process;

service ImageProcess {
rpc GetBarcodeInfoFromUrl(ImageInfo) returns (BarcodeInfos) {}
}

message ImageInfo {
string image_url = 1;
}

message BarcodeInfos {
message BarcodeInfo {
message Rect {
int32 left = 1;
int32 top = 2;
int32 width = 3;
int32 height = 4;
}
message Point {
int32 x = 1;
int32 y = 2;
}
string data = 1;
string type = 2;
Rect rect = 3;
repeated Point polygon = 4;
string orientation = 5;
int32 quality = 6;
}
repeated BarcodeInfo barcode_infos = 1;
}
20 changes: 20 additions & 0 deletions api/libs/ingredient/src/ingredient.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
import { UserIngredientRepository } from './repositories/user-ingredient.repository';
import { UserIngredientService } from './services/user-ingredient.service';
import { ConfigService } from '@nestjs/config';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { join } from 'path';

@Module({
imports: [
Expand All @@ -18,6 +20,24 @@ import { ConfigService } from '@nestjs/config';
inject: [ConfigService],
},
]),
ClientsModule.registerAsync([
{
name: 'IMAGE_PROCESS_SERVICE',
useFactory: () => {
return {
transport: Transport.GRPC,
options: {
package: 'image_process',
url: 'localhost:50051',
protoPath: join(__dirname, '../../../image-process.proto'),
loader: {
keepCase: true,
},
},
};
},
},
]),
],
controllers: [UserIngredientController],
providers: [UserIngredientService, UserIngredientRepository],
Expand Down
31 changes: 29 additions & 2 deletions api/libs/ingredient/src/services/user-ingredient.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Logable } from '@app/common/log/log.decorator';
import { Injectable, NotFoundException } from '@nestjs/common';
import {
Inject,
Injectable,
NotFoundException,
OnModuleInit,
} from '@nestjs/common';
import { ClientGrpc } from '@nestjs/microservices';
import { firstValueFrom, lastValueFrom, map, Observable } from 'rxjs';
import { FilterUserIngredientDto } from '../dto/filter-ingredient.dto';
import {
CreateUserIngredientDto,
Expand All @@ -8,13 +15,33 @@ import {
import { UserIngredient } from '../entities/user-ingredient.entity';
import { UserIngredientRepository } from '../repositories/user-ingredient.repository';

interface ImageProcessService {
getBarcodeInfoFromUrl(imageInfo: { image_url: string }): Observable<any>;
}

@Logable()
@Injectable()
export class UserIngredientService {
export class UserIngredientService implements OnModuleInit {
private imageProcessService: ImageProcessService;
constructor(
private readonly userIngredientRepository: UserIngredientRepository,
@Inject('IMAGE_PROCESS_SERVICE') private readonly client: ClientGrpc,
) {}

onModuleInit() {
this.imageProcessService =
this.client.getService<ImageProcessService>('ImageProcess');
}

getIngredientInfoFromImage(imageUrl: string) {
console.log('image_url: ', imageUrl);
const ret = this.imageProcessService.getBarcodeInfoFromUrl({
image_url: imageUrl,
});
console.log('ret: ', ret);
return ret;
}

async create(
createUserIngredientDto: CreateUserIngredientDto,
): Promise<UserIngredient> {
Expand Down
4 changes: 4 additions & 0 deletions api/nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"entryFile": "main",
"sourceRoot": "apps/api/src",
"compilerOptions": {
"assets": ["**/*.proto"],
"watchAssets": true,
"plugins": [
{
"name": "@nestjs/swagger",
Expand All @@ -29,6 +31,8 @@
"entryFile": "main",
"sourceRoot": "apps/api-bundled/src",
"compilerOptions": {
"assets": ["**/*.proto"],
"watchAssets": true,
"tsConfigPath": "apps/api-bundled/tsconfig.app.json",
"webpack": true
}
Expand Down
Loading

0 comments on commit 32834ea

Please sign in to comment.