Skip to content

Commit c874109

Browse files
committed
Initial commit
0 parents  commit c874109

19 files changed

+798
-0
lines changed

.gitignore

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Compiled Object files, Static and Dynamic libs (Shared Objects)
2+
*.o
3+
*.a
4+
*.so
5+
6+
# Folders
7+
_obj
8+
_test
9+
.idea
10+
vendor
11+
12+
# Architecture specific extensions/prefixes
13+
*.[568vq]
14+
[568vq].out
15+
16+
*.cgo1.go
17+
*.cgo2.c
18+
_cgo_defun.c
19+
_cgo_gotypes.go
20+
_cgo_export.*
21+
22+
_testmain.go
23+
24+
*.exe
25+
*.test
26+
*.prof

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Osamu TONOMORI
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# jsonrpc
2+
3+
[![CircleCI](https://img.shields.io/circleci/project/osamingo/jsonrpc/master.svg)](https://circleci.com/gh/osamingo/jsonrpc)
4+
[![codecov](https://codecov.io/gh/osamingo/jsonrpc/branch/master/graph/badge.svg)](https://codecov.io/gh/osamingo/jsonrpc)
5+
[![Go Report Card](https://goreportcard.com/badge/osamingo/jsonrpc)](https://goreportcard.com/report/osamingo/jsonrpc)
6+
[![codebeat badge](https://codebeat.co/badges/cbd0290d-200b-4693-80dc-296d9447c35b)](https://codebeat.co/projects/github-com-osamingo-jsonrpc)
7+
[![GoDoc](https://godoc.org/github.com/osamingo/jsonrpc?status.svg)](https://godoc.org/github.com/osamingo/jsonrpc)
8+
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/osamingo/jsonrpc/master/LICENSE)
9+
10+
## About
11+
12+
- Simple implements
13+
- No `reflect` package
14+
- Compliance with JSON-RPC 2.0
15+
16+
## Install
17+
18+
```
19+
$ go get -u github.com/osamingo/jsonrpc
20+
```
21+
22+
## Usage
23+
24+
```go
25+
package main
26+
27+
import (
28+
"context"
29+
"encoding/json"
30+
"log"
31+
"net/http"
32+
33+
"github.com/osamingo/jsonrpc"
34+
)
35+
36+
type (
37+
EchoParams struct {
38+
Name string `json:"name"`
39+
}
40+
EchoResult struct {
41+
Message string `json:"message"`
42+
}
43+
)
44+
45+
func Echo(c context.Context, params *json.RawMessage) (interface{}, *jsonrpc.Error) {
46+
47+
var p EchoParams
48+
if err := json.Unmarshal(*params, &p); err != nil {
49+
return nil, jsonrpc.ErrInvalidParams()
50+
}
51+
52+
return EchoResult{
53+
Message: "Hello, " + p.Name,
54+
}, nil
55+
}
56+
57+
func JSONRPC(w http.ResponseWriter, r *http.Request) {
58+
59+
rs, err := jsonrpc.ParseRequest(r)
60+
if err != nil {
61+
jsonrpc.SendResponse(w, []jsonrpc.Response{
62+
{
63+
Version: jsonrpc.Version,
64+
Error: err,
65+
},
66+
})
67+
return
68+
}
69+
70+
resp := make([]jsonrpc.Response, 0, len(rs))
71+
for i := range rs {
72+
var f jsonrpc.Func
73+
res := jsonrpc.NewResponse(rs[i])
74+
f, res.Error = jsonrpc.TakeMethod(rs[i])
75+
if res.Error != nil {
76+
resp = append(resp, res)
77+
continue
78+
}
79+
80+
res.Result, res.Error = f(r.Context(), rs[i].Params)
81+
resp = append(resp, res)
82+
}
83+
84+
if err := jsonrpc.SendResponse(w, resp); err != nil {
85+
log.Println(err)
86+
}
87+
}
88+
89+
func init() {
90+
jsonrpc.RegisterMethod("Echo", Echo)
91+
}
92+
93+
func main() {
94+
http.HandleFunc("/_jr", JSONRPC)
95+
if err := http.ListenAndServe(":8080", nil); err != nil {
96+
log.Fatalln(err)
97+
}
98+
}
99+
```
100+
101+
## License
102+
103+
Released under the [MIT License](https://github.com/osamingo/jsonrpc/blob/master/LICENSE).

_example/main.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"log"
7+
"net/http"
8+
9+
"github.com/osamingo/jsonrpc"
10+
)
11+
12+
type (
13+
EchoParams struct {
14+
Name string `json:"name"`
15+
}
16+
EchoResult struct {
17+
Message string `json:"message"`
18+
}
19+
)
20+
21+
func Echo(c context.Context, params *json.RawMessage) (interface{}, *jsonrpc.Error) {
22+
23+
var p EchoParams
24+
if err := json.Unmarshal(*params, &p); err != nil {
25+
return nil, jsonrpc.ErrInvalidParams()
26+
}
27+
28+
return EchoResult{
29+
Message: "Hello, " + p.Name,
30+
}, nil
31+
}
32+
33+
func JSONRPC(w http.ResponseWriter, r *http.Request) {
34+
35+
rs, err := jsonrpc.ParseRequest(r)
36+
if err != nil {
37+
jsonrpc.SendResponse(w, []jsonrpc.Response{
38+
{
39+
Version: jsonrpc.Version,
40+
Error: err,
41+
},
42+
})
43+
return
44+
}
45+
46+
resp := make([]jsonrpc.Response, 0, len(rs))
47+
for i := range rs {
48+
var f jsonrpc.Func
49+
res := jsonrpc.NewResponse(rs[i])
50+
f, res.Error = jsonrpc.TakeMethod(rs[i])
51+
if res.Error != nil {
52+
resp = append(resp, res)
53+
continue
54+
}
55+
56+
res.Result, res.Error = f(r.Context(), rs[i].Params)
57+
resp = append(resp, res)
58+
}
59+
60+
if err := jsonrpc.SendResponse(w, resp); err != nil {
61+
log.Println(err)
62+
}
63+
}
64+
65+
func init() {
66+
jsonrpc.RegisterMethod("Echo", Echo)
67+
}
68+
69+
func main() {
70+
http.HandleFunc("/_jr", JSONRPC)
71+
if err := http.ListenAndServe(":8080", nil); err != nil {
72+
log.Fatalln(err)
73+
}
74+
}

circle.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
machine:
2+
environment:
3+
IMPORT_PATH: "/home/ubuntu/.go_workspace/src/github.com/osamingo"
4+
APP_PATH: "${IMPORT_PATH}/jsonrpc"
5+
6+
dependencies:
7+
override:
8+
- sudo add-apt-repository ppa:masterminds/glide -y
9+
- sudo apt-get update
10+
- sudo apt-get install glide -y
11+
12+
test:
13+
pre:
14+
- mkdir -p "${IMPORT_PATH}"
15+
- ln -sf "$(pwd)" "${APP_PATH}"
16+
- cd "${APP_PATH}" && glide install
17+
override:
18+
- cd "${APP_PATH}" && go test -race -covermode=atomic -coverprofile=coverage.txt .
19+
post:
20+
- cd "${APP_PATH}" && bash <(curl -s https://codecov.io/bash)

codecov.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
codecov:
2+
branch: master
3+
4+
coverage:
5+
precision: 2
6+
round: down
7+
range: "70...100"
8+
9+
status:
10+
project:
11+
default:
12+
target: auto
13+
threshold: 90
14+
branches: null
15+
16+
patch:
17+
default:
18+
target: auto
19+
branches: null
20+
21+
changes:
22+
default:
23+
branches: null
24+
25+
ignore:
26+
- .*/vendor/.*
27+
28+
comment: off

doc.go

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
Package jsonrpc helps JSON-RPC 2.0 implements.
3+
*/
4+
package jsonrpc

error.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package jsonrpc
2+
3+
import "fmt"
4+
5+
const (
6+
// ErrorCodeParse is parse error code.
7+
ErrorCodeParse ErrorCode = -32700
8+
// ErrorCodeInvalidRequest is invalid request error code.
9+
ErrorCodeInvalidRequest ErrorCode = -32600
10+
// ErrorCodeMethodNotFound is method not found error code.
11+
ErrorCodeMethodNotFound ErrorCode = -32601
12+
// ErrorCodeInvalidParams is invalid params error code.
13+
ErrorCodeInvalidParams ErrorCode = -32602
14+
// ErrorCodeInternal is internal error code.
15+
ErrorCodeInternal ErrorCode = -32603
16+
)
17+
18+
var _ error = (*Error)(nil)
19+
20+
type (
21+
// A ErrorCode by JSON-RPC 2.0.
22+
ErrorCode int
23+
24+
// An Error is a wrapper for a JSON interface value.
25+
Error struct {
26+
Code ErrorCode `json:"code"`
27+
Message string `json:"message"`
28+
Data interface{} `json:"data,omitempty"`
29+
}
30+
)
31+
32+
// Error implements error interface.
33+
func (e *Error) Error() string {
34+
return fmt.Sprintf("jsonrpc: code: %d, message: %s, data: %+v", e.Code, e.Message, e.Data)
35+
}
36+
37+
// ErrParse returns parse error.
38+
func ErrParse() *Error {
39+
return &Error{
40+
Code: ErrorCodeParse,
41+
Message: "Parse error",
42+
}
43+
}
44+
45+
// ErrInvalidRequest returns invalid request error.
46+
func ErrInvalidRequest() *Error {
47+
return &Error{
48+
Code: ErrorCodeInvalidRequest,
49+
Message: "Invalid Request",
50+
}
51+
}
52+
53+
// ErrMethodNotFound returns method not found error.
54+
func ErrMethodNotFound() *Error {
55+
return &Error{
56+
Code: ErrorCodeMethodNotFound,
57+
Message: "Method not found",
58+
}
59+
}
60+
61+
// ErrInvalidParams returns invalid params error.
62+
func ErrInvalidParams() *Error {
63+
return &Error{
64+
Code: ErrorCodeInvalidParams,
65+
Message: "Invalid params",
66+
}
67+
}
68+
69+
// ErrInternal returns internal error.
70+
func ErrInternal() *Error {
71+
return &Error{
72+
Code: ErrorCodeInternal,
73+
Message: "Internal error",
74+
}
75+
}

0 commit comments

Comments
 (0)