/
handler.go
114 lines (109 loc) · 3.15 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package handler
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"text/template"
"time"
"github.com/labstack/echo"
"github.com/leekchan/gtf"
"github.com/patrickmn/go-cache"
"github.com/takutakahashi/container-api-gateway/pkg/types"
)
type Response struct {
Stdout string `json:"stdout" xml:"stdout"`
Stderr string `json:"stderr" xml:"stderr"`
}
// GetHandler generate handler from endpoint
func GetHandler(endpoint types.Endpoint, b types.BaseBackend) echo.HandlerFunc {
return func(c echo.Context) error {
params := make([]types.Param, len(endpoint.Params))
for i, param := range endpoint.Params {
p := c.FormValue(param.Name)
if p == "" && !param.Optional {
return c.String(http.StatusBadRequest, "required param "+param.Name+" was not found.")
}
// if p is null, use default
if p != "" {
param.Value = p
}
params[i] = param
}
endpoint.Params = params
if endpoint.Cache.Store == nil {
endpoint.Cache.Store = cache.New(endpoint.Cache.Expire*time.Minute, endpoint.Cache.Expire*time.Minute)
}
var stdout *bytes.Buffer
var err error
if endpoint.Cache.Enabled {
fmt.Println("try to get from cache")
var found bool
stdout, found = endpoint.Cache.GetStdout(fmt.Sprintf("%v", endpoint.Params))
fmt.Println(found)
if found {
err = nil
} else {
stdout, _, err = b.Execute(endpoint)
if err == nil {
endpoint.Cache.SetStdout(fmt.Sprintf("%v", endpoint.Params), stdout)
}
}
} else {
stdout, _, err = b.Execute(endpoint)
}
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
} else {
var o interface{}
if stdout != nil {
in := bytes.Replace(stdout.Bytes(), []byte("'"), []byte("\""), -1)
fmt.Println(string(in))
err = json.Unmarshal(in, &o)
fmt.Println(o)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, o)
} else {
return c.String(http.StatusOK, endpoint.Response)
}
}
}
}
func GetFormHandler(baseURL string, endpoint types.Endpoint) echo.HandlerFunc {
return func(c echo.Context) error {
type s struct {
Endpoint types.Endpoint
Base string
}
var buf bytes.Buffer
if endpoint.TemplateURL != "" {
fmt.Println("use external template")
resp, err := http.Get(endpoint.TemplateURL)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
b := new(bytes.Buffer)
b.ReadFrom(resp.Body)
tmpl, err := template.New("form.html").Funcs(gtf.GtfTextFuncMap).Parse(b.String())
err = tmpl.Execute(&buf, s{Endpoint: endpoint, Base: baseURL})
if err != nil {
log.Println("executing error")
return c.String(http.StatusInternalServerError, err.Error())
}
} else {
tmpl, err := template.New("form.html").Funcs(gtf.GtfTextFuncMap).ParseFiles("./src/template/form.html")
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
err = tmpl.Execute(&buf, s{Endpoint: endpoint, Base: baseURL})
if err != nil {
log.Println("executing error")
return c.String(http.StatusInternalServerError, err.Error())
}
}
return c.HTML(http.StatusOK, buf.String())
}
}