/
routes.go
134 lines (108 loc) · 3.08 KB
/
routes.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package dashboard
import (
"fmt"
"io"
"net/http"
"os"
"github.com/cockroachdb/errors"
"github.com/labstack/echo"
"github.com/markbates/pkger"
)
// ErrInvalidParameter defines the invalid parameter error.
var ErrInvalidParameter = errors.New("invalid parameter")
// ErrInternalError defines the internal error.
var ErrInternalError = errors.New("internal error")
// ErrNotFound defines the not found error.
var ErrNotFound = errors.New("not found")
// ErrForbidden defines the forbidden error.
var ErrForbidden = errors.New("forbidden")
const (
app = "/plugins/dashboard/frontend/build"
assets = "/plugins/dashboard/frontend/src/assets"
)
func indexRoute(e echo.Context) error {
if Parameters.Dev {
req, err := http.NewRequestWithContext(e.Request().Context(), "GET", "http://127.0.0.1:9090/", nil /* body */)
if err != nil {
return err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
devIndexHTML, err := io.ReadAll(res.Body)
if err != nil {
return err
}
return e.HTMLBlob(http.StatusOK, devIndexHTML)
}
index, err := pkger.Open(app + "/index.html")
if err != nil {
return err
}
defer index.Close()
indexHTML, err := io.ReadAll(index)
if err != nil {
return err
}
return e.HTMLBlob(http.StatusOK, indexHTML)
}
func setupRoutes(e *echo.Echo) {
if Parameters.Dev {
e.Static("/assets", "./plugins/dashboard/frontend/src/assets")
} else {
// load assets from pkger: either from within the binary or actual disk
pkger.Walk(app, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
e.GET("/app/"+info.Name(), echo.WrapHandler(http.StripPrefix("/app", http.FileServer(pkger.Dir(app)))))
return nil
})
pkger.Walk(assets, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
e.GET("/assets/"+info.Name(), echo.WrapHandler(http.StripPrefix("/assets", http.FileServer(pkger.Dir(assets)))))
return nil
})
}
e.GET("/ws", websocketRoute)
e.GET("/", indexRoute)
// used to route into the dashboard index
e.GET("*", indexRoute)
apiRoutes := e.Group("/api")
setupExplorerRoutes(apiRoutes)
setupVisualizerRoutes(apiRoutes)
e.HTTPErrorHandler = func(err error, c echo.Context) {
log.Warnf("Request failed: %s", err)
var statusCode int
var message string
switch errors.Unwrap(err) {
case echo.ErrNotFound:
c.Redirect(http.StatusSeeOther, "/")
return
case echo.ErrUnauthorized:
statusCode = http.StatusUnauthorized
message = "unauthorized"
case ErrForbidden:
statusCode = http.StatusForbidden
message = "access forbidden"
case ErrInternalError:
statusCode = http.StatusInternalServerError
message = "internal server error"
case ErrNotFound:
statusCode = http.StatusNotFound
message = "not found"
case ErrInvalidParameter:
statusCode = http.StatusBadRequest
message = "bad request"
default:
statusCode = http.StatusInternalServerError
message = "internal server error"
}
message = fmt.Sprintf("%s, error: %+v", message, err)
c.String(statusCode, message)
}
}