Created
August 7, 2018 05:01
-
-
Save retgits/47abf58331b7c624e3127242221696f8 to your computer and use it in GitHub Desktop.
A Flogo app written in Go that connects to PubNub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//go:generate go run $GOPATH/src/github.com/TIBCOSoftware/flogo-lib/flogo/gen/gen.go $GOPATH | |
package main | |
import ( | |
"context" | |
"github.com/TIBCOSoftware/flogo-contrib/activity/log" | |
"github.com/TIBCOSoftware/flogo-lib/core/data" | |
"github.com/TIBCOSoftware/flogo-lib/engine" | |
"github.com/TIBCOSoftware/flogo-lib/flogo" | |
"github.com/TIBCOSoftware/flogo-lib/logger" | |
"github.com/retgits/flogo-components/activity/writetofile" | |
"github.com/retgits/flogo-components/trigger/pubnubsubscriber" | |
) | |
var ( | |
// The key from PubNub (usually starts with pub-c) | |
publishKey = "" | |
// The key from PubNub (usually starts with sub-c) | |
subscribeKey = "" | |
// The channel on which messages will come (totally up to you to choose this) | |
channel = "MyChannel" | |
// The name of the file to write | |
filename = "visitors.txt" | |
) | |
// The main function will start the Flogo engine and will make sure that it keeps running | |
func main() { | |
// Create a new Flogo app using the appBuilder function | |
app := appBuilder() | |
// Create a new Flogo engine | |
e, err := flogo.NewEngine(app) | |
if err != nil { | |
logger.Error(err) | |
return | |
} | |
// Start the engine! | |
engine.RunEngine(e) | |
} | |
// The appBuilder function builds an app that the engine needs. It has all the configuration of the triggers and specifies which function should be called if a new event arrives | |
func appBuilder() *flogo.App { | |
// Create a new Flogo app | |
app := flogo.NewApp() | |
// Register the PubNub trigger. The input are all the fields from the settings section of the trigger.json | |
trg := app.NewTrigger(&pubnubsubscriber.PubNubTrigger{}, map[string]interface{}{"publishKey": publishKey, "subscribeKey": subscribeKey}) | |
// Tell the trigger which function to execute when a new message comes in. The input is the channel on which to listen | |
trg.NewFuncHandler(map[string]interface{}{"channel": channel}, handler) | |
return app | |
} | |
// The handler is the function that gets executed every time an event comes in | |
func handler(ctx context.Context, inputs map[string]*data.Attribute) (map[string]*data.Attribute, error) { | |
// Get the message from the inputs. All the fields described in the outputs section of the trigger.json are available in the same way | |
message := inputs["message"].Value() | |
// Just like in the Web UI you can log the incoming message using the Log Message activity. In this case the input is the message we got from the trigger | |
in := map[string]interface{}{"message": message, "flowInfo": "true", "addToFlow": "true"} | |
_, err := flogo.EvalActivity(&log.LogActivity{}, in) | |
if err != nil { | |
return nil, err | |
} | |
// Just like in the Web UI, you can use the Write to File activity to write data to a file. In this case the exact same settings are used as in the Web UI earlier | |
in = map[string]interface{}{"filename": filename, "content": message, "append": true, "create": true} | |
_, err = flogo.EvalActivity(&writetofile.MyActivity{}, in) | |
if err != nil { | |
return nil, err | |
} | |
return nil, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment