-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
995 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Example for Fastify |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}, | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.