Skip to content

Commit

Permalink
feat: move service controllers to api gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-sa committed Aug 13, 2024
1 parent e5478fa commit 6322a7a
Show file tree
Hide file tree
Showing 29 changed files with 288 additions and 39 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
Should we have a single api gateway that has all the controllers which then calls the Restate services?
4 changes: 1 addition & 3 deletions accounting-service/src/accounting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import { AccountRepository } from './account.repository.js';

@restate.service<AccountingServiceApi>()
export class AccountingService implements AccountingServiceHandlers {
constructor(
private readonly accountRepository: AccountRepository,
) {}
constructor(private readonly accountRepository: AccountRepository) {}

// @ts-ignore
@(restate.kafka<KafkaConsumerTopic>().handler())
Expand Down
2 changes: 2 additions & 0 deletions api-gateway/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Framework
SERVER_PORT=3000
18 changes: 18 additions & 0 deletions api-gateway/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
100 changes: 100 additions & 0 deletions api-gateway/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"name": "api-gateway",
"$schema": "../node_modules/nx/schemas/project-schema.json",
"projectType": "application",
"prefix": "ftgo",
"sourceRoot": "api-gateway/src",
"tags": [],
"targets": {
"build": {
"executor": "@nx/vite:build",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "development",
"options": {
"outputPath": "dist/api-gateway",
"ssr": "src/main.ts",
"outputFileName": "main.mjs"
},
"configurations": {
"development": {
"mode": "development"
},
"production": {
"mode": "production"
}
}
},
"serve": {
"executor": "@nx/js:node",
"defaultConfiguration": "development",
"options": {
"buildTarget": "api-gateway:build",
"watch": false
},
"configurations": {
"development": {
"buildTarget": "api-gateway:build:development",
"args": ["api-gateway:start"],
"watch": true
},
"production": {
"buildTarget": "api-gateway:build:production"
},
"staging": {
"buildTarget": "api-gateway:build:staging"
}
}
},
"container": {
"executor": "@nx-tools/nx-container:build",
"dependsOn": ["build"],
"options": {
"engine": "docker",
"push": true,
"cache-from": ["type=gha"],
"cache-to": ["type=gha,mode=max"],
"metadata": {
"images": ["ghcr.io/zapxme/api-gateway"],
"tags": [
"type=schedule",
"type=ref,event=branch",
"type=ref,event=tag",
"type=ref,event=pr",
"type=sha,prefix=sha-"
]
}
}
},
"create-migration": {
"executor": "@nx/js:node",
"defaultConfiguration": "development",
"options": {
"buildTarget": "api-gateway:build",
"args": [
"migration:create",
"--migrationDir",
"api-gateway/src/migrations"
],
"watch": false
},
"configurations": {
"development": {
"buildTarget": "api-gateway:build:development"
},
"production": {
"buildTarget": "api-gateway:build:production"
},
"staging": {
"buildTarget": "api-gateway:build:staging"
}
}
},
"lint": {
"executor": "@nx/eslint:lint"
},
"test": {
"executor": "@nx/vite:test",
"outputs": ["{projectRoot}/coverage"]
}
}
}
4 changes: 4 additions & 0 deletions api-gateway/src/accounting/accounting.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { http } from '@deepkit/http';

@http.controller('accounting')
export class AccountingController {}
10 changes: 10 additions & 0 deletions api-gateway/src/accounting/accounting.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createModule } from '@deepkit/app';

import { provideAccountingServiceApi } from '@ftgo/accounting-service-api';

import { AccountingController } from './accounting.controller.js';

export class AccountingModule extends createModule({
controllers: [AccountingController],
providers: [provideAccountingServiceApi()],
}) {}
7 changes: 7 additions & 0 deletions api-gateway/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { RestateConfig } from 'deepkit-restate';
import { FrameworkConfig } from '@deepkit/framework';

export class ApiGatewayConfig {
readonly restate: RestateConfig;
readonly server: FrameworkConfig = new FrameworkConfig();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { http } from '@deepkit/http';

@http.controller('')
@http.controller('consumer')
export class ConsumerController {}
10 changes: 10 additions & 0 deletions api-gateway/src/consumer/consumer.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createModule } from '@deepkit/app';

import { provideConsumerServiceApi } from '@ftgo/consumer-service-api';

import { ConsumerController } from './consumer.controller.js';

export class ConsumerModule extends createModule({
controllers: [ConsumerController],
providers: [provideConsumerServiceApi()],
}) {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { http } from '@deepkit/http';

@http.controller('')
@http.controller('delivery')
export class DeliveryController {}
10 changes: 10 additions & 0 deletions api-gateway/src/delivery/delivery.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createModule } from '@deepkit/app';

import { provideDeliveryServiceApi } from '@ftgo/delivery-service-api';

import { DeliveryController } from './delivery.controller.js';

export class DeliveryModule extends createModule({
controllers: [DeliveryController],
providers: [provideDeliveryServiceApi()],
}) {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { http } from '@deepkit/http';

@http.controller('')
@http.controller('kitchen')
export class KitchenController {}
10 changes: 10 additions & 0 deletions api-gateway/src/kitchen/kitchen.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createModule } from '@deepkit/app';

import { provideKitchenServiceApi } from '@ftgo/kitchen-service-api';

import { KitchenController } from './kitchen.controller.js';

export class KitchenModule extends createModule({
controllers: [KitchenController],
providers: [provideKitchenServiceApi()],
}) {}
36 changes: 36 additions & 0 deletions api-gateway/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { App } from '@deepkit/app';
import { FrameworkModule } from '@deepkit/framework';
import { RestateModule } from 'deepkit-restate';

import { ApiGatewayConfig } from './config.js';
import { AccountingModule } from './accounting/accounting.module.js';
import { ConsumerModule } from './consumer/consumer.module.js';
import { DeliveryModule } from './delivery/delivery.module.js';
import { KitchenModule } from './kitchen/kitchen.module.js';
import { OrderModule } from './order/order.module.js';
import { RestaurantModule } from './restaurant/restaurant.module.js';

void new App({
config: ApiGatewayConfig,
imports: [
new FrameworkModule(),
new RestateModule(),
new AccountingModule(),
new ConsumerModule(),
new DeliveryModule(),
new KitchenModule(),
new OrderModule(),
new RestateModule(),
new RestaurantModule(),
],
controllers: [],
})
.setup((module, config: ApiGatewayConfig) => {
module.getImportedModuleByClass(FrameworkModule).configure(config.server);

module.getImportedModuleByClass(RestateModule).configure({
ingress: config.restate.ingress,
});
})
.loadConfigFromEnv({ prefix: '' })
.run();
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { http } from '@deepkit/http';

@http.controller('')
@http.controller('order')
export class OrderController {}
10 changes: 10 additions & 0 deletions api-gateway/src/order/order.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createModule } from '@deepkit/app';

import { provideOrderServiceApi } from '@ftgo/order-service-api';

import { OrderController } from './order.controller.js';

export class OrderModule extends createModule({
controllers: [OrderController],
providers: [provideOrderServiceApi()],
}) {}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ import { RestateClient } from 'deepkit-restate';
import {
CreateRestaurantRequest,
CreateRestaurantResponse,
Restaurant,
RestaurantServiceApi,
} from '@ftgo/restaurant-service-api';

@http.controller('')
@http.controller('restaurant')
export class RestaurantController {
constructor(
private readonly service: RestaurantServiceApi,
private readonly restate: RestateClient,
private readonly client: RestateClient,
) {}

@http.POST('create')
async create(
request: CreateRestaurantRequest,
): Promise<CreateRestaurantResponse> {
const restaurant = await this.restate.rpc(this.service.create(request));
const restaurant = await this.client.rpc(this.service.create(request));
return { id: restaurant.id };
}
}
10 changes: 10 additions & 0 deletions api-gateway/src/restaurant/restaurant.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createModule } from '@deepkit/app';

import { provideRestaurantServiceApi } from '@ftgo/restaurant-service-api';

import { RestaurantController } from './restaurant.controller.js';

export class RestaurantModule extends createModule({
controllers: [RestaurantController],
providers: [provideRestaurantServiceApi()],
}) {}
9 changes: 9 additions & 0 deletions api-gateway/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../dist/out-tsc",
"types": []
},
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.test.ts", "src/**/*.spec.ts"]
}
17 changes: 17 additions & 0 deletions api-gateway/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"strictPropertyInitialization": false,
"types": ["vite/client"]
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.app.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
8 changes: 8 additions & 0 deletions api-gateway/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../dist/out-tsc",
"types": ["vitest/globals"]
},
"include": ["src/**/*.ts"]
}
6 changes: 6 additions & 0 deletions api-gateway/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// eslint-disable-next-line @nx/enforce-module-boundaries
import { defineNodeConfig } from '../node-vite-config';

export default defineNodeConfig({
root: __dirname,
});
2 changes: 1 addition & 1 deletion consumer-service/src/consumer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class ConsumerService implements ConsumerServiceHandlers {

@restate.handler()
async create(name: PersonName): Promise<UUID> {
const consumer = await this.consumer.create({ name }) as Consumer;
const consumer = (await this.consumer.create({ name })) as Consumer;
await this.kafka.produce<KafkaConsumerTopic>([consumer]);
return consumer.id;
}
Expand Down
14 changes: 3 additions & 11 deletions consumer-service/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ import { RestateKafkaProducerModule } from 'deepkit-restate/kafka';
import { RestateModule } from 'deepkit-restate';

import { provideDatabase } from '@ftgo/common';
import {
Account,
provideAccountingServiceApi,
} from '@ftgo/accounting-service-api';
import { Consumer } from '@ftgo/consumer-service-api';

import { ConsumerServiceConfig } from './config.js';
import { ConsumerController } from './consumer.controller.js';
import { ConsumerService } from './consumer.service.js';
import { ConsumerRepository } from './consumer.repository.js';

Expand All @@ -25,12 +21,8 @@ void new App({
brokers: [''],
}),
],
controllers: [ConsumerController, ConsumerService],
providers: [
provideDatabase([Account]),
provideAccountingServiceApi(),
ConsumerRepository,
],
controllers: [ConsumerService],
providers: [provideDatabase([Consumer]), ConsumerRepository],
})
.setup((module, config: ConsumerServiceConfig) => {
module
Expand Down
Loading

0 comments on commit 6322a7a

Please sign in to comment.