Skip to content

Commit

Permalink
feat: add simple example
Browse files Browse the repository at this point in the history
  • Loading branch information
chakhsu committed Jan 20, 2024
1 parent ea4d12a commit b8b079b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions example/asyncStream/loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ProtoLoader } from '../../lib/index.js'
// same as import { ProtoLoader } from 'grpcity'

import path from 'node:path'
import { fileURLToPath } from 'node:url'

Expand Down
2 changes: 2 additions & 0 deletions example/helloworld/loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ProtoLoader } from '../../lib/index.js'
// same as import { ProtoLoader } from 'grpcity'

import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
Expand Down
2 changes: 2 additions & 0 deletions example/reflection/loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ProtoLoader } from '../../lib/index.js'
// same as import { ProtoLoader } from 'grpcity'

import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
Expand Down
12 changes: 12 additions & 0 deletions example/simple/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { loader } from './loader.js'

const start = async (addr) => {
await loader.init()
const clients = await loader.initClients({ services: { 'simple.Hellor': addr } })

const client = clients.get('simple.Hellor')
const { response } = await client.sayHello({ message: 'grpcity' })
console.log(response)
}

start('localhost:5051')
13 changes: 13 additions & 0 deletions example/simple/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ProtoLoader } from '../../lib/index.js'
// same as import { ProtoLoader } from 'grpcity'

import path from 'node:path'
import { fileURLToPath } from 'node:url'

// get this file dir path
const __dirname = path.dirname(fileURLToPath(import.meta.url))

export const loader = new ProtoLoader({
location: path.join(__dirname, './'),
files: ['service.proto']
})
18 changes: 18 additions & 0 deletions example/simple/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { loader } from './loader.js'

const start = async (addr) => {
await loader.init()
const server = await loader.initServer()

const implementation = {
sayHello: async (call) => {
return { message: `I got your message: ${call.request.message}` }
}
}

server.add('simple.Hellor', implementation)
await server.listen(addr)
console.log('start:', addr)
}

start('localhost:5051')
11 changes: 11 additions & 0 deletions example/simple/service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package simple;

service Hellor {
rpc sayHello (Message) returns (Message) {}
}

message Message {
string message = 1;
}
2 changes: 2 additions & 0 deletions example/stream/loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ProtoLoader } from '../../lib/index.js'
// same as import { ProtoLoader } from 'grpcity'

import path from 'node:path'
import { fileURLToPath } from 'node:url'

Expand Down

0 comments on commit b8b079b

Please sign in to comment.