Skip to content

Commit 6522189

Browse files
committed
simplify signature check, update docs
1 parent 4cf29e8 commit 6522189

File tree

4 files changed

+19
-22
lines changed

4 files changed

+19
-22
lines changed

README.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# jrpc - rpc with json [![Build](https://github.com/go-pkgz/jrpc/actions/workflows/ci.yml/badge.svg)](https://github.com/go-pkgz/jrpc/actions/workflows/ci.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/go-pkgz/jrpc)](https://goreportcard.com/report/github.com/go-pkgz/jrpc) [![Coverage Status](https://coveralls.io/repos/github/go-pkgz/jrpc/badge.svg?branch=master)](https://coveralls.io/github/go-pkgz/jrpc?branch=master) [![godoc](https://godoc.org/github.com/go-pkgz/jrpc?status.svg)](https://godoc.org/github.com/go-pkgz/jrpc)
22

33
jrpc library provides client and server for RPC-like communication over HTTP with json encoded messages.
4-
The protocol is a somewhat simplified version of json-rpc with a single POST call sending Request json
5-
(method name and the list of parameters) moreover, receiving json Response with result data and an error string.
4+
The protocol is a somewhat simplified version of json-rpc with a single POST call sending `Request` json
5+
(method name and the list of parameters) moreover, receiving json `Response` with result data and an error string.
66

77
## Usage
88

@@ -28,29 +28,27 @@ plugin.Run(9090)
2828

2929
The constructor `NewServer` accept two parameters:
3030
* `API` - a base url for rpc calls
31-
* `Options` - allows configure server parameters such is timeouts, logger, limits, middlewares and etc.
32-
33-
##### options
34-
`jrpc.NewServer` call accepts functional options:
35-
* `jrpc.Auth` - set credentials basic auth to server, accepts `username` and `password`
36-
* `jrpc.WithTimeout` - sets global timeouts for server requests, such as read, write and idle. Call accept `Timeouts` struct.
37-
* `jrpc.WithLimits` - define limit for server call, accepts limit value in `float64` type
38-
* `jrpc.WithThrottler` - sets throttler middleware with specify limit value
39-
* `jrpc.WithMiddlewares` - sets custom middlewares list to server, accepts list of handler with idiomatic type `func(http.Handler) http.Handler`
40-
* `jrpc.WithtSignature` - sets server signature, accept appName, author and version. Disable by default.
41-
* `jrpc.WithLogger` - define custom logger (e.g. [lgr](https://github.com/go-pkgz/lgr))
31+
* `Options` - optional parameters such is timeouts, logger, limits, middlewares and etc.
32+
* `Auth` - set credentials basic auth to server, accepts `username` and `password`
33+
* `WithTimeout` - sets global timeouts for server requests, such as read, write and idle. Call accept `Timeouts` struct.
34+
* `WithLimits` - define limit for server call, accepts limit value in `float64` type
35+
* `WithThrottler` - sets throttler middleware with specify limit value
36+
* `WithtSignature` - sets server signature, accept appName, author and version. Disable by default.
37+
* `WithLogger` - define custom logger (e.g. [lgr](https://github.com/go-pkgz/lgr))
38+
* `WithMiddlewares` - sets custom middlewares list to server, accepts list of handler with idiomatic type `func(http.Handler) http.Handler`
4239

4340
Example with options:
4441
```go
4542
plugin := NewServer("/command",
43+
Auth("user", "password"),
4644
WithTimeout(Timeouts{ReadHeaderTimeout: 5 * time.Second, WriteTimeout: 5 * time.Second, IdleTimeout: 10 * time.Second}),
4745
WithThrottler(120),
48-
WithLimits(100),
49-
WithMiddlewares(middleware.Heartbeat('/ping'), middleware.Profiler, middleware. StripSlashes),
46+
WithLimits(100),
47+
WithtSignature("the best plugin ever", "author", "1.0.0"),
48+
WithMiddlewares(middleware.Heartbeat('/ping'), middleware.Profiler, middleware.StripSlashes),
5049
)
5150
```
52-
**NOTICE:**
53-
Such middlewares as `RealIP`,`Recoverer`,`Ping`,`NoCache` and `basicAuth` define by default.
51+
5452
### Application (client)
5553

5654
```go

options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func WithMiddlewares(middlewares ...func(http.Handler) http.Handler) Option {
5454
// WithSignature sets signature data for server response headers
5555
func WithSignature(appName, author, version string) Option {
5656
return func(s *Server) {
57-
s.signature = &signaturePayload{
57+
s.signature = signaturePayload{
5858
appName: appName,
5959
author: author,
6060
version: version,

server.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ type Server struct {
2626
authPasswd string // basic auth password, should match Client.AuthPasswd, optional
2727
customMiddlewares middlewares // list of custom middlewares, should match array of http.Handler func, optional
2828

29-
// use as pointer for checks all fields at one time
30-
signature *signaturePayload // add server signature to server response headers appName, author, version), disable by default
29+
signature signaturePayload // add server signature to server response headers appName, author, version), disable by default
3130

3231
timeouts Timeouts // values and timeouts for the server
3332
limits limits // values and limits for the server
@@ -106,7 +105,7 @@ func (s *Server) Run(port int) error {
106105

107106
router.Use(middleware.RealIP, rest.Ping, rest.Recoverer(s.logger))
108107

109-
if s.signature != nil {
108+
if s.signature.version != "" || s.signature.author != "" || s.signature.appName != "" {
110109
router.Use(rest.AppInfo(s.signature.appName, s.signature.author, s.signature.version))
111110
}
112111

server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func TestServer_WithSignature(t *testing.T) {
313313
}
314314

315315
s = NewServer("/v1/cmd", WithSignature("testApp", "testAuthor", "0.1.0"), WithMiddlewares(checkSignatureMiddlewareFn))
316-
assert.Equal(t, &signaturePayload{"testApp", "testAuthor", "0.1.0"}, s.signature)
316+
assert.Equal(t, signaturePayload{"testApp", "testAuthor", "0.1.0"}, s.signature)
317317

318318
s.Add("fn1", func(id uint64, params json.RawMessage) Response {
319319
return Response{}

0 commit comments

Comments
 (0)