mirror of
https://github.com/gusaul/grpcox.git
synced 2024-11-17 06:26:56 +00:00
293bb364c0
Currently, grpcox depends on server reflection to get proto descriptor. It has a significant drawback, since not every grpc server support [server reflection](https://github.com/grpc/grpc/blob/master/doc/server-reflection.md#known-implementations). Using local proto files is more feasible, as every grpc server certainly have one. Even though using protofile should be simple enough, there's still a problem regarding this. Some protofile use extra plugins for their proto. i.e. gogoprotobuf is a project that does just that. The problems with plugins are most of them require explicit import to the plugin inside of the protofile. It will break grpcurl proto descriptor extraction. Thus, the plugin proto must be uploaded alongside the protofile. Also, the protofile should be modified automatically to change their import to local import. Given that, I proposed a way for the user to upload multiple protofile to grpcox. Then, use that to get the descriptor. Changelog: - Add `use local proto` checkbox in HTML client. On checked it will show upload button and list of selected proto. - `get-service` ajax will use POST when `use local proto` is checked. The uploaded protofile will be the payload for the ajax request. - Add a new route to handle POST "get-service". It will persist the uploaded protofile to `/tmp/` directory and add protos field in the resource. - Modify `openDescriptor` to use local proto if protos field in the resource is available. - Modify `openDescriptor` to return an error, as opening descriptor from local proto may fail. - Modify the main server so it can be shut down gracefully. This is necessary as grpcox need to remove persisted proto right after the server is turned off. This Pull Request will resolve #16
242 lines
4.7 KiB
Go
242 lines
4.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/gusaul/grpcox/core"
|
|
)
|
|
|
|
// Handler hold all handler methods
|
|
type Handler struct {
|
|
g *core.GrpCox
|
|
}
|
|
|
|
// InitHandler Constructor
|
|
func InitHandler() *Handler {
|
|
return &Handler{
|
|
g: core.InitGrpCox(),
|
|
}
|
|
}
|
|
|
|
func (h *Handler) index(w http.ResponseWriter, r *http.Request) {
|
|
body := new(bytes.Buffer)
|
|
err := indexHTML.Execute(body, make(map[string]string))
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
w.Write(body.Bytes())
|
|
}
|
|
|
|
func (h *Handler) getActiveConns(w http.ResponseWriter, r *http.Request) {
|
|
response(w, h.g.GetActiveConns(context.TODO()))
|
|
}
|
|
|
|
func (h *Handler) closeActiveConns(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
host := vars["host"]
|
|
if host == "" {
|
|
writeError(w, fmt.Errorf("Invalid Host"))
|
|
return
|
|
}
|
|
|
|
err := h.g.CloseActiveConns(strings.Trim(host, " "))
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
response(w, map[string]bool{"success": true})
|
|
}
|
|
|
|
func (h *Handler) getLists(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
host := vars["host"]
|
|
if host == "" {
|
|
writeError(w, fmt.Errorf("Invalid Host"))
|
|
return
|
|
}
|
|
|
|
service := vars["serv_name"]
|
|
|
|
useTLS, _ := strconv.ParseBool(r.Header.Get("use_tls"))
|
|
restart, _ := strconv.ParseBool(r.FormValue("restart"))
|
|
|
|
res, err := h.g.GetResource(context.Background(), host, !useTLS, restart)
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
|
|
result, err := res.List(service)
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
|
|
h.g.Extend(host)
|
|
response(w, result)
|
|
}
|
|
|
|
// getListsWithProto handling client request for service list with proto
|
|
func (h *Handler) getListsWithProto(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
host := vars["host"]
|
|
if host == "" {
|
|
writeError(w, fmt.Errorf("Invalid Host"))
|
|
return
|
|
}
|
|
|
|
service := vars["serv_name"]
|
|
|
|
useTLS, _ := strconv.ParseBool(r.Header.Get("use_tls"))
|
|
restart, _ := strconv.ParseBool(r.FormValue("restart"))
|
|
|
|
// limit upload file to 5mb
|
|
err := r.ParseMultipartForm(5 << 20)
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
|
|
// convert uploaded files to list of Proto struct
|
|
files := r.MultipartForm.File["protos"]
|
|
protos := make([]core.Proto, 0, len(files))
|
|
for _, file := range files {
|
|
fileData, err := file.Open()
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
defer fileData.Close()
|
|
|
|
content, err := ioutil.ReadAll(fileData)
|
|
if err != nil {
|
|
writeError(w, err)
|
|
}
|
|
|
|
protos = append(protos, core.Proto{
|
|
Name: file.Filename,
|
|
Content: content,
|
|
})
|
|
}
|
|
|
|
res, err := h.g.GetResourceWithProto(context.Background(), host, !useTLS, restart, protos)
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
|
|
result, err := res.List(service)
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
|
|
h.g.Extend(host)
|
|
response(w, result)
|
|
}
|
|
|
|
func (h *Handler) describeFunction(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
host := vars["host"]
|
|
if host == "" {
|
|
writeError(w, fmt.Errorf("Invalid Host"))
|
|
return
|
|
}
|
|
|
|
funcName := vars["func_name"]
|
|
if host == "" {
|
|
writeError(w, fmt.Errorf("Invalid Func Name"))
|
|
return
|
|
}
|
|
|
|
useTLS, _ := strconv.ParseBool(r.Header.Get("use_tls"))
|
|
|
|
res, err := h.g.GetResource(context.Background(), host, !useTLS, false)
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
|
|
// get param
|
|
result, _, err := res.Describe(funcName)
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
match := reGetFuncArg.FindStringSubmatch(result)
|
|
if len(match) < 2 {
|
|
writeError(w, fmt.Errorf("Invalid Func Type"))
|
|
return
|
|
}
|
|
|
|
// describe func
|
|
result, template, err := res.Describe(match[1])
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
|
|
type desc struct {
|
|
Schema string `json:"schema"`
|
|
Template string `json:"template"`
|
|
}
|
|
|
|
h.g.Extend(host)
|
|
response(w, desc{
|
|
Schema: result,
|
|
Template: template,
|
|
})
|
|
|
|
}
|
|
|
|
func (h *Handler) invokeFunction(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
host := vars["host"]
|
|
if host == "" {
|
|
writeError(w, fmt.Errorf("Invalid Host"))
|
|
return
|
|
}
|
|
|
|
funcName := vars["func_name"]
|
|
if host == "" {
|
|
writeError(w, fmt.Errorf("Invalid Func Name"))
|
|
return
|
|
}
|
|
|
|
useTLS, _ := strconv.ParseBool(r.Header.Get("use_tls"))
|
|
|
|
res, err := h.g.GetResource(context.Background(), host, !useTLS, false)
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
|
|
// get param
|
|
result, timer, err := res.Invoke(context.Background(), funcName, r.Body)
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
|
|
type invRes struct {
|
|
Time string `json:"timer"`
|
|
Result string `json:"result"`
|
|
}
|
|
|
|
h.g.Extend(host)
|
|
response(w, invRes{
|
|
Time: timer.String(),
|
|
Result: result,
|
|
})
|
|
}
|