Skip to content

How To Generate a RPC Service

Stefan Martinov edited this page Jul 7, 2017 · 5 revisions

Initializing the repository

To generate a gotsrpc service, we need to make sure that we have gotsrpc installed. To install gotscrp check out the gotsrpc repository and installation instructions on https://github.com/foomo/gotsrpc.

After we installed the gotsrpc, we can verify that it's working by calling gotsrpc which should print out its usage.

To use the gotsrpc first we need to have a definition file called gotsrpc.yml

For example, if we wanted to generate a demo gotsrpc servife, for 2 services Foo and Demo, and expose them under /service/ and generate a go implementation as well as the typescript implementation:

---
targets:
  demo    
    module: GoTSRPC.Demo
    services:
      /service/foo: Foo
      /service/demo: Demo
    package: github.com/foomo/gotsrpc/demo
    out: /tmp/test.ts
    gorpc:
      - Foo
      - Demo
    tsrpc:
      - Foo
      - Demo

  mappings:
    github.com/foomo/gotsrpc/demo:
      module: GoTSRPC.Demo
      out: /tmp/test-files-demo.ts
    github.com/foomo/gotsrpc/demo/nested:
      module: GoTSRPC.Demo.Nested
      out: /tmp/test-files-demo-nested.ts

Adding a new endpoint (target)

Find the place where we need to add the new endpoint and extend the service that generates the outputs. We can add as many request and response parameters, however the first two method parameters must always be w http.ResponseWriter, r *http.Request and the last return parameter must be *services.ServiceError

func (s *Service) SERVICE_NAME(w http.ResponseWriter, r *http.Request, params Type...*) (params Type...*, e *services.ServiceError) {
  //1. Invoke validations such as permission/account checks
  //2. Retrieve required data for service invocation, such as users, context etc..
  //3. Invoke the facade service and return the result
  return ...
}

After adding and wiring the service, we then need to add it to the gotsrpc.yml definition file which will mark it to be generated. We will specify it to generate the typescript file in the temp folder as well as to generate invocations for go and typescript.

---
targets:
  ...
  myservice:
    module: <Module.Service>
    services:
      /services/service_name: service_name
    package: github.com/<githuborg>/<githubrepo>/module
    out: /tmp/typescriptservice.ts
    gorpc:
      - service_name
    tsrpc:
      - service_name

After adding the new target, we need to add the mappings for the value objects used in the RPC, for our module. The mappings will generate typescript objects required to invoke hte RPC service.

To add the mapping we need to edit the gotsrpc.yml

  ...
  mappings:
    github.com/<githuborg>/<githubrepo>/module:
      module: <Module.Service>
      out: ~/tmp/vo/typescriptservice.ts

And after all the steps were completed, don't forget to generate the gotsrpc with the updated gotsrpc configuration file, e.g. gotsrpc gotsrpc.yml in the folder where the gotsrpc.yml file is located.

Clone this wiki locally