-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagoMiddleware.go
97 lines (78 loc) · 2.13 KB
/
diagoMiddleware.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
package diago
import (
"bytes"
_ "embed"
"github.com/gin-gonic/gin"
"github.com/gouef/router"
"html/template"
"net/http"
)
type Data struct {
ExtensionsPanelHtml []template.HTML
ExtensionsJSHtml []template.HTML
ExtensionsHtml []template.HTML
}
func Middleware(r *router.Router, d *Diago) gin.HandlerFunc {
return func(c *gin.Context) {
if r != nil && r.IsRelease() {
c.Next()
return
}
originalWriter := c.Writer
for _, e := range d.GetExtensions() {
e.BeforeNext(c)
}
responseBuffer := &bytes.Buffer{}
writer := &responseWriter{
ResponseWriter: c.Writer,
buffer: responseBuffer,
statusCode: http.StatusOK,
}
c.Writer = writer
c.Next()
c.Copy()
for _, e := range d.GetExtensions() {
e.AfterNext(c)
}
contentType := writer.Header().Get("Content-Type")
if d.ContainsMIME(contentType) {
var extensionsHtml []template.HTML
var extensionsPanelHtml []template.HTML
var extensionsJSHtml []template.HTML
for _, e := range d.GetExtensions() {
extensionsHtml = append(extensionsHtml, template.HTML(e.GetHtml(c)))
extensionsPanelHtml = append(extensionsPanelHtml, template.HTML(e.GetPanelHtml(c)))
extensionsJSHtml = append(extensionsJSHtml, template.HTML(e.GetJSHtml(c)))
}
diagoData := Data{
ExtensionsHtml: extensionsHtml,
ExtensionsPanelHtml: extensionsPanelHtml,
ExtensionsJSHtml: extensionsJSHtml,
}
diagoPanelHTML, err := d.PanelGenerator.GenerateHTML("diagoPanel", d.TemplateProvider, diagoData)
if err != nil {
err = c.Error(err)
c.Status(500)
c.Writer.WriteHeaderNow()
diagoPanelHTML = "Error generating Diago panel HTML"
}
writer.buffer.WriteString(diagoPanelHTML)
}
status := c.Writer.Status()
c.Writer = originalWriter
c.Writer.Write(responseBuffer.Bytes())
c.Status(status)
}
}
type responseWriter struct {
gin.ResponseWriter
buffer *bytes.Buffer
statusCode int
}
func (w *responseWriter) Write(data []byte) (int, error) {
return w.buffer.Write(data)
}
func (w *responseWriter) WriteHeader(statusCode int) {
w.statusCode = statusCode
w.ResponseWriter.WriteHeader(statusCode)
}