webrpc golang-basics example
A simple example of a Go web service built using webrpc.
The process of developing something like this is..
- Start with your webrpc schema file, in this case, ./example.ridl in RIDL format.. or,
you can also write your schema in JSON format like so, ./example.webrpc.json. RIDL is simpler :)
- Design your schema file and think about the methods calls clients will need to make
to your service
- Write the "services" section of the schema file
- From the inputs and outputs for the function definitions, start writing the "messages"
section of the data types needed in your program.
- Run the code generator to build the server and client:
webrpc-gen -schema=example.ridl -target=go -pkg=main -server -client -out=./example.gen.go
- or... *
webrpc-gen -schema=example.webrpc.json -target=go -pkg=main -server -client -out=./example.gen.go
- however, in this example we put it inside a
go:generate
, so you can run go generate .
- Write your server (./main.go) and implement the
ExampleServiceRPC
interface type
that was created by the code generator, and located in the gen'd file.
- Enjoy!
Next steps, you can generate a Typescript client by running:
webrpc-gen -schema=example.ridl -target=ts -pkg=example -client -out=./example-client.ts
- check out the hello-webrpc for an example with a Webapp client talking to a webrpc backend
Testing the example
You can run the tests if you want with go test -v .
.
Running the example
You can make strongly-typed requests to the server through the generated Go client
as done inside of example_test.go, as in..
resp, err := client.GetUser(context.Background(), &GetUserRequest{
UserID: 1234,
})
spew.Dump(err)
spew.Dump(resp)
..or, if you want to see the internals, lets run some curl commands manually. In another terminal
window, run some raw curl commands:
Request:
curl -v -X POST -H"Content-Type: application/json" -v -d '{"userID":1234}' http://localhost:4242/rpc/ExampleService/GetUser
Response:
{"id":1234,"USERNAME":"hihi"}
How it works
Please read all of the source in this folder :) including example_test.go