Skip to content

Commit 13825f3

Browse files
committed
Add JS client example from webrpc repo
1 parent e4c3a0b commit 13825f3

File tree

8 files changed

+998
-0
lines changed

8 files changed

+998
-0
lines changed

_examples/hello-webrpc/Makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
all:
2+
@echo "please read Makefile source or README to see available commands"
3+
4+
tools:
5+
GO111MODULE=off go get -u github.com/goware/webify
6+
7+
generate: generate-server generate-client
8+
9+
generate-server:
10+
webrpc-gen -schema=hello-api.ridl -target=go -pkg=main -server -out=./server/hello_api.gen.go
11+
12+
generate-client:
13+
webrpc-gen -schema=hello-api.ridl -target=js -extra=noexports -client -out=./webapp/client.gen.js
14+
15+
run-server:
16+
go run ./server
17+
18+
run-client:
19+
webify -port=4444 -dir=./webapp
20+

_examples/hello-webrpc/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
hello-webrpc
2+
============
3+
4+
* Server: Go
5+
* Client: Web Browser (Javascript)
6+
7+
Simple client+server app with Go api backend (server) and Javascript Webapp (client).
8+
9+
1. `$ make tools` - to download `webify` cli to serve the 'webapp/' local files
10+
2. `$ make run-server` - to start the Go server at http://localhost:4242/
11+
3. `$ make run-client` - to serve webapp files at http://localhost:4444/
12+
4. Open your browser to https://localhost:4444/ and open your console, and see rpc calls, tada
13+
14+
webrpc comes with its own schema design language called RIDL, which stands for "RPC interface
15+
design language" :) it reads and feels like documentation, but it very flexible. See
16+
[hello-api.ridl](./hello-api.ridl) for the RIDL file for this service.
17+
18+
as well, webrpc supports a json-formatted schema with the identical functionality as the RIDL format.
19+
See here, [hello-api.webrpc.json](./hello-api.webrpc.json).

_examples/hello-webrpc/hello-api.ridl

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
webrpc = v1
2+
3+
name = hello-webrpc
4+
version = v1.0.0
5+
6+
enum Kind: uint32
7+
- USER = 1
8+
- ADMIN = 2
9+
10+
message Empty
11+
12+
message User
13+
- ID: uint64
14+
+ json = id
15+
+ go.tag.db = id
16+
17+
- username: string
18+
+ json = USERNAME
19+
+ go.tag.db = username
20+
21+
- createdAt?: timestamp
22+
+ json = created_at,omitempty
23+
+ go.tag.db = created_at
24+
25+
26+
service ExampleService
27+
- Ping() => (status: bool)
28+
- GetUser(userID: uint64) => (user: User)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
{
2+
"webrpc": "v1",
3+
"name": "hello-api",
4+
"version": "v0.0.1",
5+
"messages": [
6+
{
7+
"name": "Kind",
8+
"type": "enum",
9+
"fields": [
10+
{
11+
"name": "USER",
12+
"type": "uint32",
13+
"value": "1"
14+
},
15+
{
16+
"name": "ADMIN",
17+
"type": "uint32",
18+
"value": "2"
19+
}
20+
]
21+
},
22+
{
23+
"name": "Empty",
24+
"type": "struct",
25+
"fields": [
26+
]
27+
},
28+
{
29+
"name": "GetUserRequest",
30+
"type": "struct",
31+
"fields": [
32+
{
33+
"name": "userID",
34+
"type": "uint64",
35+
"optional": false
36+
}
37+
]
38+
},
39+
{
40+
"name": "User",
41+
"type": "struct",
42+
"fields": [
43+
{
44+
"name": "ID",
45+
"type": "uint64",
46+
"optional": false,
47+
"meta": [
48+
{ "json": "id" },
49+
{ "go.tag.db": "id" }
50+
]
51+
},
52+
{
53+
"name": "username",
54+
"type": "string",
55+
"optional": false,
56+
"meta": [
57+
{ "json": "USERNAME" },
58+
{ "go.tag.db": "username" }
59+
]
60+
},
61+
{
62+
"name": "createdAt",
63+
"type": "timestamp",
64+
"optional": true,
65+
"meta": [
66+
{ "json": "created_at,omitempty" },
67+
{ "go.tag.db": "created_at" }
68+
]
69+
}
70+
71+
]
72+
}
73+
],
74+
"services": [
75+
{
76+
"name": "ExampleService",
77+
"methods": [
78+
{
79+
"name": "Ping",
80+
"inputs": [],
81+
"outputs": [
82+
{
83+
"name": "",
84+
"type": "bool"
85+
}
86+
]
87+
},
88+
{
89+
"name": "GetUser",
90+
"inputs": [
91+
{
92+
"name": "req",
93+
"type": "GetUserRequest"
94+
}
95+
],
96+
"outputs": [
97+
{
98+
"name": "",
99+
"type": "User"
100+
}
101+
]
102+
}
103+
]
104+
}
105+
]
106+
}

0 commit comments

Comments
 (0)