From e80053238c6c24929fedfb9e9103de241e9d863b Mon Sep 17 00:00:00 2001 From: Lorenzo Mangani Date: Thu, 20 Apr 2023 23:55:56 +0200 Subject: [PATCH 1/6] Create fluxpipelib.go Signed-off-by: Lorenzo Mangani --- fluxpipelib.go | 126 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 fluxpipelib.go diff --git a/fluxpipelib.go b/fluxpipelib.go new file mode 100644 index 0000000..f21314e --- /dev/null +++ b/fluxpipelib.go @@ -0,0 +1,126 @@ +package main + +import ( + "C" + "bufio" + "bytes" + "context" + "encoding/json" + "flag" + "fmt" + "os" + "strings" + "time" + + "github.com/influxdata/flux" + "github.com/influxdata/flux/csv" + _ "github.com/influxdata/flux/fluxinit/static" + "github.com/influxdata/flux/lang" + "github.com/influxdata/flux/memory" + "github.com/influxdata/flux/runtime" + + _fluxhttp "github.com/influxdata/flux/dependencies/http" + "github.com/influxdata/flux/dependencies/secret" + "github.com/influxdata/flux/dependencies/url" + + _ "embed" + "io/ioutil" + "net/http" + + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" +) + +# SHARED +# CGO_ENABLED=1 go build -buildmode=c-shared -o fluxpipe.a fluxpipelib.go + +# STATIC +# 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 +} + +// 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 + } +} + +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() {} From 8df802807308911c9df03b4c20ec0305ea0e4394 Mon Sep 17 00:00:00 2001 From: Lorenzo Mangani Date: Fri, 21 Apr 2023 00:00:37 +0200 Subject: [PATCH 2/6] fmt --- fluxpipelib.go | 78 +++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/fluxpipelib.go b/fluxpipelib.go index f21314e..79d6d83 100644 --- a/fluxpipelib.go +++ b/fluxpipelib.go @@ -1,41 +1,41 @@ package main import ( - "C" - "bufio" - "bytes" - "context" - "encoding/json" - "flag" - "fmt" - "os" - "strings" - "time" - - "github.com/influxdata/flux" - "github.com/influxdata/flux/csv" - _ "github.com/influxdata/flux/fluxinit/static" - "github.com/influxdata/flux/lang" - "github.com/influxdata/flux/memory" - "github.com/influxdata/flux/runtime" - - _fluxhttp "github.com/influxdata/flux/dependencies/http" - "github.com/influxdata/flux/dependencies/secret" - "github.com/influxdata/flux/dependencies/url" - - _ "embed" - "io/ioutil" - "net/http" - - "github.com/labstack/echo/v4" - "github.com/labstack/echo/v4/middleware" + "C" + "bufio" + "bytes" + "context" + "encoding/json" + "flag" + "fmt" + "os" + "strings" + "time" + + "github.com/influxdata/flux" + "github.com/influxdata/flux/csv" + _ "github.com/influxdata/flux/fluxinit/static" + "github.com/influxdata/flux/lang" + "github.com/influxdata/flux/memory" + "github.com/influxdata/flux/runtime" + + _fluxhttp "github.com/influxdata/flux/dependencies/http" + "github.com/influxdata/flux/dependencies/secret" + "github.com/influxdata/flux/dependencies/url" + + _ "embed" + "io/ioutil" + "net/http" + + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" ) -# SHARED -# CGO_ENABLED=1 go build -buildmode=c-shared -o fluxpipe.a fluxpipelib.go +// SHARED +// # CGO_ENABLED=1 go build -buildmode=c-shared -o fluxpipe.a fluxpipelib.go -# STATIC -# CGO_ENABLED=1 go build -buildmode=c-archive -o fluxpipe.a fluxpipelib.go +// STATIC +// # CGO_ENABLED=1 go build -buildmode=c-archive -o fluxpipe.a fluxpipelib.go var APPNAME = "fluxpipe-library" @@ -86,7 +86,7 @@ func exec(inputString string) (string, error) { q, err := runQuery(ctx, inputString) if err != nil { fmt.Println("unexpected error while creating query: %s", err) - return "", err + return "", err } results := flux.NewResultIteratorFromQuery(q) @@ -111,16 +111,16 @@ func exec(inputString string) (string, error) { } type FluxEvent struct { - Query string `json:"flux"` + 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 + res, err := exec(query) + if err != nil { + return fmt.Sprintf(`{"code":"invalid","message":"%v"}`, err.Error()) + } + return res } func main() {} From bbd8eff13da2f6d63ee1ad91246dc6bd1d1865a6 Mon Sep 17 00:00:00 2001 From: Lorenzo Mangani Date: Fri, 21 Apr 2023 00:06:10 +0200 Subject: [PATCH 3/6] Update build.sh Signed-off-by: Lorenzo Mangani --- build.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/build.sh b/build.sh index 8d47071..3f32d14 100755 --- a/build.sh +++ b/build.sh @@ -3,5 +3,10 @@ export PKG_CONFIG_PATH=$(pwd) echo "Building fluxpipe-server ..." go build -a -ldflags '-extldflags "-static -w -ldl"' -o fluxpipe-server fluxpipe-server.go + echo "Building fluxpipe-lambda ..." go build -a -ldflags '-extldflags "-static -w -ldl"' -o fluxpipe-lambda fluxpipe-lambda.go + +echo "Building fluxpipe-lib ..." +CGO_ENABLED=1 go build -buildmode=c-archive -o fluxpipelib.a fluxpipelib.go +CGO_ENABLED=1 go build -buildmode=c-shared -o fluxpipelib.dylib fluxpipelib.go From e653f3725849b972096059cd6efda8d7e3b814bb Mon Sep 17 00:00:00 2001 From: Lorenzo Mangani Date: Fri, 21 Apr 2023 11:36:08 +0200 Subject: [PATCH 4/6] Update fluxpipelib.go Signed-off-by: Lorenzo Mangani --- fluxpipelib.go | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/fluxpipelib.go b/fluxpipelib.go index 79d6d83..2676d62 100644 --- a/fluxpipelib.go +++ b/fluxpipelib.go @@ -2,14 +2,9 @@ package main import ( "C" - "bufio" "bytes" "context" - "encoding/json" - "flag" "fmt" - "os" - "strings" "time" "github.com/influxdata/flux" @@ -22,19 +17,10 @@ import ( _fluxhttp "github.com/influxdata/flux/dependencies/http" "github.com/influxdata/flux/dependencies/secret" "github.com/influxdata/flux/dependencies/url" - - _ "embed" - "io/ioutil" - "net/http" - - "github.com/labstack/echo/v4" - "github.com/labstack/echo/v4/middleware" ) -// SHARED -// # CGO_ENABLED=1 go build -buildmode=c-shared -o fluxpipe.a fluxpipelib.go -// STATIC +// # 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" @@ -53,7 +39,6 @@ func runQuery(ctx context.Context, script string) (flux.Query, error) { return q, nil } -// NewCustomDependencies produces a Custom set of dependencies including EnvironmentSecretService. func NewCustomDependencies() flux.Deps { validator := url.PassValidator{} return flux.Deps{ From 15d29aa0f78d1d11382aa8bf2a6c3e3e84d48c7d Mon Sep 17 00:00:00 2001 From: Lorenzo Mangani Date: Fri, 21 Apr 2023 12:04:14 +0200 Subject: [PATCH 5/6] Update build.sh Signed-off-by: Lorenzo Mangani --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 3f32d14..178657a 100755 --- a/build.sh +++ b/build.sh @@ -9,4 +9,4 @@ go build -a -ldflags '-extldflags "-static -w -ldl"' -o fluxpipe-lambda fluxpipe echo "Building fluxpipe-lib ..." CGO_ENABLED=1 go build -buildmode=c-archive -o fluxpipelib.a fluxpipelib.go -CGO_ENABLED=1 go build -buildmode=c-shared -o fluxpipelib.dylib fluxpipelib.go +# CGO_ENABLED=1 go build -buildmode=c-shared -o fluxpipelib.dylib fluxpipelib.go From 2174deee1538640b2d80ca29f657d3e5aad4a961 Mon Sep 17 00:00:00 2001 From: Lorenzo Mangani Date: Fri, 21 Apr 2023 12:06:15 +0200 Subject: [PATCH 6/6] Update go.yml Signed-off-by: Lorenzo Mangani --- .github/workflows/go.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 4ad57b6..0f3188b 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -65,6 +65,7 @@ jobs: run: | strip fluxpipe-server strip fluxpipe-lambda + strip fluxpipelib.a upx fluxpipe-server - name: Get Timestamp @@ -88,6 +89,7 @@ jobs: release_config: | fluxpipe-server fluxpipe-lambda + fluxpipelib.a tag_name: ${{ env.VERSION }} release_name: fluxpipe_${{ env.VERSION }} draft: false