Browse Source

Example 3: JSON-RPC 1

Frederic G. MARAND 11 years ago
parent
commit
c894f4a9b2
1 changed files with 62 additions and 0 deletions
  1. 62 0
      ch3/ex3rpc.go

+ 62 - 0
ch3/ex3rpc.go

@@ -0,0 +1,62 @@
+package main
+
+/*
+Sample request content:
+	Method: POST
+	Content-type: application/json -> without it JSON-RPC returns a 415.
+	Accept header is not needed.
+
+{
+  "method": "StringService.Length",
+  "params" : [
+    {
+      "Message": "Hello, world"
+    }
+  ],
+  "id": 1
+}
+
+Sample Curl usage:
+
+curl -X POST -H "Content-type: application/json" -d '{"method": "StringService.Length", "params": [{"Message": "Hello, world"}], "Id": 1}' http://localhost:8080/rpc
+
+ */
+
+import (
+	"net/http"
+
+	"github.com/gorilla/rpc/v2"
+	"github.com/gorilla/rpc/v2/json"
+
+	"fmt"
+	"unicode/utf8"
+	"github.com/davecgh/go-spew/spew"
+)
+
+type RPCApiArguments struct {
+	Message string
+}
+
+type RPCAPIResponse struct {
+	Message string
+}
+
+type StringService struct{}
+
+func (s *StringService) Length(q *http.Request, args *RPCApiArguments, r *RPCAPIResponse) error {
+	r.Message = fmt.Sprintf("Your request [%s] is %d characters long",
+		args.Message,
+		utf8.RuneCountInString(args.Message))
+	spew.Dump(r)
+	return nil
+}
+
+func main() {
+	address := ":8080"
+	fmt.Printf("API started on %s\n", address)
+	s := rpc.NewServer()
+	s.RegisterCodec(json.NewCodec(), "application/json")
+	s.RegisterService(new(StringService), "")
+	http.Handle("/rpc", s)
+	http.ListenAndServe(address, nil)
+}

PANIC: session(release): write data/sessions/5/7/578bc143fddafc07: no space left on device

PANIC

session(release): write data/sessions/5/7/578bc143fddafc07: no space left on device
/my/cache/.heroku/go/go-path/pkg/mod/github.com/go-macaron/session@v1.0.3/session.go:204 (0xb13e07)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/context.go:80 (0x967b75)
/my/cache/.heroku/go/go-path/pkg/mod/github.com/go-macaron/inject@v0.0.0-20200308113650-138e5925c53b/inject.go:157 (0x9512ee)
/my/cache/.heroku/go/go-path/pkg/mod/github.com/go-macaron/inject@v0.0.0-20200308113650-138e5925c53b/inject.go:135 (0x951205)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/context.go:124 (0x967cc4)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/context.go:114 (0x967bf6)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/recovery.go:161 (0x15baec4)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/logger.go:40 (0x96b257)
/my/cache/.heroku/go/go-path/pkg/mod/github.com/go-macaron/inject@v0.0.0-20200308113650-138e5925c53b/inject.go:157 (0x9512ee)
/my/cache/.heroku/go/go-path/pkg/mod/github.com/go-macaron/inject@v0.0.0-20200308113650-138e5925c53b/inject.go:135 (0x951205)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/context.go:124 (0x967cc4)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/router.go:187 (0x972959)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/router.go:304 (0x973a01)
/my/cache/.heroku/go/go-path/pkg/mod/gopkg.in/macaron.v1@v1.5.1/macaron.go:218 (0x96c572)
/my/cache/.heroku/go/go1.26.3/go/src/net/http/server.go:3311 (0x85a5cd)
/my/cache/.heroku/go/go1.26.3/go/src/net/http/server.go:2073 (0x837f6f)
/my/cache/.heroku/go/go1.26.3/go/src/runtime/asm_amd64.s:1771 (0x493380)