Skip to content

Commit

Permalink
(feat) allow remote URLS for all charts
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Jan 3, 2024
1 parent b1ba057 commit 3036d4e
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

| Common Queries | Required | Description | Default |
| :------------- | :------- | :---------- | :---------------- |
| `data` || JSON | |
| `data` || JSON or URL | |
| `title` | | string | |
| `subtitle` | | string | |
| `theme` | | string | `light` or `dark` |
Expand Down
15 changes: 12 additions & 3 deletions pkg/bar_chart_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
)

type BarChartHandler struct {
chart *BarChart
chart *BarChart
allowedRemoteDomains string
}

func NewBarChartHandler() *BarChartHandler {
func NewBarChartHandler(allowedRemoteDomains string) *BarChartHandler {
return &BarChartHandler{
chart: NewBarChart(),
chart: NewBarChart(),
allowedRemoteDomains: allowedRemoteDomains,
}
}

Expand All @@ -28,6 +30,13 @@ func (h *BarChartHandler) Get(c echo.Context) ([]byte, error) {
if err := BindRequest(c, req); err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err)
}
err := SetDataIfRemoteURL(req, h.allowedRemoteDomains)
if err != nil {
msgs := map[string]string{
"data": err.Error(),
}
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, msgs)
}

var data BarChartData
if err := json.Unmarshal([]byte(req.ChartData), &data); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/bar_chart_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestGetBarChart(t *testing.T) {
e := echo.New()

e.GET("/bar", func(c echo.Context) error {
img, err := NewBarChartHandler().Get(c)
img, err := NewBarChartHandler("").Get(c)
if err != nil {
return err
}
Expand Down
15 changes: 12 additions & 3 deletions pkg/donut_chart_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
)

type DonutChartHandler struct {
chart *DonutChart
chart *DonutChart
allowedRemoteDomains string
}

func NewDonutChartHandler() *DonutChartHandler {
func NewDonutChartHandler(allowedRemoteDomains string) *DonutChartHandler {
return &DonutChartHandler{
chart: NewDonutChart(),
chart: NewDonutChart(),
allowedRemoteDomains: allowedRemoteDomains,
}
}

Expand All @@ -27,6 +29,13 @@ func (h *DonutChartHandler) Get(c echo.Context) ([]byte, error) {
if err := BindRequest(c, req); err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err)
}
err := SetDataIfRemoteURL(req, h.allowedRemoteDomains)
if err != nil {
msgs := map[string]string{
"data": err.Error(),
}
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, msgs)
}

var data DonutChartData
if err := json.Unmarshal([]byte(req.ChartData), &data); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/donut_chart_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestGetDonutChart(t *testing.T) {
e := echo.New()

e.GET("/donut", func(c echo.Context) error {
img, err := NewDonutChartHandler().Get(c)
img, err := NewDonutChartHandler("").Get(c)
if err != nil {
return err
}
Expand Down
15 changes: 12 additions & 3 deletions pkg/funnel_chart_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
)

type FunnelChartHandler struct {
chart *FunnelChart
chart *FunnelChart
allowedRemoteDomains string
}

func NewFunnelChartHandler() *FunnelChartHandler {
func NewFunnelChartHandler(allowedRemoteDomains string) *FunnelChartHandler {
return &FunnelChartHandler{
chart: NewFunnelChart(),
chart: NewFunnelChart(),
allowedRemoteDomains: allowedRemoteDomains,
}
}

Expand All @@ -27,6 +29,13 @@ func (h *FunnelChartHandler) Get(c echo.Context) ([]byte, error) {
if err := BindRequest(c, req); err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err)
}
err := SetDataIfRemoteURL(req, h.allowedRemoteDomains)
if err != nil {
msgs := map[string]string{
"data": err.Error(),
}
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, msgs)
}

var data FunnelChartData
if err := json.Unmarshal([]byte(req.ChartData), &data); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/funnel_chart_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestGetFunnelChart(t *testing.T) {
e := echo.New()

e.GET("/funnel", func(c echo.Context) error {
img, err := NewFunnelChartHandler().Get(c)
img, err := NewFunnelChartHandler("").Get(c)
if err != nil {
return err
}
Expand Down
15 changes: 12 additions & 3 deletions pkg/pie_chart_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
)

type PieChartHandler struct {
chart *PieChart
chart *PieChart
allowedRemoteDomains string
}

func NewPieChartHandler() *PieChartHandler {
func NewPieChartHandler(allowedRemoteDomains string) *PieChartHandler {
return &PieChartHandler{
chart: NewPieChart(),
chart: NewPieChart(),
allowedRemoteDomains: allowedRemoteDomains,
}
}

Expand All @@ -27,6 +29,13 @@ func (h *PieChartHandler) Get(c echo.Context) ([]byte, error) {
if err := BindRequest(c, req); err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err)
}
err := SetDataIfRemoteURL(req, h.allowedRemoteDomains)
if err != nil {
msgs := map[string]string{
"data": err.Error(),
}
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, msgs)
}

var data PieChartData
if err := json.Unmarshal([]byte(req.ChartData), &data); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/pie_chart_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestGetPieChart(t *testing.T) {
e := echo.New()

e.GET("/pie", func(c echo.Context) error {
img, err := NewPieChartHandler().Get(c)
img, err := NewPieChartHandler("").Get(c)
if err != nil {
return err
}
Expand Down
15 changes: 12 additions & 3 deletions pkg/radar_chart_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
)

type RadarChartHandler struct {
chart *RadarChart
chart *RadarChart
allowedRemoteDomains string
}

func NewRadarChartHandler() *RadarChartHandler {
func NewRadarChartHandler(allowedRemoteDomains string) *RadarChartHandler {
return &RadarChartHandler{
chart: NewRadarChart(),
chart: NewRadarChart(),
allowedRemoteDomains: allowedRemoteDomains,
}
}

Expand All @@ -28,6 +30,13 @@ func (h *RadarChartHandler) Get(c echo.Context) ([]byte, error) {
if err := BindRequest(c, req); err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err)
}
err := SetDataIfRemoteURL(req, h.allowedRemoteDomains)
if err != nil {
msgs := map[string]string{
"data": err.Error(),
}
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, msgs)
}

var data RadarChartData
if err := json.Unmarshal([]byte(req.ChartData), &data); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/radar_chart_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestGetRadarChart(t *testing.T) {
e := echo.New()

e.GET("/radar", func(c echo.Context) error {
img, err := NewRadarChartHandler().Get(c)
img, err := NewRadarChartHandler("").Get(c)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,47 +63,47 @@ func SetupRoutes(e *echo.Echo, baseURL string, publicDir embed.FS, allowedRemote
})
// /bar
e.GET(baseURL+"bar", func(c echo.Context) error {
img, err := NewBarChartHandler().Get(c)
img, err := NewBarChartHandler(allowedRemoteDomains).Get(c)
if err != nil {
return err
}
return c.Blob(http.StatusOK, "", img)
})
// /radar
e.GET(baseURL+"radar", func(c echo.Context) error {
img, err := NewRadarChartHandler().Get(c)
img, err := NewRadarChartHandler(allowedRemoteDomains).Get(c)
if err != nil {
return err
}
return c.Blob(http.StatusOK, "", img)
})
// /donut
e.GET(baseURL+"donut", func(c echo.Context) error {
img, err := NewDonutChartHandler().Get(c)
img, err := NewDonutChartHandler(allowedRemoteDomains).Get(c)
if err != nil {
return err
}
return c.Blob(http.StatusOK, "", img)
})
// /pie
e.GET(baseURL+"pie", func(c echo.Context) error {
img, err := NewPieChartHandler().Get(c)
img, err := NewPieChartHandler(allowedRemoteDomains).Get(c)
if err != nil {
return err
}
return c.Blob(http.StatusOK, "", img)
})
// /funnel
e.GET(baseURL+"funnel", func(c echo.Context) error {
img, err := NewFunnelChartHandler().Get(c)
img, err := NewFunnelChartHandler(allowedRemoteDomains).Get(c)
if err != nil {
return err
}
return c.Blob(http.StatusOK, "", img)
})
// /table
e.GET(baseURL+"table", func(c echo.Context) error {
img, err := NewTableChartHandler().Get(c)
img, err := NewTableChartHandler(allowedRemoteDomains).Get(c)
if err != nil {
return err
}
Expand Down
15 changes: 12 additions & 3 deletions pkg/table_chart_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
)

type TableChartHandler struct {
chart *TableChart
chart *TableChart
allowedRemoteDomains string
}

func NewTableChartHandler() *TableChartHandler {
func NewTableChartHandler(allowedRemoteDomains string) *TableChartHandler {
return &TableChartHandler{
chart: NewTableChart(),
chart: NewTableChart(),
allowedRemoteDomains: allowedRemoteDomains,
}
}

Expand All @@ -27,6 +29,13 @@ func (h *TableChartHandler) Get(c echo.Context) ([]byte, error) {
if err := BindRequest(c, req); err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err)
}
err := SetDataIfRemoteURL(req, h.allowedRemoteDomains)
if err != nil {
msgs := map[string]string{
"data": err.Error(),
}
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, msgs)
}

var data TableChartData
if err := json.Unmarshal([]byte(req.ChartData), &data); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/table_chart_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestGetTableChart(t *testing.T) {
e := echo.New()

e.GET("/table", func(c echo.Context) error {
img, err := NewTableChartHandler().Get(c)
img, err := NewTableChartHandler("").Get(c)
if err != nil {
return err
}
Expand Down

0 comments on commit 3036d4e

Please sign in to comment.