-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc.go
36 lines (32 loc) · 794 Bytes
/
rpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
)
type JSONRPCRequest struct {
ID int `json:"id"`
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params []interface{} `json:"params"`
}
type JSONRPCResponse struct {
ID int `json:"id"`
JSONRPC string `json:"jsonrpc"`
Result interface{} `json:"result"`
}
func (r *JSONRPCResponse) Write(w http.ResponseWriter) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(r); err != nil {
log.Fatal(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
data := buf.Bytes()
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", fmt.Sprint(len(data)))
w.WriteHeader(http.StatusOK)
w.Write(data)
}