Skip to content

Commit

Permalink
Merge pull request #22 from kevincobain2000/develop
Browse files Browse the repository at this point in the history
refactor to use env instead of arg for allowed domains
  • Loading branch information
kevincobain2000 authored Jan 28, 2024
2 parents b895450 + 11d436e commit 47ea94e
Show file tree
Hide file tree
Showing 20 changed files with 137 additions and 74 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21.3

require (
github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6
github.com/go-echarts/statsview v0.3.4
github.com/go-playground/validator v9.31.0+incompatible
github.com/imroc/req/v3 v3.42.2
github.com/labstack/echo/v4 v4.11.3
Expand All @@ -21,6 +22,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gaukas/godicttls v0.0.4 // indirect
github.com/go-echarts/go-echarts/v2 v2.2.3 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
Expand All @@ -42,6 +44,7 @@ require (
github.com/quic-go/qtls-go1-20 v0.3.3 // indirect
github.com/quic-go/quic-go v0.38.1 // indirect
github.com/refraction-networking/utls v1.5.3 // indirect
github.com/rs/cors v1.7.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.14.0 // indirect
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6 h1:6VSn3hB5U5GeA6kQ
github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6/go.mod h1:YxOVT5+yHzKvwhsiSIWmbAYM3Dr9AEEbER2dVayfBkg=
github.com/gaukas/godicttls v0.0.4 h1:NlRaXb3J6hAnTmWdsEKb9bcSBD6BvcIjdGdeb0zfXbk=
github.com/gaukas/godicttls v0.0.4/go.mod h1:l6EenT4TLWgTdwslVb4sEMOCf7Bv0JAK67deKr9/NCI=
github.com/go-echarts/go-echarts/v2 v2.2.3 h1:H8oPdUpzuiV2K8S4xYZa1JRNjP3U0h7HVqvhPrmCk1A=
github.com/go-echarts/go-echarts/v2 v2.2.3/go.mod h1:6TOomEztzGDVDkOSCFBq3ed7xOYfbOqhaBzD0YV771A=
github.com/go-echarts/statsview v0.3.4 h1:CCuytRAutdnF901NrR4BzSjHXjUp8OyA3/iopgG/1/Y=
github.com/go-echarts/statsview v0.3.4/go.mod h1:AehKjL9cTFMeIo5QdV8sQO43vFmfY65X5GMWa3XMciY=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
Expand Down Expand Up @@ -45,6 +49,7 @@ github.com/imroc/req/v3 v3.42.2 h1:/BwrKXGR7X1/ptccaQAiziDCeZ7T6ye55g3ZhiLy1fc=
github.com/imroc/req/v3 v3.42.2/go.mod h1:W7dOrfQORA9nFoj+CafIZ6P5iyk+rWdbp2sffOAvABU=
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down Expand Up @@ -80,11 +85,14 @@ github.com/quic-go/quic-go v0.38.1 h1:M36YWA5dEhEeT+slOu/SwMEucbYd0YFidxG3KlGPZa
github.com/quic-go/quic-go v0.38.1/go.mod h1:ijnZM7JsFIkp4cRyjxJNIzdSfCLmUMg9wdyhGmg+SN4=
github.com/refraction-networking/utls v1.5.3 h1:Ds5Ocg1+MC1ahNx5iBEcHe0jHeLaA/fLey61EENm7ro=
github.com/refraction-networking/utls v1.5.3/go.mod h1:SPuDbBmgLGp8s+HLNc83FuavwZCFoMmExj+ltUHiHUw=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down Expand Up @@ -171,6 +179,7 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8T
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
Expand Down
49 changes: 37 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,54 @@ import (
//go:embed all:frontend/dist/*
var publicDir embed.FS

var (
port string
type Flags struct {
host string
baseURL string
port string
baseUrl string
allowedRemoteDomains string
)
pprofHost string
pprofPort string
}

var f Flags
var version = "dev"

func main() {
if len(os.Args) > 1 && os.Args[1] == "version" {
fmt.Println(version)
return
}
flags()
e := pkg.NewEcho(baseURL, publicDir, allowedRemoteDomains)
SetupFlags()
e := pkg.NewEcho(f.baseUrl, publicDir)

pkg.GracefulServerWithPid(e, host, port)
pkg.GracefulServerWithPid(e, f.host, f.port)
}

func flags() {
flag.StringVar(&host, "host", "localhost", "host to serve")
flag.StringVar(&port, "port", "3001", "port to serve")
flag.StringVar(&baseURL, "base-url", "/", "base url with slash")
flag.StringVar(&allowedRemoteDomains, "allow-domains", "", "csv list of allowed domains for remote fetching")
func SetupFlags() {
flag.StringVar(&f.host, "host", "localhost", "host to serve")
flag.StringVar(&f.port, "port", "3001", "port to serve")
flag.StringVar(&f.baseUrl, "base-url", "/", "base url with slash")
flag.StringVar(&f.allowedRemoteDomains, "remote-domains", "", "csv list of allowed domains for remote fetching")
flag.StringVar(&f.pprofHost, "pprof-host", "", "pprof host")
flag.StringVar(&f.pprofPort, "pprof-port", "", "pprof port")
flag.Parse()

if f.pprofHost != "" && os.Getenv("PPROF_HOST") == "" {
err := os.Setenv("PPROF_HOST", f.pprofHost)
if err != nil {
pkg.Logger().Error(err)
}
}
if f.pprofPort != "" && os.Getenv("PPROF_PORT") == "" {
err := os.Setenv("PPROF_PORT", f.pprofPort)
if err != nil {
pkg.Logger().Error(err)
}
}
if f.allowedRemoteDomains != "" && os.Getenv("ALLOWED_REMOTE_DOMAINS") == "" {
err := os.Setenv("ALLOWED_REMOTE_DOMAINS", f.allowedRemoteDomains)
if err != nil {
pkg.Logger().Error(err)
}
}
}
10 changes: 4 additions & 6 deletions pkg/bar_chart_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import (
)

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

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

Expand All @@ -38,7 +36,7 @@ func (h *BarChartHandler) Get(c echo.Context) ([]byte, error) {
if err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, msgs)
}
err = SetDataIfRemoteURL(req, h.allowedRemoteDomains)
err = SetDataIfRemoteURL(req)
if err != nil {
msgs := map[string]string{
"data": err.Error(),
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
7 changes: 6 additions & 1 deletion pkg/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/http"
"net/url"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -125,7 +126,11 @@ func GetURL(urlStr string) (string, error) {
return resp.ToString()
}

func SetDataIfRemoteURL(req *ChartRequest, allowedRemoteDomains string) error {
func SetDataIfRemoteURL(req *ChartRequest) error {
allowedRemoteDomains := os.Getenv("ALLOWED_REMOTE_DOMAINS")
if allowedRemoteDomains == "" {
return nil
}
if IsURL(req.ChartData) {
if !IsAllowedDomain(req.ChartData, allowedRemoteDomains) {
return errors.New("URL is not allowed")
Expand Down
10 changes: 4 additions & 6 deletions pkg/donut_chart_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import (
)

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

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

Expand All @@ -36,7 +34,7 @@ func (h *DonutChartHandler) Get(c echo.Context) ([]byte, error) {
if err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, msgs)
}
err = SetDataIfRemoteURL(req, h.allowedRemoteDomains)
err = SetDataIfRemoteURL(req)
if err != nil {
msgs := map[string]string{
"data": err.Error(),
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
43 changes: 39 additions & 4 deletions pkg/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,32 @@ import (
"strconv"

"github.com/fvbock/endless"
"github.com/go-echarts/statsview"
"github.com/go-echarts/statsview/viewer"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)

func NewEcho(baseURL string, publicDir embed.FS, allowedRemoteDomains string) *echo.Echo {
func NewEcho(baseURL string, publicDir embed.FS) *echo.Echo {
if os.Getenv("PPROF_HOST") != "" && os.Getenv("PPROF_PORT") != "" {
Logger().Info("pprof enabled and listening on: ", os.Getenv("PPROF_HOST")+":"+os.Getenv("PPROF_PORT"))
addr := os.Getenv("PPROF_HOST") + ":" + os.Getenv("PPROF_PORT")
viewer.SetConfiguration(viewer.WithTheme(viewer.ThemeWesteros), viewer.WithAddr(addr))
mgr := statsview.New()
_ = mgr
go mgr.Start()
// mgr.Stop()
}
e := echo.New()
//recover
e.Use(middleware.Recover())
e.HTTPErrorHandler = HTTPErrorHandler
e.Pre(middleware.RemoveTrailingSlash())
e.Use(middleware.Gzip())
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "REQUEST[${time_custom}] ${method} ${uri} (${latency_human}) ${status} ${remote_ip}\n",
Format: ltsv(),
CustomTimeFormat: "2006-01-02 15:04:05",
}))
SetupRoutes(e, baseURL, publicDir, allowedRemoteDomains)
SetupRoutes(e, baseURL, publicDir)
return e
}

Expand Down Expand Up @@ -72,3 +82,28 @@ func HTTPErrorHandler(err error, c echo.Context) {
Logger().Error(err)
}
}

func ltsv() string {
time_custom := "2006-01-02 15:04:05"
_ = time_custom
var format string
format += "time:${time_custom}\t"
format += "host:${remote_ip}\t"
format += "forwardedfor:${header:x-forwarded-for}\t"
format += "req:-\t"
format += "status:${status}\t"
format += "method:${method}\t"
format += "uri:${uri}\t"
format += "size:${bytes_out}\t"
format += "referer:${referer}\t"
format += "ua:${user_agent}\t"
format += "reqtime_ns:${latency}\t"
format += "cache:-\t"
format += "runtime:-\t"
format += "apptime:-\t"
format += "vhost:${host}\t"
format += "reqtime_human:${latency_human}\t"
format += "x-request-id:${id}\t"
format += "host:${host}\n"
return format
}
10 changes: 4 additions & 6 deletions pkg/funnel_chart_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import (
)

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

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

Expand All @@ -36,7 +34,7 @@ func (h *FunnelChartHandler) Get(c echo.Context) ([]byte, error) {
if err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, msgs)
}
err = SetDataIfRemoteURL(req, h.allowedRemoteDomains)
err = SetDataIfRemoteURL(req)
if err != nil {
msgs := map[string]string{
"data": err.Error(),
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
10 changes: 4 additions & 6 deletions pkg/line_chart_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import (
)

type LineChartHandler struct {
chart *LineChart
allowedRemoteDomains string
chart *LineChart
}

func NewLineChartHandler(allowedRemoteDomains string) *LineChartHandler {
func NewLineChartHandler() *LineChartHandler {
return &LineChartHandler{
chart: NewLineChart(),
allowedRemoteDomains: allowedRemoteDomains,
chart: NewLineChart(),
}
}

Expand All @@ -37,7 +35,7 @@ func (h *LineChartHandler) Get(c echo.Context) ([]byte, error) {
if err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, msgs)
}
err = SetDataIfRemoteURL(req, h.allowedRemoteDomains)
err = SetDataIfRemoteURL(req)
if err != nil {
msgs := map[string]string{
"data": err.Error(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/line_chart_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestGetLineChart(t *testing.T) {
e := echo.New()

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

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

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

Expand All @@ -36,7 +34,7 @@ func (h *PieChartHandler) Get(c echo.Context) ([]byte, error) {
if err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, msgs)
}
err = SetDataIfRemoteURL(req, h.allowedRemoteDomains)
err = SetDataIfRemoteURL(req)
if err != nil {
msgs := map[string]string{
"data": err.Error(),
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
Loading

0 comments on commit 47ea94e

Please sign in to comment.