aboutsummaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-05-28 16:53:20 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-05-28 16:53:20 +0300
commitf304a07a8cfe67b2a65f95f27eb10a9b854c4ef8 (patch)
tree9e6a7e9ea6b8d600cccac5a5d50f3232d631d073 /example
parent281eda83c9f4bcb06318444df3574df0840205fa (diff)
Improved middlewares
Diffstat (limited to 'example')
-rw-r--r--example/main.go37
-rw-r--r--example/test.http17
2 files changed, 52 insertions, 2 deletions
diff --git a/example/main.go b/example/main.go
index d5d034e..11e1fb0 100644
--- a/example/main.go
+++ b/example/main.go
@@ -7,7 +7,9 @@ import (
"os"
"os/signal"
+ "github.com/qri-io/jsonschema"
"go.neonxp.dev/jsonrpc2/rpc"
+ "go.neonxp.dev/jsonrpc2/rpc/middleware"
"go.neonxp.dev/jsonrpc2/transport"
)
@@ -17,9 +19,42 @@ func main() {
rpc.WithTransport(&transport.HTTP{Bind: ":8000", CORSOrigin: "*"}),
)
// Set options after constructor
+ validation, err := middleware.Validation(map[string]middleware.MethodSchema{
+ "divide": {
+ Request: *jsonschema.Must(`{
+ "type": "object",
+ "properties": {
+ "a": {
+ "type": "integer"
+ },
+ "b": {
+ "type": "integer",
+ "not":{"const":0}
+ }
+ },
+ "required": ["a", "b"]
+ }`),
+ Response: *jsonschema.Must(`{
+ "type": "object",
+ "properties": {
+ "quo": {
+ "type": "integer"
+ },
+ "rem": {
+ "type": "integer"
+ }
+ },
+ "required": ["quo", "rem"]
+ }`),
+ },
+ })
+ if err != nil {
+ log.Fatal(err)
+ }
s.Use(
rpc.WithTransport(&transport.TCP{Bind: ":3000"}),
- rpc.WithMiddleware(rpc.LoggerMiddleware(rpc.StdLogger)),
+ rpc.WithMiddleware(middleware.Logger(rpc.StdLogger)),
+ rpc.WithMiddleware(validation),
)
s.Register("multiply", rpc.H(Multiply))
diff --git a/example/test.http b/example/test.http
index e126917..9e71f9e 100644
--- a/example/test.http
+++ b/example/test.http
@@ -19,7 +19,7 @@ Content-Type: application/json
"method": "divide",
"params": {
"a": 10,
- "b": 3
+ "b": 10
},
"id": 2
}
@@ -44,3 +44,18 @@ Content-Type: application/json
{"foo": "boo"}
{"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"}
{"jsonrpc": "2.0", "method": "get_data", "id": "9"}
+ {
+ "jsonrpc": "2.0",
+ "method": "divide",
+ "params": {
+ "a": 10,
+ "b": 0
+ },
+ "id": "divide"
+ }
+ {
+ "jsonrpc": "2.0",
+ "method": "divide",
+ "params": {},
+ "id": "divide"
+ }