mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 12:32:21 +00:00
35 lines
670 B
Go
35 lines
670 B
Go
package main
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/rpc"
|
|
"net/rpc/jsonrpc"
|
|
)
|
|
|
|
type HelloService struct{}
|
|
|
|
func (p *HelloService) Hello(request string, reply *string) error {
|
|
*reply = "hello:" + request
|
|
return nil
|
|
}
|
|
|
|
func main() {
|
|
rpc.RegisterName("HelloService", new(HelloService))
|
|
|
|
// curl localhost:1234/jsonrpc --data '{"method":"HelloService.Hello","params":["hello"],"id":0}'
|
|
http.HandleFunc("/jsonrpc", func(w http.ResponseWriter, r *http.Request) {
|
|
var conn io.ReadWriteCloser = struct {
|
|
io.Writer
|
|
io.ReadCloser
|
|
}{
|
|
ReadCloser: r.Body,
|
|
Writer: w,
|
|
}
|
|
|
|
rpc.ServeRequest(jsonrpc.NewServerCodec(conn))
|
|
})
|
|
|
|
http.ListenAndServe(":1234", nil)
|
|
}
|