-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
59 lines (53 loc) · 1.13 KB
/
main.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
jsonparse "encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
//Args comment
type Args struct {
ID string
}
//Book comment
type Book struct {
ID string `"json:"string,omitempty"`
Name string `"json:name,omitempty"`
Author string `"json:author,omitempty"`
}
// JSONServer struct
type JSONServer struct{}
//GiveBookDetail comment
func (t *JSONServer) GiveBookDetail(r *http.Request, args *Args, reply *Book) error {
var books []Book
raw, readerr := ioutil.ReadFile("./books.json")
if readerr != nil {
log.Println("error:", readerr)
os.Exit(1)
}
marshalerr := jsonparse.Unmarshal(raw, &books)
if marshalerr != nil {
log.Println("error:", marshalerr)
os.Exit(1)
}
// Iterate over JSON data to find the given book
for _, book := range books {
if book.ID == args.ID {
*reply = book
break
}
}
return nil
}
func main() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(JSONServer), "")
r := mux.NewRouter()
r.Handle("/rpc", s)
http.ListenAndServe(":1234", r)
}