2018-11-04 19:56:06 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
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
|
|
|
|
}
|
|
|
|
|
2019-04-05 11:07:19 +00:00
|
|
|
h.g.Extend(host)
|
2018-11-04 19:56:06 +00:00
|
|
|
response(w, result)
|
|
|
|
}
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2019-04-05 11:07:19 +00:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2019-04-05 11:07:19 +00:00
|
|
|
h.g.Extend(host)
|
2018-11-04 19:56:06 +00:00
|
|
|
response(w, invRes{
|
|
|
|
Time: timer.String(),
|
|
|
|
Result: result,
|
|
|
|
})
|
|
|
|
}
|