diff --git a/build.sh b/build.sh index 488d109..15974ea 100755 --- a/build.sh +++ b/build.sh @@ -6,11 +6,11 @@ export PKG_CONFIG_PATH=$(pwd) echo "Building fluxpipe-server ..." -go build -a -ldflags '-extldflags "-static -w -ldl"' -o fluxpipe-server fluxpipe-server.go +go build -a -ldflags '-extldflags "-static -w -ldl"' -o fluxpipe-server ./cmd/server echo "Building fluxpipe-lambda ..." -go build -a -ldflags '-extldflags "-static -w -ldl"' -o fluxpipe-lambda fluxpipe-lambda.go +go build -a -ldflags '-extldflags "-static -w -ldl"' -o fluxpipe-lambda ./cmd/lambda echo "Building fluxpipe-lib ..." -CGO_ENABLED=1 go build -buildmode=c-archive -o fluxpipelib.a fluxpipelib.go +CGO_ENABLED=1 go build -buildmode=c-archive -o fluxpipelib.a ./cmd/lib # CGO_ENABLED=1 go build -buildmode=c-shared -o fluxpipelib.dylib fluxpipelib.go diff --git a/cmd/lambda/fluxpipe-lambda.go b/cmd/lambda/fluxpipe-lambda.go new file mode 100644 index 0000000..c20e53b --- /dev/null +++ b/cmd/lambda/fluxpipe-lambda.go @@ -0,0 +1,28 @@ +package main + +import ( + "context" + "github.com/metrico/fluxpipe/service" + + "github.com/aws/aws-lambda-go/lambda" +) + +var APPNAME = "fluxpipe-lambda" + +func exec(ctx context.Context, inputString string) (string, string) { + res, err := service.RunE(ctx, inputString) + return res, err.Error() +} + +type FluxEvent struct { + Query string `json:"flux"` +} + +func HandleRequest(ctx context.Context, flux FluxEvent) (string, error) { + buf, _ := exec(ctx, flux.Query) + return buf, nil +} + +func main() { + lambda.Start(HandleRequest) +} diff --git a/cmd/lib/fluxpipelib.go b/cmd/lib/fluxpipelib.go new file mode 100644 index 0000000..33d4947 --- /dev/null +++ b/cmd/lib/fluxpipelib.go @@ -0,0 +1,25 @@ +package main + +import ( + "C" + "context" + "fmt" + _ "github.com/InfluxCommunity/flux/fluxinit/static" + "github.com/metrico/fluxpipe/service" +) + +// # CGO_ENABLED=1 go build -buildmode=c-shared -o fluxpipe.a fluxpipelib.go +// # CGO_ENABLED=1 go build -buildmode=c-archive -o fluxpipe.a fluxpipelib.go + +var APPNAME = "fluxpipe-library" + +//export Query +func Query(query string) string { + res, err := service.RunE(context.Background(), query) + if err != nil { + return fmt.Sprintf(`{"code":"invalid","message":"%v"}`, err.Error()) + } + return res +} + +func main() {} diff --git a/fluxpipe-server.go b/cmd/server/fluxpipe-server.go similarity index 54% rename from fluxpipe-server.go rename to cmd/server/fluxpipe-server.go index 7aa70a5..3f6adb0 100644 --- a/fluxpipe-server.go +++ b/cmd/server/fluxpipe-server.go @@ -2,30 +2,16 @@ package main import ( "bufio" - "bytes" "context" "encoding/json" "flag" "fmt" - "os" - "strings" - "time" - - "github.com/InfluxCommunity/flux" - "github.com/InfluxCommunity/flux/csv" - _ "github.com/InfluxCommunity/flux/fluxinit/static" - "github.com/InfluxCommunity/flux/lang" - "github.com/InfluxCommunity/flux/memory" - "github.com/InfluxCommunity/flux/runtime" - - - _fluxhttp "github.com/InfluxCommunity/flux/dependencies/http" - "github.com/InfluxCommunity/flux/dependencies/secret" - "github.com/InfluxCommunity/flux/dependencies/url" - - _ "embed" + "github.com/metrico/fluxpipe/service" + "github.com/metrico/fluxpipe/static" "io/ioutil" "net/http" + "os" + "strings" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" @@ -33,26 +19,6 @@ import ( var APPNAME = "fluxpipe" -//go:embed static/play.html -var PLAY []byte - -//go:embed static/favicon.ico -var FAVICON []byte - -func runQuery(ctx context.Context, script string) (flux.Query, error) { - - program, err := lang.Compile(ctx, script, runtime.Default, time.Now()) - if err != nil { - return nil, err - } - - q, err := program.Start(ctx, memory.DefaultAllocator) - if err != nil { - return nil, err - } - return q, nil -} - func postQuery(c echo.Context) error { c.Response().Header().Set(echo.HeaderContentType, "text/csv; charset=utf-8") @@ -82,7 +48,7 @@ func postQuery(c echo.Context) error { } else { q := json_map["query"] query := fmt.Sprintf("%v", q) - res, err := exec(query) + res, err := service.RunE(c.Request().Context(), query) if err != nil { c.Response().Header().Set(echo.HeaderContentType, "application/json; charset=utf-8") c.Response().Header().Set("x-platform-error-code", "invalid") @@ -93,8 +59,7 @@ func postQuery(c echo.Context) error { } } else { - - res, err := exec(string(s)) + res, err := service.RunE(c.Request().Context(), string(s)) if err != nil { c.Response().Header().Set(echo.HeaderContentType, "application/json; charset=utf-8") c.Response().Header().Set("x-platform-error-code", "invalid") @@ -105,63 +70,6 @@ func postQuery(c echo.Context) error { } } -// NewCustomDependencies produces a Custom set of dependencies including EnvironmentSecretService. -func NewCustomDependencies() flux.Deps { - validator := url.PassValidator{} - return flux.Deps{ - Deps: flux.WrappedDeps{ - HTTPClient: _fluxhttp.NewLimitedDefaultClient(validator), - // Default to having no filesystem, no secrets, and no url validation (always pass). - FilesystemService: nil, - SecretService: secret.EnvironmentSecretService{}, - URLValidator: validator, - }, - } -} - -func exec(inputString string) (string, error) { - - // CustomDeps produces a Custom set of dependencies including EnvironmentSecretService. - customValidator := url.PassValidator{} - customDeps := flux.Deps{ - Deps: flux.WrappedDeps{ - HTTPClient: _fluxhttp.NewLimitedDefaultClient(customValidator), - FilesystemService: nil, - SecretService: secret.EnvironmentSecretService{}, - URLValidator: customValidator, - }, - } - - // ctx := flux.NewDefaultDependencies().Inject(context.Background()) - ctx := customDeps.Inject(context.Background()) - - q, err := runQuery(ctx, inputString) - if err != nil { - fmt.Println("unexpected error while creating query: %s", err) - return "", err - } - - results := flux.NewResultIteratorFromQuery(q) - defer results.Release() - - buf := bytes.NewBuffer(nil) - encoder := csv.NewMultiResultEncoder(csv.DefaultEncoderConfig()) - - if _, err := encoder.Encode(buf, results); err != nil { - return "", err - } - - q.Done() - - if q.Err() != nil { - fmt.Println("unexpected error from query execution: %s", q.Err()) - return "", q.Err() - - } else { - return buf.String(), nil - } -} - func main() { port := flag.String("port", "8086", "API port") @@ -169,7 +77,7 @@ func main() { cors := flag.Bool("cors", true, "API cors mode") flag.Parse() - scanner := bufio.NewScanner((os.Stdin)) + scanner := bufio.NewScanner(os.Stdin) inputString := "" if *stdin == true { @@ -181,12 +89,12 @@ func main() { fmt.Fprintln(os.Stderr, "reading standard input:", err) } - buf, err := exec(inputString) + buf, err := service.RunE(context.Background(), inputString) if err != nil { fmt.Fprintln(os.Stderr, "we have some error: ", err) return } - + fmt.Println(strings.Replace(buf, "\r\n", "\n", -1)) } else { @@ -204,10 +112,10 @@ func main() { } e.GET("/", func(c echo.Context) error { - return c.Blob(http.StatusOK, "text/html", PLAY) + return c.Blob(http.StatusOK, "text/html", static.PLAY) }) e.GET("/favicon.ico", func(c echo.Context) error { - return c.Blob(http.StatusOK, "image/x-icon", FAVICON) + return c.Blob(http.StatusOK, "image/x-icon", static.FAVICON) }) e.GET("/hello", func(c echo.Context) error { diff --git a/fluxpipe-lambda.go b/fluxpipe-lambda.go deleted file mode 100644 index 951e70e..0000000 --- a/fluxpipe-lambda.go +++ /dev/null @@ -1,108 +0,0 @@ -package main - -import ( - "context" - "fmt" - "time" - "bytes" - - "github.com/InfluxCommunity/flux" - "github.com/InfluxCommunity/flux/csv" - _ "github.com/InfluxCommunity/flux/fluxinit/static" - "github.com/InfluxCommunity/flux/lang" - "github.com/InfluxCommunity/flux/memory" - "github.com/InfluxCommunity/flux/runtime" - - _fluxhttp "github.com/InfluxCommunity/flux/dependencies/http" - "github.com/InfluxCommunity/flux/dependencies/secret" - "github.com/InfluxCommunity/flux/dependencies/url" - - "github.com/aws/aws-lambda-go/lambda" - -) - -var APPNAME = "fluxpipe-lambda" - -func runQuery(ctx context.Context, script string) (flux.Query, error) { - - program, err := lang.Compile(ctx, script, runtime.Default, time.Now()) - if err != nil { - return nil, err - } - - q, err := program.Start(ctx, memory.DefaultAllocator) - if err != nil { - return nil, err - } - return q, nil -} - -// NewCustomDependencies produces a Custom set of dependencies including EnvironmentSecretService. -func NewCustomDependencies() flux.Deps { - validator := url.PassValidator{} - return flux.Deps{ - Deps: flux.WrappedDeps{ - HTTPClient: _fluxhttp.NewLimitedDefaultClient(validator), - // Default to having no filesystem, no secrets, and no url validation (always pass). - FilesystemService: nil, - SecretService: secret.EnvironmentSecretService{}, - URLValidator: validator, - }, - } -} - -func exec(inputString string) (string, string) { - - // CustomDeps produces a Custom set of dependencies including EnvironmentSecretService. - customValidator := url.PassValidator{} - customDeps := flux.Deps{ - Deps: flux.WrappedDeps{ - HTTPClient: _fluxhttp.NewLimitedDefaultClient(customValidator), - FilesystemService: nil, - SecretService: secret.EnvironmentSecretService{}, - URLValidator: customValidator, - }, - } - - // ctx := flux.NewDefaultDependencies().Inject(context.Background()) - ctx := customDeps.Inject(context.Background()) - - q, err := runQuery(ctx, inputString) - if err != nil { - fmt.Println("unexpected error while creating query: %s", err) - return "", string(fmt.Sprintf(`{"code":"invalid","message":"%v"}`, err)) - } - - results := flux.NewResultIteratorFromQuery(q) - defer results.Release() - - buf := bytes.NewBuffer(nil) - encoder := csv.NewMultiResultEncoder(csv.DefaultEncoderConfig()) - - if _, err := encoder.Encode(buf, results); err != nil { - panic(err) - } - - q.Done() - - if q.Err() != nil { - fmt.Println("unexpected error from query execution: %s", q.Err()) - return "", string(fmt.Sprintf(`{"code":"invalid","message":"%v"}`, q.Err())) - - } else { - return buf.String(), "" - } -} - -type FluxEvent struct { - Query string `json:"flux"` -} - -func HandleRequest(ctx context.Context, flux FluxEvent) (string, error) { - buf, _ := exec(flux.Query) - return buf, nil -} - -func main() { - lambda.Start(HandleRequest) -} diff --git a/fluxpipelib.go b/fluxpipelib.go deleted file mode 100644 index 0bb3053..0000000 --- a/fluxpipelib.go +++ /dev/null @@ -1,111 +0,0 @@ -package main - -import ( - "C" - "bytes" - "context" - "fmt" - "time" - - "github.com/InfluxCommunity/flux" - "github.com/InfluxCommunity/flux/csv" - _ "github.com/InfluxCommunity/flux/fluxinit/static" - "github.com/InfluxCommunity/flux/lang" - "github.com/InfluxCommunity/flux/memory" - "github.com/InfluxCommunity/flux/runtime" - - _fluxhttp "github.com/InfluxCommunity/flux/dependencies/http" - "github.com/InfluxCommunity/flux/dependencies/secret" - "github.com/InfluxCommunity/flux/dependencies/url" -) - - -// # CGO_ENABLED=1 go build -buildmode=c-shared -o fluxpipe.a fluxpipelib.go -// # CGO_ENABLED=1 go build -buildmode=c-archive -o fluxpipe.a fluxpipelib.go - -var APPNAME = "fluxpipe-library" - -func runQuery(ctx context.Context, script string) (flux.Query, error) { - - program, err := lang.Compile(ctx, script, runtime.Default, time.Now()) - if err != nil { - return nil, err - } - - q, err := program.Start(ctx, memory.DefaultAllocator) - if err != nil { - return nil, err - } - return q, nil -} - -func NewCustomDependencies() flux.Deps { - validator := url.PassValidator{} - return flux.Deps{ - Deps: flux.WrappedDeps{ - HTTPClient: _fluxhttp.NewLimitedDefaultClient(validator), - // Default to having no filesystem, no secrets, and no url validation (always pass). - FilesystemService: nil, - SecretService: secret.EnvironmentSecretService{}, - URLValidator: validator, - }, - } -} - -func exec(inputString string) (string, error) { - - // CustomDeps produces a Custom set of dependencies including EnvironmentSecretService. - customValidator := url.PassValidator{} - customDeps := flux.Deps{ - Deps: flux.WrappedDeps{ - HTTPClient: _fluxhttp.NewLimitedDefaultClient(customValidator), - FilesystemService: nil, - SecretService: secret.EnvironmentSecretService{}, - URLValidator: customValidator, - }, - } - - // ctx := flux.NewDefaultDependencies().Inject(context.Background()) - ctx := customDeps.Inject(context.Background()) - - q, err := runQuery(ctx, inputString) - if err != nil { - fmt.Println("unexpected error while creating query: %s", err) - return "", err - } - - results := flux.NewResultIteratorFromQuery(q) - defer results.Release() - - buf := bytes.NewBuffer(nil) - encoder := csv.NewMultiResultEncoder(csv.DefaultEncoderConfig()) - - if _, err := encoder.Encode(buf, results); err != nil { - return "", err - } - - q.Done() - - if q.Err() != nil { - fmt.Println("unexpected error from query execution: %s", q.Err()) - return "", q.Err() - - } else { - return buf.String(), nil - } -} - -type FluxEvent struct { - Query string `json:"flux"` -} - -//export Query -func Query(query string) string { - res, err := exec(query) - if err != nil { - return fmt.Sprintf(`{"code":"invalid","message":"%v"}`, err.Error()) - } - return res -} - -func main() {} diff --git a/go.mod b/go.mod index a3590cf..285af1e 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,12 @@ go 1.18 require ( github.com/InfluxCommunity/flux v0.195.1 github.com/aws/aws-lambda-go v1.13.3 + github.com/fatih/color v1.13.0 github.com/labstack/echo/v4 v4.2.1 + github.com/mattn/go-sqlite3 v1.11.0 + github.com/opentracing/opentracing-go v1.2.0 + github.com/spf13/cobra v0.0.3 + github.com/uber/jaeger-client-go v2.28.0+incompatible ) replace github.com/influxdata/influxdb-iox-client-go/v2 v2.0.0-beta.2 => github.com/metrico/influxdb-iox-client-go/v2 v2.0.0-beta.3 @@ -44,8 +49,10 @@ require ( github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.9.0 // indirect github.com/aws/aws-sdk-go-v2/service/s3 v1.19.0 // indirect github.com/aws/smithy-go v1.9.0 // indirect + github.com/benbjohnson/clock v1.1.0 // indirect github.com/benbjohnson/immutable v0.3.0 // indirect github.com/bonitoo-io/go-sql-bigquery v0.3.4-1.4.0 // indirect + github.com/c-bata/go-prompt v0.2.2 // indirect github.com/cespare/xxhash/v2 v2.1.1 // indirect github.com/deepmap/oapi-codegen v1.6.0 // indirect github.com/denisenkom/go-mssqldb v0.10.0 // indirect @@ -66,6 +73,7 @@ require ( github.com/google/go-cmp v0.5.8 // indirect github.com/google/uuid v1.3.0 // indirect github.com/googleapis/gax-go/v2 v2.0.5 // indirect + github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/influxdata/gosnowflake v1.6.9 // indirect github.com/influxdata/influxdb-client-go/v2 v2.3.1-0.20210518120617-5d1fff431040 // indirect github.com/influxdata/influxdb-iox-client-go/v2 v2.0.0-beta.2 // indirect @@ -82,23 +90,25 @@ require ( github.com/mattn/go-colorable v0.1.9 // indirect github.com/mattn/go-ieproxy v0.0.1 // indirect github.com/mattn/go-isatty v0.0.16 // indirect + github.com/mattn/go-runewidth v0.0.3 // indirect + github.com/mattn/go-tty v0.0.0-20180907095812-13ff1204f104 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 // indirect github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect - github.com/opentracing/opentracing-go v1.2.0 // indirect github.com/pierrec/lz4/v4 v4.1.16 // indirect github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pkg/term v0.0.0-20180730021639-bffc007b7fd5 // indirect github.com/prometheus/client_model v0.1.0 // indirect github.com/prometheus/common v0.7.0 // indirect github.com/segmentio/kafka-go v0.1.0 // indirect github.com/sergi/go-diff v1.0.0 // indirect github.com/sirupsen/logrus v1.8.1 // indirect + github.com/spf13/pflag v1.0.3 // indirect github.com/stretchr/objx v0.5.0 // indirect github.com/uber-go/tally v3.3.15+incompatible // indirect github.com/uber/athenadriver v1.1.4 // indirect - github.com/uber/jaeger-client-go v2.28.0+incompatible // indirect github.com/uber/jaeger-lib v2.4.1+incompatible // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.1 // indirect diff --git a/go.sum b/go.sum index 199d75e..5e9c871 100644 --- a/go.sum +++ b/go.sum @@ -150,6 +150,7 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.10.0/go.mod h1:jLKCFqS+1T4i7HDqCP9GM github.com/aws/smithy-go v1.9.0 h1:c7FUdEqrQA1/UVKKCNDFQPNKGp4FQg3YW4Ck5SLTG58= github.com/aws/smithy-go v1.9.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= +github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/benbjohnson/immutable v0.3.0 h1:TVRhuZx2wG9SZ0LRdqlbs9S5BZ6Y24hJEHTCgWHZEIw= github.com/benbjohnson/immutable v0.3.0/go.mod h1:uc6OHo6PN2++n98KHLxW8ef4W42ylHiQSENghE1ezxI= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= @@ -160,6 +161,7 @@ github.com/bonitoo-io/go-sql-bigquery v0.3.4-1.4.0 h1:MaVh0h9+KaMnJcoDvvIGp+O3fe github.com/bonitoo-io/go-sql-bigquery v0.3.4-1.4.0/go.mod h1:J4Y6YJm0qTWB9aFziB7cPeSyc6dOZFyJdteSeybVpXQ= github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/c-bata/go-prompt v0.2.2 h1:uyKRz6Z6DUyj49QVijyM339UJV9yhbr70gESwbNU3e0= +github.com/c-bata/go-prompt v0.2.2/go.mod h1:VzqtzE2ksDBcdln8G7mk2RX9QyGjH+OVqOCSiVIqS34= github.com/cactus/go-statsd-client/statsd v0.0.0-20191106001114-12b4e2b38748/go.mod h1:l/bIBLeOl9eX+wxJAzxS4TveKRtAqlyDpHjhkfO0MEI= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= @@ -216,6 +218,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.m github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= +github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k= @@ -388,6 +392,7 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/influxdata/gosnowflake v1.6.9 h1:BhE39Mmh8bC+Rvd4QQsP2gHypfeYIH1wqW1AjGWxxrE= github.com/influxdata/gosnowflake v1.6.9/go.mod h1:9W/BvCXOKx2gJtQ+jdi1Vudev9t9/UDOEHnlJZ/y1nU= @@ -468,12 +473,16 @@ github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNx github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4= +github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mattn/go-sqlite3 v1.11.0 h1:LDdKkqtYlom37fkvqs8rMPFKAMe8+SgjbwZ6ex1/A/Q= +github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-tty v0.0.0-20180907095812-13ff1204f104 h1:d8RFOZ2IiFtFWBcKEHAFYJcPTf0wY5q0exFNJZVWa1U= +github.com/mattn/go-tty v0.0.0-20180907095812-13ff1204f104/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/metrico/influxdb-iox-client-go/v2 v2.0.0-beta.3 h1:OmH1x/EgyXKtvkdgOKMdfXscmmjAsvTuCBZ+8FmjT9c= @@ -545,6 +554,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA= github.com/pkg/term v0.0.0-20180730021639-bffc007b7fd5 h1:tFwafIEMf0B7NlcxV/zJ6leBIa81D3hgGSgsE5hCkOQ= +github.com/pkg/term v0.0.0-20180730021639-bffc007b7fd5/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= @@ -591,8 +601,11 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1 github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY= +github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8= github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI= diff --git a/service/service.go b/service/service.go new file mode 100644 index 0000000..46da173 --- /dev/null +++ b/service/service.go @@ -0,0 +1,122 @@ +package service + +import ( + "bytes" + "context" + "fmt" + "github.com/InfluxCommunity/flux" + "github.com/InfluxCommunity/flux/csv" + "github.com/InfluxCommunity/flux/dependencies/bigtable" + "github.com/InfluxCommunity/flux/dependencies/influxdb" + "github.com/InfluxCommunity/flux/dependencies/mqtt" + "github.com/InfluxCommunity/flux/dependency" + _ "github.com/InfluxCommunity/flux/fluxinit/static" + "github.com/InfluxCommunity/flux/lang" + "github.com/InfluxCommunity/flux/memory" + "github.com/InfluxCommunity/flux/runtime" +) + +func RunE(ctx context.Context, script string) (string, error) { + // Defer initialization until other common errors + // have already passed to avoid a long load time + // for a simple unrelated error. + + ctx = injectDependencies(ctx) + + return executeE(ctx, script) +} + +func executeE(ctx context.Context, script string) (string, error) { + c := lang.FluxCompiler{ + Query: script, + } + prog, err := c.Compile(ctx, runtime.Default) + if err != nil { + return "", err + } + + mem := &memory.ResourceAllocator{} + q, err := prog.Start(ctx, mem) + if err != nil { + return "", err + } + + results := flux.NewResultIteratorFromQuery(q) + defer results.Release() + + config := csv.DefaultEncoderConfig() + encoder := csv.NewMultiResultEncoder(config) + b := bytes.Buffer{} + _, err = encoder.Encode(&b, results) + if err != nil { + return "", err + } + results.Release() + + return string(b.Bytes()), results.Err() +} + +func injectDependencies(ctx context.Context) context.Context { + _deps := flux.NewDefaultDependencies() + + deps := Dependencies{ + Deps: _deps, + + influxdb: influxdb.Dependency{ + Provider: &HttpProvider{&influxdb.HttpProvider{DefaultConfig: influxdb.Config{}}}, + }, + + bigtable: bigtable.Dependency{ + Provider: bigtable.DefaultProvider{}, + }, + + mqtt: mqtt.Dependency{ + Dialer: mqtt.DefaultDialer{}, + }, + } + ctx, span := dependency.Inject(ctx, deps) + defer span.Finish() + return ctx +} + +type HttpProvider struct { + *influxdb.HttpProvider +} + +func (h *HttpProvider) ReaderFor( + ctx context.Context, conf influxdb.Config, bounds flux.Bounds, + predicateSet influxdb.PredicateSet) (influxdb.Reader, error) { + if conf.Host == "" { + return nil, fmt.Errorf("host is required") + } + return h.HttpProvider.ReaderFor(ctx, conf, bounds, predicateSet) +} + +func (h *HttpProvider) SeriesCardinalityReaderFor(ctx context.Context, + conf influxdb.Config, bounds flux.Bounds, predicateSet influxdb.PredicateSet) (influxdb.Reader, error) { + if conf.Host == "" { + return nil, fmt.Errorf("host is required") + } + return h.HttpProvider.SeriesCardinalityReaderFor(ctx, conf, bounds, predicateSet) +} + +func (h *HttpProvider) WriterFor(ctx context.Context, conf influxdb.Config) (influxdb.Writer, error) { + if conf.Host == "" { + return nil, fmt.Errorf("host is required") + } + return h.HttpProvider.WriterFor(ctx, conf) +} + +type Dependencies struct { + flux.Deps + influxdb influxdb.Dependency + bigtable bigtable.Dependency + mqtt mqtt.Dependency +} + +func (d Dependencies) Inject(ctx context.Context) context.Context { + ctx = d.Deps.Inject(ctx) + ctx = d.influxdb.Inject(ctx) + ctx = d.bigtable.Inject(ctx) + return d.mqtt.Inject(ctx) +} diff --git a/static/static.go b/static/static.go new file mode 100644 index 0000000..ad45510 --- /dev/null +++ b/static/static.go @@ -0,0 +1,9 @@ +package static + +import _ "embed" + +//go:embed play.html +var PLAY []byte + +//go:embed favicon.ico +var FAVICON []byte