You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
5.3 KiB

  1. # jsonrpc
  2. [![Travis branch](https://img.shields.io/travis/osamingo/jsonrpc/master.svg)](https://travis-ci.org/osamingo/jsonrpc)
  3. [![codecov](https://codecov.io/gh/osamingo/jsonrpc/branch/master/graph/badge.svg)](https://codecov.io/gh/osamingo/jsonrpc)
  4. [![Test Coverage](https://api.codeclimate.com/v1/badges/e820b394cdbd47103165/test_coverage)](https://codeclimate.com/github/osamingo/jsonrpc/test_coverage)
  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. [![Maintainability](https://api.codeclimate.com/v1/badges/e820b394cdbd47103165/maintainability)](https://codeclimate.com/github/osamingo/jsonrpc/maintainability)
  8. [![GoDoc](https://godoc.org/github.com/osamingo/jsonrpc?status.svg)](https://godoc.org/github.com/osamingo/jsonrpc)
  9. [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/osamingo/jsonrpc/master/LICENSE)
  10. ## About
  11. - Simple, Poetic, Pithy.
  12. - No `reflect` package.
  13. - But `reflect` package is used only when invoke the debug handler.
  14. - Support GAE/Go Standard Environment.
  15. - Compliance with [JSON-RPC 2.0](http://www.jsonrpc.org/specification).
  16. Note: If you use Go 1.6, see [v1.0](https://github.com/osamingo/jsonrpc/releases/tag/v1.0).
  17. ## Install
  18. ```
  19. $ go get -u github.com/osamingo/jsonrpc
  20. ```
  21. ## Usage
  22. ```go
  23. package main
  24. import (
  25. "context"
  26. "log"
  27. "net/http"
  28. "github.com/intel-go/fastjson"
  29. "github.com/osamingo/jsonrpc"
  30. )
  31. type (
  32. EchoHandler struct{}
  33. EchoParams struct {
  34. Name string `json:"name"`
  35. }
  36. EchoResult struct {
  37. Message string `json:"message"`
  38. }
  39. )
  40. func (h EchoHandler) ServeJSONRPC(c context.Context, params *fastjson.RawMessage) (interface{}, *jsonrpc.Error) {
  41. var p EchoParams
  42. if err := jsonrpc.Unmarshal(params, &p); err != nil {
  43. return nil, err
  44. }
  45. return EchoResult{
  46. Message: "Hello, " + p.Name,
  47. }, nil
  48. }
  49. func main() {
  50. mr := jsonrpc.NewMethodRepository()
  51. if err := mr.RegisterMethod("Main.Echo", EchoHandler{}, EchoParams{}, EchoResult{}); err != nil {
  52. log.Fatalln(err)
  53. }
  54. http.Handle("/jrpc", mr)
  55. http.HandleFunc("/jrpc/debug", mr.ServeDebug)
  56. if err := http.ListenAndServe(":8080", http.DefaultServeMux); err != nil {
  57. log.Fatalln(err)
  58. }
  59. }
  60. ```
  61. #### Advanced
  62. ```go
  63. package main
  64. import (
  65. "log"
  66. "net/http"
  67. "github.com/osamingo/jsonrpc"
  68. )
  69. type (
  70. HandleParamsResulter interface {
  71. jsonrpc.Handler
  72. Name() string
  73. Params() interface{}
  74. Result() interface{}
  75. }
  76. Servicer interface {
  77. MethodName(HandleParamsResulter) string
  78. Handlers() []HandleParamsResulter
  79. }
  80. UserService struct {
  81. SignUpHandler HandleParamsResulter
  82. SignInHandler HandleParamsResulter
  83. }
  84. )
  85. func (us *UserService) MethodName(h HandleParamsResulter) string {
  86. return "UserService." + h.Name()
  87. }
  88. func (us *UserService) Handlers() []HandleParamsResulter {
  89. return []HandleParamsResulter{us.SignUpHandler, us.SignInHandler}
  90. }
  91. func NewUserService() *UserService {
  92. return &UserService{
  93. // Initialize handlers
  94. }
  95. }
  96. func main() {
  97. mr := jsonrpc.NewMethodRepository()
  98. for _, s := range []Servicer{NewUserService()} {
  99. for _, h := range s.Handlers() {
  100. mr.RegisterMethod(s.MethodName(h), h, h.Params(), h.Result())
  101. }
  102. }
  103. http.Handle("/jrpc", mr)
  104. http.HandleFunc("/jrpc/debug", mr.ServeDebug)
  105. if err := http.ListenAndServe(":8080", http.DefaultServeMux); err != nil {
  106. log.Fatalln(err)
  107. }
  108. }
  109. ```
  110. ### Result
  111. #### Invoke the Echo method
  112. ```
  113. POST /jrpc HTTP/1.1
  114. Accept: application/json, */*
  115. Accept-Encoding: gzip, deflate
  116. Connection: keep-alive
  117. Content-Length: 82
  118. Content-Type: application/json
  119. Host: localhost:8080
  120. User-Agent: HTTPie/0.9.6
  121. {
  122. "jsonrpc": "2.0",
  123. "method": "Main.Echo",
  124. "params": {
  125. "name": "John Doe"
  126. },
  127. "id": "243a718a-2ebb-4e32-8cc8-210c39e8a14b"
  128. }
  129. HTTP/1.1 200 OK
  130. Content-Length: 68
  131. Content-Type: application/json
  132. Date: Mon, 28 Nov 2016 13:48:13 GMT
  133. {
  134. "jsonrpc": "2.0",
  135. "result": {
  136. "message": "Hello, John Doe"
  137. },
  138. "id": "243a718a-2ebb-4e32-8cc8-210c39e8a14b"
  139. }
  140. ```
  141. #### Access to debug handler
  142. ```
  143. GET /jrpc/debug HTTP/1.1
  144. Accept: */*
  145. Accept-Encoding: gzip, deflate
  146. Connection: keep-alive
  147. Host: localhost:8080
  148. User-Agent: HTTPie/0.9.6
  149. HTTP/1.1 200 OK
  150. Content-Length: 408
  151. Content-Type: application/json
  152. Date: Mon, 28 Nov 2016 13:56:24 GMT
  153. [
  154. {
  155. "handler": "EchoHandler",
  156. "name": "Main.Echo",
  157. "params": {
  158. "$ref": "#/definitions/EchoParams",
  159. "$schema": "http://json-schema.org/draft-04/schema#",
  160. "definitions": {
  161. "EchoParams": {
  162. "additionalProperties": false,
  163. "properties": {
  164. "name": {
  165. "type": "string"
  166. }
  167. },
  168. "required": [
  169. "name"
  170. ],
  171. "type": "object"
  172. }
  173. }
  174. },
  175. "result": {
  176. "$ref": "#/definitions/EchoResult",
  177. "$schema": "http://json-schema.org/draft-04/schema#",
  178. "definitions": {
  179. "EchoResult": {
  180. "additionalProperties": false,
  181. "properties": {
  182. "message": {
  183. "type": "string"
  184. }
  185. },
  186. "required": [
  187. "message"
  188. ],
  189. "type": "object"
  190. }
  191. }
  192. }
  193. }
  194. ]
  195. ```
  196. ## License
  197. Released under the [MIT License](https://github.com/osamingo/jsonrpc/blob/master/LICENSE).