Skip to content

Commit 76c6b87

Browse files
authored
Merge pull request #6 from DizoftTeam/feature/custom-handler
Add custom handler
2 parents ab86cc1 + b5855de commit 76c6b87

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010

1111
Base implementation of JSONRpc v2.0 in Go
1212

13+
## Breaking changes of v2
14+
15+
### HttpHandler
16+
17+
Cause: add new handler
18+
19+
> Old
20+
21+
http.HandleFunc("/", jsonrpc.Handler)
22+
23+
> New
24+
25+
http.HandleFunc("/", jsonrpc.HttpHandler)
26+
1327
## How to get
1428

1529
Use follow command
@@ -66,7 +80,7 @@ func registerMethods() {
6680
func main() {
6781
registerMethods()
6882

69-
http.HandleFunc("/", jsonrpc.Handler)
83+
http.HandleFunc("/", jsonrpc.HttpHandler)
7084

7185
log.Print("\nStarting server at :8089\n")
7286

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/DizoftTeam/jsonrpc_server
22

3-
go 1.15
3+
go 1.19
44

5-
require github.com/mitchellh/mapstructure v1.3.3
5+
require github.com/mitchellh/mapstructure v1.5.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
github.com/mitchellh/mapstructure v1.3.3 h1:SzB1nHZ2Xi+17FP0zVQBHIZqvwRN9408fJO8h+eeNA8=
2-
github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
1+
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
2+
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=

jsonrpc.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package jsonrpc_server
22

33
import (
4-
"github.com/DizoftTeam/jsonrpc_server/utils"
5-
6-
"github.com/mitchellh/mapstructure"
7-
84
"encoding/json"
95
"fmt"
106
"log"
117
"net/http"
8+
"os"
129
"reflect"
1310
"strings"
11+
12+
"github.com/DizoftTeam/jsonrpc_server/utils"
13+
"github.com/mitchellh/mapstructure"
1414
)
1515

1616
const (
@@ -20,6 +20,8 @@ const (
2020
var (
2121
methods = map[string]Method{} // Array of methods
2222
httpRequest *http.Request // Current request
23+
24+
jlog = log.New(os.Stderr, "[JSONRpc]", log.LstdFlags)
2325
)
2426

2527
// RPCRequest RPC struct
@@ -60,7 +62,7 @@ func Register(name string, handler RPCMethod) {
6062
func RegisterFunc(name string, method Method) {
6163
methods[name] = method
6264

63-
log.Printf("Register method: %v\n", name)
65+
jlog.Printf("Register method: %v\n", name)
6466
}
6567

6668
// NewSession create new request session
@@ -72,8 +74,34 @@ func NewSession() *Session {
7274

7375
// --------------- PUBLIC ---------------
7476

75-
// Handler Main point function
76-
func Handler(w http.ResponseWriter, r *http.Request) {
77+
// CustomHandler used for custom incoming messages point (like WS)
78+
func CustomHandler(rawJsonData []byte) string {
79+
var request interface{}
80+
var response string
81+
82+
if err := json.Unmarshal(rawJsonData, &request); err != nil {
83+
return `{"jsonrpc": "2.0", "error": {"code": -42700, "message": "Common Error"}}`
84+
}
85+
86+
reqType := reflect.ValueOf(request).Kind()
87+
88+
if reqType == reflect.Slice {
89+
var responses []string
90+
91+
for _, item := range request.([]interface{}) {
92+
responses = append(responses, processRequest(item.(map[string]interface{})))
93+
}
94+
95+
response = fmt.Sprintf("[%s]", strings.Join(responses, ","))
96+
} else {
97+
response = processRequest(request.(map[string]interface{}))
98+
}
99+
100+
return response
101+
}
102+
103+
// HttpHandler Main point function of http handler
104+
func HttpHandler(w http.ResponseWriter, r *http.Request) {
77105
defer r.Body.Close()
78106

79107
w.Header().Add("Content-Type", "application/json")

0 commit comments

Comments
 (0)