aboutsummaryrefslogtreecommitdiff
path: root/rpc/middleware.go
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-05-22 16:37:48 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-05-22 16:37:48 +0300
commit4a81eff217c40c459c9a9ed4f318b4dd9bc5ee8a (patch)
tree35e6d3b1f80af80fddedb26d5543377931abfe2f /rpc/middleware.go
parentc74596c6a6a741e3365a2f372de6e7cdf2583fdc (diff)
Middlewares and optionsv1.1.0
Diffstat (limited to 'rpc/middleware.go')
-rw-r--r--rpc/middleware.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/rpc/middleware.go b/rpc/middleware.go
new file mode 100644
index 0000000..cd99823
--- /dev/null
+++ b/rpc/middleware.go
@@ -0,0 +1,43 @@
+//Package rpc provides abstract rpc server
+//
+//Copyright (C) 2022 Alexander Kiryukhin <i@neonxp.dev>
+//
+//This file is part of go.neonxp.dev/jsonrpc2 project.
+//
+//This program is free software: you can redistribute it and/or modify
+//it under the terms of the GNU General Public License as published by
+//the Free Software Foundation, either version 3 of the License, or
+//(at your option) any later version.
+//
+//This program is distributed in the hope that it will be useful,
+//but WITHOUT ANY WARRANTY; without even the implied warranty of
+//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//GNU General Public License for more details.
+//
+//You should have received a copy of the GNU General Public License
+//along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "context"
+ "strings"
+ "time"
+)
+
+type Middleware func(handler RpcHandler) RpcHandler
+
+type RpcHandler func(ctx context.Context, req *RpcRequest) *RpcResponse
+
+func LoggerMiddleware(logger Logger) Middleware {
+ return func(handler RpcHandler) RpcHandler {
+ return func(ctx context.Context, req *RpcRequest) *RpcResponse {
+ t1 := time.Now().UnixMicro()
+ resp := handler(ctx, req)
+ t2 := time.Now().UnixMicro()
+ args := strings.ReplaceAll(string(req.Params), "\n", "")
+ logger.Logf("rpc call=%s, args=%s, take=%dμs", req.Method, args, (t2 - t1))
+ return resp
+ }
+ }
+}