Skip to content

Go App Api Proposal

Frank Martinez edited this page Jun 21, 2018 · 5 revisions

Proposal for Go Application Api

The idea is to make it simple for a Go developer to code a flogo application. Given this, the goal is to:

  • Simplify the programming interface.
  • Ability to utilize existing triggers.
  • Ability to attach simple action handlers as functions
  • Still allow the use of existing Action (ability to execute and attach flows as resources

Examples

Simple Application using REST trigger and function handler:

func main() {

	app := flogo.NewApp()

	trg := app.NewTrigger(&rest.RestTrigger{}, map[string]interface{}{"port": 8080})
	trg.NewFuncHandler(map[string]interface{}{"method": "GET", "path": "/blah"}, LogMessage)

	e, err := flogo.NewEngine(app)

	if err != nil {
		logger.Error(err)
		return
	}

	engine.RunEngine(e)
}

func LogMessage(ctx context.Context, inputs map[string]*data.Attribute) (map[string]*data.Attribute, error) {
	logger.Infof("#v", inputs)
	return nil, nil
}

Running a flow:

func main() {

	app := flogo.NewApp()

        //load flow as json.RawMessage
        app.AddResource("flow:myflow", flowJson)

	trg := app.NewTrigger(&rest.RestTrigger{}, map[string]interface{}{"port": 8080})

	h1 := trg.NewHandler(map[string]interface{}{"method": "GET", "path": "/blah"})
	a := h1.NewAction(&flow.FlowAction{}, map[string]interface{}{"flowURI": "res://flow:myflow"})
	a.SetInputMappings("in1='blah'", "in2=1")
	a.SetOutputMappings("out1='blah'", "out2=$.flowOut")
	
	e, err := flogo.NewEngine(app)

	if err != nil {
		logger.Error(err)
		return
	}

	engine.RunEngine(e)
}

Running Activities:

func main() {

	app := flogo.NewApp()

	trg := app.NewTrigger(&rest.RestTrigger{}, map[string]interface{}{"port": 8080})
	trg.NewFuncHandler(map[string]interface{}{"method": "GET", "path": "/blah"}, RunActivities)

	e, err := flogo.NewEngine(app)

	if err != nil {
		logger.Error(err)
		return
	}

	engine.RunEngine(e)
}

func RunActivities(ctx context.Context, inputs map[string]*data.Attribute) (map[string]*data.Attribute, error) {

	in := map[string]interface{}{"message": "test"}
	_, err := flogo.EvalActivity(&log.LogActivity{}, in)

	if err != nil {
		return nil, err
	}

	return nil, nil
}

Running Activity Action:

func main() {

	app := flogo.NewApp()

	trg := app.NewTrigger(&rest.RestTrigger{}, map[string]interface{}{"port": 8080})
	h := trg.NewHandler(map[string]interface{}{"method": "GET", "path": "/blah/:id"})
	a := h.NewAction(&activity.ActivityAction{}, map[string]interface{}{"ref": "github.com/TIBCOSoftware/flogo-contrib/activity/log"})
	a.SetInputMappings("message=$.pathParams.id")
	a.SetOutputMappings("code=200")

	e, err := flogo.NewEngine(app)

	if err != nil {
		logger.Error(err)
		return
	}

	engine.RunEngine(e)
}

Generating Metadata

Currently, in order to generate the necessary metadata for contributed activities and triggers, you must run go generate before you build your app. This will traverse the library source and generate the corresponding metadata for activities and triggers that you are using.

Add the following to your main go file:

//go:generate go run <path to flogo gen.go file>  <path to src>

Path to flogo gen.go is relative to path of file with generate. Ex. ../github.com/TIBCOSoftware/flogo-lib/flogo/gen/gen.go

Code

Branch: https://github.com/TIBCOSoftware/flogo-lib/tree/go_api