1package handler 2 3import ( 4 "bytes" 5 "context" 6 "fmt" 7 "net/http" 8 "strconv" 9 "strings" 10 11 "github.com/gorilla/mux" 12 "github.com/gusaul/grpcox/core" 13) 14 15// Handler hold all handler methods 16type Handler struct { 17 g *core.GrpCox 18} 19 20// InitHandler Constructor 21func InitHandler() *Handler { 22 return &Handler{ 23 g: core.InitGrpCox(), 24 } 25} 26 27func (h *Handler) index(w http.ResponseWriter, r *http.Request) { 28 body := new(bytes.Buffer) 29 err := indexHTML.Execute(body, make(map[string]string)) 30 if err != nil { 31 writeError(w, err) 32 return 33 } 34 w.WriteHeader(http.StatusOK) 35 w.Header().Set("Content-Type", "text/html; charset=utf-8") 36 w.Write(body.Bytes()) 37} 38 39func (h *Handler) getActiveConns(w http.ResponseWriter, r *http.Request) { 40 response(w, h.g.GetActiveConns(context.TODO())) 41} 42 43func (h *Handler) closeActiveConns(w http.ResponseWriter, r *http.Request) { 44 vars := mux.Vars(r) 45 host := vars["host"] 46 if host == "" { 47 writeError(w, fmt.Errorf("Invalid Host")) 48 return 49 } 50 51 err := h.g.CloseActiveConns(strings.Trim(host, " ")) 52 if err != nil { 53 writeError(w, err) 54 return 55 } 56 response(w, map[string]bool{"success": true}) 57} 58 59func (h *Handler) getLists(w http.ResponseWriter, r *http.Request) { 60 vars := mux.Vars(r) 61 host := vars["host"] 62 if host == "" { 63 writeError(w, fmt.Errorf("Invalid Host")) 64 return 65 } 66 67 service := vars["serv_name"] 68 69 useTLS, _ := strconv.ParseBool(r.Header.Get("use_tls")) 70 restart, _ := strconv.ParseBool(r.FormValue("restart")) 71 72 res, err := h.g.GetResource(context.Background(), host, !useTLS, restart) 73 if err != nil { 74 writeError(w, err) 75 return 76 } 77 78 result, err := res.List(service) 79 if err != nil { 80 writeError(w, err) 81 return 82 } 83 84 h.g.Extend(host) 85 response(w, result) 86} 87 88func (h *Handler) describeFunction(w http.ResponseWriter, r *http.Request) { 89 vars := mux.Vars(r) 90 host := vars["host"] 91 if host == "" { 92 writeError(w, fmt.Errorf("Invalid Host")) 93 return 94 } 95 96 funcName := vars["func_name"] 97 if host == "" { 98 writeError(w, fmt.Errorf("Invalid Func Name")) 99 return 100 } 101 102 useTLS, _ := strconv.ParseBool(r.Header.Get("use_tls")) 103 104 res, err := h.g.GetResource(context.Background(), host, !useTLS, false) 105 if err != nil { 106 writeError(w, err) 107 return 108 } 109 110 // get param 111 result, _, err := res.Describe(funcName) 112 if err != nil { 113 writeError(w, err) 114 return 115 } 116 match := reGetFuncArg.FindStringSubmatch(result) 117 if len(match) < 2 { 118 writeError(w, fmt.Errorf("Invalid Func Type")) 119 return 120 } 121 122 // describe func 123 result, template, err := res.Describe(match[1]) 124 if err != nil { 125 writeError(w, err) 126 return 127 } 128 129 type desc struct { 130 Schema string `json:"schema"` 131 Template string `json:"template"` 132 } 133 134 h.g.Extend(host) 135 response(w, desc{ 136 Schema: result, 137 Template: template, 138 }) 139 140} 141 142func (h *Handler) invokeFunction(w http.ResponseWriter, r *http.Request) { 143 vars := mux.Vars(r) 144 host := vars["host"] 145 if host == "" { 146 writeError(w, fmt.Errorf("Invalid Host")) 147 return 148 } 149 150 funcName := vars["func_name"] 151 if host == "" { 152 writeError(w, fmt.Errorf("Invalid Func Name")) 153 return 154 } 155 156 useTLS, _ := strconv.ParseBool(r.Header.Get("use_tls")) 157 158 res, err := h.g.GetResource(context.Background(), host, !useTLS, false) 159 if err != nil { 160 writeError(w, err) 161 return 162 } 163 164 // get param 165 result, timer, err := res.Invoke(context.Background(), funcName, r.Body) 166 if err != nil { 167 writeError(w, err) 168 return 169 } 170 171 type invRes struct { 172 Time string `json:"timer"` 173 Result string `json:"result"` 174 } 175 176 h.g.Extend(host) 177 response(w, invRes{ 178 Time: timer.String(), 179 Result: result, 180 }) 181} 182