Skip to content

Commit

Permalink
create example for fastify
Browse files Browse the repository at this point in the history
  • Loading branch information
zgid123 committed Apr 5, 2023
1 parent eee786e commit d0b7b58
Show file tree
Hide file tree
Showing 12 changed files with 995 additions and 49 deletions.
1 change: 1 addition & 0 deletions examples/fastify/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Example for Fastify
24 changes: 24 additions & 0 deletions examples/fastify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "fastify",
"version": "1.0.0",
"description": "Example for Fastify",
"type": "module",
"scripts": {
"dev": "vite-node -w src/main.ts",
"build": "vite build"
},
"dependencies": {
"@fastify/autoload": "^5.7.1",
"@grpc-ts/fastify-client": "workspace:*",
"@grpc-ts/fastify-server": "workspace:^1.0.0",
"detect-port": "^1.5.1",
"fastify": "^4.15.0",
"vite": "^4.2.1"
},
"devDependencies": {
"@fastify/vite": "^4.0.0",
"@types/detect-port": "^1.3.2",
"fastify-cli": "^5.7.1",
"vite-node": "^0.29.8"
}
}
5 changes: 5 additions & 0 deletions examples/fastify/src/config/fastify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Fastify from 'fastify';

export const fastify = Fastify({
logger: process.env.NODE_ENV === 'production' ? false : true,
});
93 changes: 93 additions & 0 deletions examples/fastify/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import detect from 'detect-port';
import FastifyGrpcClient, { createMetadata } from '@grpc-ts/fastify-client';
import FastifyGrpcServer, {
dateToGrpcTimestamp,
} from '@grpc-ts/fastify-server';

import { fastify } from 'config/fastify';

async function bootstrap(): Promise<typeof fastify> {
fastify.register(FastifyGrpcServer, {
url: 'localhost:3010',
package: [
{
packageName: 'example.v1',
protoPath: '../proto/example.proto',
},
{
packageName: 'example2.v1',
protoPath: '../proto/example2.proto',
},
{
protoPath: '../proto/example3.proto',
},
],
packageDefinitionOptions: {
oneofs: true,
longs: String,
enums: String,
defaults: true,
},
options: {
keepaliveTimeMs: 5_000,
},
});

fastify.register(FastifyGrpcClient, {
url: 'localhost:3010',
package: [
{
packageName: 'example.v1',
protoPath: '../proto/example.proto',
},
{
packageName: 'example2.v1',
protoPath: '../proto/example2.proto',
},
{
protoPath: '../proto/example3.proto',
},
],
packageDefinitionOptions: {
oneofs: true,
longs: String,
enums: String,
defaults: true,
},
options: {
keepaliveTimeMs: 5_000,
},
});

const port = await detect(3_000);
await fastify.listen({
port,
});

fastify.grpcServer
.getServer()
.addUnaryHandler('ExampleService', 'sendMessage', (request, metadata) => {
console.log(request);
console.log(metadata);
return {
message: {
message: 'hola',
createdAt: dateToGrpcTimestamp(new Date()),
},
};
});

const result = await fastify.grpcClient
.getService('ExampleService')
.sendMessage(
{ message: 'hello', createdAt: dateToGrpcTimestamp(new Date()) },
createMetadata({
meta: 'test',
}),
);
console.log(result);

return fastify;
}

bootstrap();
25 changes: 25 additions & 0 deletions examples/fastify/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"baseUrl": "src",
"declaration": true,
"emitDecoratorMetadata": true,
"incremental": true,
"module": "esnext",
"noEmit": false,
"noFallthroughCasesInSwitch": false,
"noImplicitAny": false,
"outDir": "dist",
"sourceMap": true,
"strictBindCallApply": false,
"strictNullChecks": false
},
"include": [
"src"
],
"exclude": [
"dist",
"node_modules"
]
}
45 changes: 45 additions & 0 deletions examples/fastify/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { resolve } from 'path';
import { readdirSync } from 'fs';
import detect from 'detect-port';
import { defineConfig, type UserConfigExport } from 'vite';

export default async function config(): Promise<UserConfigExport> {
const port = await detect(3_000);

const items = readdirSync(resolve(__dirname, 'src'));

return defineConfig({
server: {
port,
watch: {
usePolling: true,
},
},
resolve: {
alias: items.map((item) => {
if (/\.(t|j)sx?$/.test(item)) {
const name = item.replace(/\.(t|j)sx?$/, '');

return {
find: name,
replacement: `/src/${name}`,
};
} else {
return {
find: item,
replacement: `/src/${item}`,
};
}
}),
},
build: {
ssr: true,
ssrEmitAssets: true,
rollupOptions: {
input: {
main: './src/main.ts',
},
},
},
});
}
21 changes: 21 additions & 0 deletions examples/proto/example.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";

import "google/protobuf/timestamp.proto";

package example.v1;

message Message {
string message = 1;
google.protobuf.Timestamp created_at = 2;
}

message SendMessageRequest {
string message = 1;
google.protobuf.Timestamp created_at = 2;
}

message GetMessageResponse { Message message = 1; }

service ExampleService {
rpc SendMessage(SendMessageRequest) returns (GetMessageResponse);
}
21 changes: 21 additions & 0 deletions examples/proto/example2.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";

import "google/protobuf/timestamp.proto";

package example2.v1;

message Message {
string message = 1;
google.protobuf.Timestamp created_at = 2;
}

message SendMessageRequest {
string message = 1;
google.protobuf.Timestamp created_at = 2;
}

message GetMessageResponse { Message message = 1; }

service ExampleService {
rpc SendMessage(SendMessageRequest) returns (GetMessageResponse);
}
19 changes: 19 additions & 0 deletions examples/proto/example3.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
syntax = "proto3";

import "google/protobuf/timestamp.proto";

message Message {
string message = 1;
google.protobuf.Timestamp created_at = 2;
}

message SendMessageRequest {
string message = 1;
google.protobuf.Timestamp created_at = 2;
}

message GetMessageResponse { Message message = 1; }

service ExampleService {
rpc SendMessage(SendMessageRequest) returns (GetMessageResponse);
}
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
}
},
"scripts": {
"prepublish": "build",
"prepublish": "pnpm build",
"build": "rollup --config rollup.config.ts --configPlugin typescript"
},
"dependencies": {
Expand Down
Loading

0 comments on commit d0b7b58

Please sign in to comment.