1
0
mirror of https://github.com/gusaul/grpcox.git synced 2024-12-26 10:50:11 +00:00
grpcox/handler/handler.go

242 lines
4.7 KiB
Go
Raw Permalink Normal View History

2018-11-04 19:56:06 +00:00
package handler
import (
"bytes"
"context"
"fmt"
feat(descriptor): add support for local proto descriptor 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
2020-01-29 12:14:41 +00:00
"io/ioutil"
2018-11-04 19:56:06 +00:00
"net/http"
"strconv"
2019-03-13 03:38:30 +00:00
"strings"
2018-11-04 19:56:06 +00:00
"github.com/gorilla/mux"
"github.com/gusaul/grpcox/core"
)
2019-03-13 03:38:30 +00:00
// 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) {
2018-11-04 19:56:06 +00:00
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())
}
2019-03-13 03:38:30 +00:00
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) {
2018-11-04 19:56:06 +00:00
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"))
2019-03-13 03:38:30 +00:00
restart, _ := strconv.ParseBool(r.FormValue("restart"))
2018-11-04 19:56:06 +00:00
2019-03-13 03:38:30 +00:00
res, err := h.g.GetResource(context.Background(), host, !useTLS, restart)
2018-11-04 19:56:06 +00:00
if err != nil {
writeError(w, err)
return
}
result, err := res.List(service)
if err != nil {
writeError(w, err)
return
}
h.g.Extend(host)
2018-11-04 19:56:06 +00:00
response(w, result)
}
feat(descriptor): add support for local proto descriptor 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
2020-01-29 12:14:41 +00:00
// 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)
}
2019-03-13 03:38:30 +00:00
func (h *Handler) describeFunction(w http.ResponseWriter, r *http.Request) {
2018-11-04 19:56:06 +00:00
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"))
2019-03-13 03:38:30 +00:00
res, err := h.g.GetResource(context.Background(), host, !useTLS, false)
2018-11-04 19:56:06 +00:00
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)
2018-11-04 19:56:06 +00:00
response(w, desc{
Schema: result,
Template: template,
})
}
2019-03-13 03:38:30 +00:00
func (h *Handler) invokeFunction(w http.ResponseWriter, r *http.Request) {
2018-11-04 19:56:06 +00:00
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"))
2019-03-13 03:38:30 +00:00
res, err := h.g.GetResource(context.Background(), host, !useTLS, false)
2018-11-04 19:56:06 +00:00
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)
2018-11-04 19:56:06 +00:00
response(w, invRes{
Time: timer.String(),
Result: result,
})
}