Improve binapi generator
[govpp.git] / binapi / trace / trace_rest.ba.go
1 // Code generated by GoVPP's binapi-generator. DO NOT EDIT.
2
3 package trace
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("/trace_profile_add", func(w http.ResponseWriter, req *http.Request) {
14                 var request = new(TraceProfileAdd)
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.TraceProfileAdd(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("/trace_profile_del", func(w http.ResponseWriter, req *http.Request) {
37                 var request = new(TraceProfileDel)
38                 reply, err := rpc.TraceProfileDel(req.Context(), request)
39                 if err != nil {
40                         http.Error(w, "request failed: "+err.Error(), http.StatusInternalServerError)
41                         return
42                 }
43                 rep, err := json.MarshalIndent(reply, "", "  ")
44                 if err != nil {
45                         http.Error(w, "marshal failed: "+err.Error(), http.StatusInternalServerError)
46                         return
47                 }
48                 w.Write(rep)
49         })
50         mux.HandleFunc("/trace_profile_show_config", func(w http.ResponseWriter, req *http.Request) {
51                 var request = new(TraceProfileShowConfig)
52                 reply, err := rpc.TraceProfileShowConfig(req.Context(), request)
53                 if err != nil {
54                         http.Error(w, "request failed: "+err.Error(), http.StatusInternalServerError)
55                         return
56                 }
57                 rep, err := json.MarshalIndent(reply, "", "  ")
58                 if err != nil {
59                         http.Error(w, "marshal failed: "+err.Error(), http.StatusInternalServerError)
60                         return
61                 }
62                 w.Write(rep)
63         })
64         return http.HandlerFunc(mux.ServeHTTP)
65 }