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 83 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/Riftbit/ALS-Go
  16. github.com/aerospike-labs/minion
  17. github.com/aerospike-labs/stock-exchange/exchange
  18. github.com/ajsd/goblogfe/blogfe
  19. github.com/akenn/fiestaturtle/brian
  20. github.com/alpacahq/marketstore/frontend
  21. github.com/alpacahq/marketstore/utils/rpc/msgpack2
  22. github.com/apcera/kurma/pkg/apiproxy
  23. github.com/apcera/kurma/pkg/daemon
  24. github.com/arunjit/gonotes/notes
  25. github.com/arunjit/gourls
  26. github.com/arunjitsingh/goblogfe/blogfe
  27. github.com/arunjitsingh/jsonrpc-example/server
  28. github.com/arunjitsingh/rpc/v2/json2
  29. github.com/chxj1992/go-jsonrpc/jsonrpc
  30. github.com/chyeh/open-falcon-backend/modules/query/test
  31. github.com/chyeh/query/test
  32. github.com/crosserclaws/open-falcon-backend/modules/query/test
  33. github.com/daniloanp/minio
  34. github.com/djinn/minio
  35. github.com/dnp1/minio
  36. github.com/docker/infrakit/pkg/rpc/server
  37. github.com/domino14/macondo
  38. github.com/dradtke/gob-rpc
  39. github.com/fwessels/minio-xl
  40. github.com/gautamrege/minio
  41. github.com/gohttp/rpc/v2/json
  42. github.com/gohttp/rpc/v2/json2
  43. github.com/gohttp/rpc/v2/json2/testapp
  44. github.com/gohttp/rpc/v2/protorpc
  45. github.com/gorilla/rpc/v2/json
  46. github.com/gorilla/rpc/v2/json2
  47. github.com/gorilla/rpc/v2/json2/testapp
  48. github.com/gorilla/rpc/v2/protorpc
  49. github.com/hackintoshrao/minio
  50. github.com/hlandau/degoutils/net/rpcnexus
  51. github.com/igor-k/minio
  52. github.com/jesselucas/minio
  53. github.com/kahing/minio/pkg/controller/rpc
  54. github.com/kdar/httprpc/soap
  55. github.com/levenlabs/gatewayrpc
  56. github.com/levenlabs/gatewayrpc/gateway
  57. github.com/levenlabs/golib/genapi
  58. github.com/levenlabs/golib/rpcutil
  59. github.com/matthewdale/matthewrdale.com/cmd
  60. github.com/mattyr/minio
  61. github.com/mediocregopher/fiestaturtle/brian
  62. github.com/mediocregopher/fiestaturtle/richard/richard
  63. github.com/minio/attic/minio-xl
  64. github.com/minio/minio/cmd
  65. github.com/ncw/minio
  66. github.com/ranveerkunal/uac
  67. github.com/riftbit/ALS-Go
  68. github.com/rjeczalik/rpc/v2/json
  69. github.com/rjeczalik/rpc/v2/json2
  70. github.com/rjeczalik/rpc/v2/json2/testapp
  71. github.com/rjeczalik/rpc/v2/protorpc
  72. github.com/segmentio/gorilla-rpc/v2/json
  73. github.com/segmentio/gorilla-rpc/v2/json2/testapp
  74. github.com/segmentio/gorilla-rpc/v2/protorpc
  75. github.com/shashidharatd/minio
  76. github.com/smothiki/minio.io
  77. github.com/sreeram-boyapati/minio
  78. github.com/systems-united-navy/tspm
  79. github.com/tj/go-hpc
  80. github.com/uwedeportivo/romba/cmds/rombaserver
  81. github.com/vishwasnavadak/minio
  82. github.com/yml/degoutils/net/rpcnexus
  83. gopkg.in/matm/scytale.v1/bin/magma

Imported only in test by 9 package(s)

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