From f304a07a8cfe67b2a65f95f27eb10a9b854c4ef8 Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Sat, 28 May 2022 16:53:20 +0300 Subject: Improved middlewares --- example/main.go | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'example/main.go') 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)) -- cgit v1.2.3