From 4a81eff217c40c459c9a9ed4f318b4dd9bc5ee8a Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Sun, 22 May 2022 16:37:48 +0300 Subject: Middlewares and options --- example/main.go | 13 +++++++++---- example/test.http | 36 ------------------------------------ 2 files changed, 9 insertions(+), 40 deletions(-) (limited to 'example') diff --git a/example/main.go b/example/main.go index 9f25e61..d5d034e 100644 --- a/example/main.go +++ b/example/main.go @@ -12,10 +12,15 @@ import ( ) func main() { - s := rpc.New() - - s.AddTransport(&transport.HTTP{Bind: ":8000"}) - s.AddTransport(&transport.TCP{Bind: ":3000"}) + s := rpc.New( + rpc.WithLogger(rpc.StdLogger), + rpc.WithTransport(&transport.HTTP{Bind: ":8000", CORSOrigin: "*"}), + ) + // Set options after constructor + s.Use( + rpc.WithTransport(&transport.TCP{Bind: ":3000"}), + rpc.WithMiddleware(rpc.LoggerMiddleware(rpc.StdLogger)), + ) s.Register("multiply", rpc.H(Multiply)) s.Register("divide", rpc.H(Divide)) diff --git a/example/test.http b/example/test.http index 7ed134a..e126917 100644 --- a/example/test.http +++ b/example/test.http @@ -11,18 +11,6 @@ Content-Type: application/json } -date: Sat, 21 May 2022 16:53:31 GMT -content-length: 36 -content-type: text/plain; charset=utf-8 -connection: close - -HTTP/1.1 200 - OK -date: Sat, 21 May 2022 16:53:31 GMT -content-length: 36 -content-type: text/plain; charset=utf-8 -connection: close - - ### POST http://localhost:8000/ Content-Type: application/json @@ -37,18 +25,6 @@ Content-Type: application/json } -date: Sat, 21 May 2022 16:53:51 GMT -content-length: 52 -content-type: text/plain; charset=utf-8 -connection: close - -HTTP/1.1 200 - OK -date: Sat, 21 May 2022 16:53:51 GMT -content-length: 52 -content-type: text/plain; charset=utf-8 -connection: close - - ### Batch request POST http://localhost:8000/ Content-Type: application/json @@ -68,15 +44,3 @@ 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"} - - -date: Sat, 21 May 2022 17:18:17 GMT -content-type: text/plain; charset=utf-8 -connection: close -transfer-encoding: chunked - -HTTP/1.1 200 - OK -date: Sat, 21 May 2022 17:18:17 GMT -content-type: text/plain; charset=utf-8 -connection: close -transfer-encoding: chunked \ No newline at end of file -- cgit v1.2.3