diff --git a/.idea/jsonrpc.iml b/.idea/jsonrpc.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/jsonrpc.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..80a5c51 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..1779de5 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + \ No newline at end of file diff --git a/client_test.go b/client_test.go index 753956e..860553b 100644 --- a/client_test.go +++ b/client_test.go @@ -127,6 +127,23 @@ func BenchmarkClientSync(b *testing.B) { func startServer(t *testing.T, counter *state) { s := NewServer() + + s.HandleFunc("sum", sum) + s.HandleFunc("random", random) + s.HandleFunc("counter", counter.increaseCounter) + + if err := http.ListenAndServe(port, s); err != nil { + t.Errorf("starting server: %v", err) + } +} + +func startServerAddCORS(t *testing.T, counter *state) { + s := NewServer() + s.Cors = map[string]string{ + "Access-Control-Allow-Origin":"*", + "Access-Control-Allow-Methods":"POST,GET,OPTIONS", + "Access-Control-Allow-Headers":"Content-Type", + } s.HandleFunc("sum", sum) s.HandleFunc("random", random) s.HandleFunc("counter", counter.increaseCounter) diff --git a/server.go b/server.go index ccd17e1..c4128d3 100644 --- a/server.go +++ b/server.go @@ -24,6 +24,8 @@ var ( // Server represents a JSON-RPC server. type Server struct { handler sync.Map + // cors map + Cors map[string]string } type handlerType struct { @@ -95,6 +97,11 @@ func inspectHandler(h reflect.Value) (numArgs int, ptype, rtype reflect.Type, er // ServeHTTP responds to an JSON-RPC request and executes the requested method. func (s *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request) { + if len(s.Cors) >0{ + for k1, v1 := range s.Cors { + rw.Header().Add(k1,v1) + } + } // Only POST methods are jsonrpc valid calls if r.Method != "POST" { rw.WriteHeader(http.StatusNotFound)