top

Package gorilla/rpc is a foundation for RPC over HTTP services, providing access to the exported methods of an object through HTTP requests.

This package derives from the standard net/rpc package but uses a single HTTP request per call instead of persistent connections. Other differences compared to net/rpc:

- Multiple codecs can be registered in the same server.
- A codec is chosen based on the "Content-Type" header from the request.
- Service methods also receive http.Request as parameter.
- This package can be used on Google App Engine.

Let's setup a server and register a codec and service:

import (
	"http"
	"github.com/gorilla/rpc/v2"
	"github.com/gorilla/rpc/v2/json"
)

func init() {
	s := rpc.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")
	s.RegisterService(new(HelloService), "")
	http.Handle("/rpc", s)
}

This server handles requests to the "/rpc" path using a JSON codec. A codec is tied to a content type. In the example above, the JSON codec is registered to serve requests with "application/json" as the value for the "Content-Type" header. If the header includes a charset definition, it is ignored; only the media-type part is taken into account.

A service can be registered using a name. If the name is empty, like in the example above, it will be inferred from the service type.

That's all about the server setup. Now let's define a simple service:

type HelloArgs struct {
	Who string
}

type HelloReply struct {
	Message string
}

type HelloService struct {}

func (h *HelloService) Say(r *http.Request, args *HelloArgs, reply *HelloReply) error {
	reply.Message = "Hello, " + args.Who + "!"
	return nil
}

The example above defines a service with a method "HelloService.Say" and the arguments and reply related to that method.

The service must be exported (begin with an upper case letter) or local (defined in the package registering the service).

When a service is registered, the server inspects the service methods and make available the ones that follow these rules:

- The method name is exported.
- The method has three arguments: *http.Request, *args, *reply.
- All three arguments are pointers.
- The second and third arguments are exported or local.
- The method has return type error.

All other methods are ignored.

Gorilla has packages with common RPC codecs. Check out their documentation:

JSON: http://gorilla-web.appspot.com/pkg/rpc/json

Imported by 76 package(s)

  1. github.com/4freewifi/smshinet/smshinetd
  2. github.com/Cepave/open-falcon-backend/modules/query/test
  3. github.com/Cepave/query/test
  4. github.com/FraBle/SocialVibes-AppEngine/socialvibes
  5. github.com/FraBle/SocialVibes-ComputeEngine
  6. github.com/Igor-K/minio
  7. github.com/Impress-semirding/goTalk
  8. github.com/LeKovr/elsa
  9. github.com/LeKovr/elsa/mw/rpc
  10. github.com/LevenLabs/gatewayrpc
  11. github.com/LevenLabs/gatewayrpc/gateway
  12. github.com/LevenLabs/golib/genapi
  13. github.com/LevenLabs/golib/rpcutil
  14. github.com/RiftBit/ALS-Go
  15. github.com/aerospike-labs/minion
  16. github.com/aerospike-labs/stock-exchange/exchange
  17. github.com/ajsd/goblogfe/blogfe
  18. github.com/akenn/fiestaturtle/brian
  19. github.com/apcera/kurma/pkg/apiproxy
  20. github.com/apcera/kurma/pkg/daemon
  21. github.com/arunjit/gonotes/notes
  22. github.com/arunjit/gourls
  23. github.com/arunjitsingh/goblogfe/blogfe
  24. github.com/arunjitsingh/jsonrpc-example/server
  25. github.com/arunjitsingh/rpc/v2/json2
  26. github.com/chxj1992/go-jsonrpc/jsonrpc
  27. github.com/chyeh/open-falcon-backend/modules/query/test
  28. github.com/chyeh/query/test
  29. github.com/crosserclaws/open-falcon-backend/modules/query/test
  30. github.com/daniloanp/minio
  31. github.com/djinn/minio
  32. github.com/domino14/macondo
  33. github.com/dradtke/gob-rpc
  34. github.com/fwessels/minio-xl
  35. github.com/gautamrege/minio
  36. github.com/gohttp/rpc/v2/json
  37. github.com/gohttp/rpc/v2/json2
  38. github.com/gohttp/rpc/v2/json2/testapp
  39. github.com/gohttp/rpc/v2/protorpc
  40. github.com/gorilla/rpc/v2/json
  41. github.com/gorilla/rpc/v2/json2
  42. github.com/gorilla/rpc/v2/json2/testapp
  43. github.com/gorilla/rpc/v2/protorpc
  44. github.com/hackintoshrao/minio
  45. github.com/hlandau/degoutils/net/rpcnexus
  46. github.com/igor-k/minio
  47. github.com/jesselucas/minio
  48. github.com/kahing/minio/pkg/controller/rpc
  49. github.com/kdar/httprpc/soap
  50. github.com/levenlabs/gatewayrpc
  51. github.com/levenlabs/gatewayrpc/gateway
  52. github.com/levenlabs/golib/genapi
  53. github.com/levenlabs/golib/rpcutil
  54. github.com/mattyr/minio
  55. github.com/mediocregopher/fiestaturtle/brian
  56. github.com/mediocregopher/fiestaturtle/richard/richard
  57. github.com/minio/attic/minio-xl
  58. github.com/minio/minio/cmd
  59. github.com/ncw/minio
  60. github.com/ranveerkunal/uac
  61. github.com/rjeczalik/rpc/v2/json
  62. github.com/rjeczalik/rpc/v2/json2
  63. github.com/rjeczalik/rpc/v2/json2/testapp
  64. github.com/rjeczalik/rpc/v2/protorpc
  65. github.com/segmentio/gorilla-rpc/v2/json
  66. github.com/segmentio/gorilla-rpc/v2/json2/testapp
  67. github.com/segmentio/gorilla-rpc/v2/protorpc
  68. github.com/shashidharatd/minio
  69. github.com/smothiki/minio.io
  70. github.com/sreeram-boyapati/minio
  71. github.com/systems-united-navy/tspm
  72. github.com/tj/go-hpc
  73. github.com/uwedeportivo/romba/cmds/rombaserver
  74. github.com/vishwasnavadak/minio
  75. github.com/yml/degoutils/net/rpcnexus
  76. gopkg.in/matm/scytale.v1/bin/magma

Imported only in test by 8 package(s)

  1. github.com/Cepave/open-falcon-backend/modules/query/nqm
  2. github.com/Cepave/query/nqm
  3. github.com/chyeh/open-falcon-backend/modules/query/nqm
  4. github.com/chyeh/query/nqm
  5. github.com/crosserclaws/open-falcon-backend/modules/query/nqm
  6. github.com/frankbraun/mute/util/jsonclient
  7. github.com/mutecomm/mute/util/jsonclient
  8. gitlab.cern.ch/flutter/http-jsonrpc