Skip to content

Commit

Permalink
Merge pull request #17 from metrico/fluxpipe-library
Browse files Browse the repository at this point in the history
Fluxpipelib
  • Loading branch information
lmangani authored Apr 21, 2023
2 parents 1d5bed3 + 2174dee commit e7c6bf3
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ jobs:
run: |
strip fluxpipe-server
strip fluxpipe-lambda
strip fluxpipelib.a
upx fluxpipe-server
- name: Get Timestamp
Expand All @@ -88,6 +89,7 @@ jobs:
release_config: |
fluxpipe-server
fluxpipe-lambda
fluxpipelib.a
tag_name: ${{ env.VERSION }}
release_name: fluxpipe_${{ env.VERSION }}
draft: false
Expand Down
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
111 changes: 111 additions & 0 deletions fluxpipelib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"C"
"bytes"
"context"
"fmt"
"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"
)


// # 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() {}

0 comments on commit e7c6bf3

Please sign in to comment.