Improve binapi generator
[govpp.git] / binapi / af_packet / af_packet_rest.ba.go
1 // Code generated by GoVPP's binapi-generator. DO NOT EDIT.
2
3 package af_packet
4
5 import (
6         "encoding/json"
7         "io/ioutil"
8         "net/http"
9 )
10
11 func RESTHandler(rpc RPCService) http.Handler {
12         mux := http.NewServeMux()
13         mux.HandleFunc("/af_packet_create", func(w http.ResponseWriter, req *http.Request) {
14                 var request = new(AfPacketCreate)
15                 b, err := ioutil.ReadAll(req.Body)
16                 if err != nil {
17                         http.Error(w, "read body failed", http.StatusBadRequest)
18                         return
19                 }
20                 if err := json.Unmarshal(b, request); err != nil {
21                         http.Error(w, "unmarshal data failed", http.StatusBadRequest)
22                         return
23                 }
24                 reply, err := rpc.AfPacketCreate(req.Context(), request)
25                 if err != nil {
26                         http.Error(w, "request failed: "+err.Error(), http.StatusInternalServerError)
27                         return
28                 }
29                 rep, err := json.MarshalIndent(reply, "", "  ")
30                 if err != nil {
31                         http.Error(w, "marshal failed: "+err.Error(), http.StatusInternalServerError)
32                         return
33                 }
34                 w.Write(rep)
35         })
36         mux.HandleFunc("/af_packet_delete", func(w http.ResponseWriter, req *http.Request) {
37                 var request = new(AfPacketDelete)
38                 b, err := ioutil.ReadAll(req.Body)
39                 if err != nil {
40                         http.Error(w, "read body failed", http.StatusBadRequest)
41                         return
42                 }
43                 if err := json.Unmarshal(b, request); err != nil {
44                         http.Error(w, "unmarshal data failed", http.StatusBadRequest)
45                         return
46                 }
47                 reply, err := rpc.AfPacketDelete(req.Context(), request)
48                 if err != nil {
49                         http.Error(w, "request failed: "+err.Error(), http.StatusInternalServerError)
50                         return
51                 }
52                 rep, err := json.MarshalIndent(reply, "", "  ")
53                 if err != nil {
54                         http.Error(w, "marshal failed: "+err.Error(), http.StatusInternalServerError)
55                         return
56                 }
57                 w.Write(rep)
58         })
59         mux.HandleFunc("/af_packet_set_l4_cksum_offload", func(w http.ResponseWriter, req *http.Request) {
60                 var request = new(AfPacketSetL4CksumOffload)
61                 b, err := ioutil.ReadAll(req.Body)
62                 if err != nil {
63                         http.Error(w, "read body failed", http.StatusBadRequest)
64                         return
65                 }
66                 if err := json.Unmarshal(b, request); err != nil {
67                         http.Error(w, "unmarshal data failed", http.StatusBadRequest)
68                         return
69                 }
70                 reply, err := rpc.AfPacketSetL4CksumOffload(req.Context(), request)
71                 if err != nil {
72                         http.Error(w, "request failed: "+err.Error(), http.StatusInternalServerError)
73                         return
74                 }
75                 rep, err := json.MarshalIndent(reply, "", "  ")
76                 if err != nil {
77                         http.Error(w, "marshal failed: "+err.Error(), http.StatusInternalServerError)
78                         return
79                 }
80                 w.Write(rep)
81         })
82         return http.HandlerFunc(mux.ServeHTTP)
83 }