github.com/vcilabs/webrpc@v0.5.2-0.20201116131534-162e27b1b33b/_examples/golang-basics/README.md (about)

     1  webrpc golang-basics example
     2  ============================
     3  
     4  * Server: Go
     5  * Client: Go
     6  
     7  A simple example of a Go web service built using webrpc.
     8  
     9  The process of developing something like this is..
    10  
    11  1. Start with your webrpc schema file, in this case, [./example.ridl](./example.ridl) in RIDL format.. or,
    12  you can also write your schema in JSON format like so, [./example.webrpc.json](./example.webrpc.json). RIDL is simpler :)
    13  2. Design your schema file and think about the methods calls clients will need to make
    14  to your service
    15  3. Write the "services" section of the schema file
    16  4. From the inputs and outputs for the function definitions, start writing the "messages"
    17  section of the data types needed in your program.
    18  5. Run the code generator to build the server and client:
    19    * `webrpc-gen -schema=example.ridl -target=go -pkg=main -server -client -out=./example.gen.go`
    20    * or...   * `webrpc-gen -schema=example.webrpc.json -target=go -pkg=main -server -client -out=./example.gen.go`
    21    * however, in this example we put it inside a `go:generate`, so you can run `go generate .`
    22  6. Write your server ([./main.go](./main.go)) and implement the `ExampleServiceRPC` interface type
    23  that was created by the code generator, and located in the [gen'd file](./example.gen.go).
    24  7. Enjoy!
    25  
    26  Next steps, you can generate a Typescript client by running:
    27  * `webrpc-gen -schema=example.ridl -target=ts -pkg=example -client -out=./example-client.ts`
    28  * check out the [hello-webrpc](../hello-webrpc) for an example with a Webapp client talking to a webrpc backend
    29  
    30  
    31  ## Testing the example
    32  
    33  You can run the tests if you want with `go test -v .`.
    34  
    35  
    36  ### Running the example
    37  
    38  * $ `cd _examples/golang-basics`
    39  
    40  * $ `go run .` -- runs the server
    41  
    42  You can make strongly-typed requests to the server through the generated Go client
    43  as done inside of example_test.go, as in..
    44  
    45  ```go
    46  resp, err := client.GetUser(context.Background(), &GetUserRequest{
    47    UserID: 1234,
    48  })
    49  spew.Dump(err)
    50  spew.Dump(resp)
    51  ```
    52  
    53  ..or, if you want to see the internals, lets run some curl commands manually. In another terminal
    54  window, run some raw curl commands:
    55  
    56  *Request:*
    57  ```
    58  curl -v -X POST -H"Content-Type: application/json" -v -d '{"userID":1234}' http://localhost:4242/rpc/ExampleService/GetUser
    59  ```
    60  
    61  *Response:*
    62  ```
    63  {"id":1234,"USERNAME":"hihi"}
    64  ```
    65  
    66  
    67  ### How it works
    68  
    69  Please read all of the source in this folder :) including example_test.go