Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ping Trigger #4

Open
wants to merge 57 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
4c9eeda
Initial commit for ping trigger
AkshayGadikar Oct 31, 2018
9869ed6
changes ping
AkshayGadikar Oct 31, 2018
cbdb98c
mod.go change
AkshayGadikar Oct 31, 2018
6d5a8b7
change
AkshayGadikar Oct 31, 2018
f1ea66e
removed trigger.json
AkshayGadikar Oct 31, 2018
8539b35
change-1
AkshayGadikar Oct 31, 2018
15d8682
trigger.go changes to interface
AkshayGadikar Oct 31, 2018
8673847
changes to interface
AkshayGadikar Oct 31, 2018
ca89358
changes to interface
AkshayGadikar Oct 31, 2018
33883af
fixes - 1
AkshayGadikar Oct 31, 2018
75a5e7c
changes to metadata
AkshayGadikar Oct 31, 2018
f1d73ea
flogo json update
AkshayGadikar Oct 31, 2018
02729cb
update trigger code
AkshayGadikar Nov 1, 2018
773b901
update trigger code
AkshayGadikar Nov 1, 2018
916bea0
update trigger code
AkshayGadikar Nov 1, 2018
b3c73f9
change -1 : go.mod
AkshayGadikar Nov 1, 2018
83696bd
change -1 : go.mod
AkshayGadikar Nov 1, 2018
2256c85
change : flogo.json
AkshayGadikar Nov 1, 2018
042ca7d
fixes
AkshayGadikar Nov 1, 2018
f6789c5
fixes
AkshayGadikar Nov 1, 2018
4fcfde3
fixes
AkshayGadikar Nov 1, 2018
73b13a5
fixes
AkshayGadikar Nov 1, 2018
ff23439
fixes
AkshayGadikar Nov 1, 2018
429d75c
fixes - port
AkshayGadikar Nov 1, 2018
a21a2f0
fixes
AkshayGadikar Nov 1, 2018
d0abeb7
fixes
AkshayGadikar Nov 1, 2018
847a6a3
fixes
AkshayGadikar Nov 1, 2018
994c13b
fixes
AkshayGadikar Nov 1, 2018
6649477
fixes
AkshayGadikar Nov 1, 2018
ef5e531
fixes
AkshayGadikar Nov 1, 2018
aa66d29
fixes
AkshayGadikar Nov 1, 2018
06cfcb3
ping details
AkshayGadikar Nov 1, 2018
c529630
ping details
AkshayGadikar Nov 1, 2018
061719a
ping details
AkshayGadikar Nov 1, 2018
d9bb115
ping details
AkshayGadikar Nov 1, 2018
a49d33f
fixes -1
AkshayGadikar Nov 1, 2018
255367c
Added ping message details
AkshayGadikar Nov 1, 2018
26701be
Added tracing details
AkshayGadikar Nov 1, 2018
f863a1b
Added tracing details - fix
AkshayGadikar Nov 1, 2018
ff86b3e
Added tracing details - fix
AkshayGadikar Nov 1, 2018
c02901d
Added tracing details - fix
AkshayGadikar Nov 1, 2018
a2bab01
Added tracing details - fix
AkshayGadikar Nov 2, 2018
fc8f84c
Added tracing details - fix
AkshayGadikar Nov 2, 2018
5d5768b
Added tracing details - fix
AkshayGadikar Nov 2, 2018
f79c087
Added Authorization token
AkshayGadikar Nov 2, 2018
e94b883
Added Authorization token - Fixes
AkshayGadikar Nov 2, 2018
6f5748c
Added Authorization token - Fixes
AkshayGadikar Nov 2, 2018
74f6d1f
Code clean
AkshayGadikar Nov 2, 2018
d67b8d2
Code clean
AkshayGadikar Nov 2, 2018
7ee0eb1
Code clean
AkshayGadikar Nov 2, 2018
afc2430
Added password
AkshayGadikar Nov 2, 2018
39033b6
Added password
AkshayGadikar Nov 2, 2018
c1a735c
Added password validation
AkshayGadikar Nov 2, 2018
37e797c
Added password validation
AkshayGadikar Nov 2, 2018
f077983
code clean
AkshayGadikar Nov 2, 2018
710f40e
Added Readme
AkshayGadikar Nov 2, 2018
e838a2b
Swagger Trigger
AkshayGadikar Nov 9, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/project-flogo/contrib
module github.com/AkshayGadikar/contrib

require (
github.com/carlescere/scheduler v0.0.0-20170109141437-ee74d2f83d82
Expand Down
49 changes: 49 additions & 0 deletions trigger/ping/examples/ping/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Flogo Ping Example

## Install

To install run the following commands:
```
flogo create -f flogo.json
cd Ping
flogo build
```

## Testing

Run:
```
bin/Ping
```

Then open another terminal and run:
```
curl http://localhost:9096/ping/details
```

You should then see something like:
```
{
"Version":"1.1",
"Appversion":"",
"Appdescription":""
}Details:{
"NumGoroutine":2,
"Alloc":762472,
"TotalAlloc":762472,
"Sys":69926912,
"Mallocs":1078,
"Frees":101,
"LiveObjects":977,
"NumGC":0
}
```
#####
```
curl http://localhost:9096/ping
```

You should then see something like:
```
{"response":"Ping successful"}
```
25 changes: 25 additions & 0 deletions trigger/ping/examples/ping/flogo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "Ping",
"type": "flogo:app",
"version": "1.0.0",
"title": "Ping Trigger",
"description": "This is a simple ping example.",
"triggers": [
{
"name": "ping",
"id": "flogo-ping",
"ref": "github.com/project-flogo/contrib/trigger/ping",
"description": "Ping Example",
"settings": {
"port": "9096",
"version": "1.1",
"appversion":"",
"appdescription":"",
"password":"Test12345"
},
"handlers": []
}
],
"actions": [],
"resources": []
}
59 changes: 59 additions & 0 deletions trigger/ping/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ping

import(
"runtime"
"encoding/json"
"strings"
)

const password = "Test12345"

type Settings struct {
Port int `md:"port,required"`
Version string `md:"version"`
AppVersion string `md:"appversion"`
AppDescription string `md:"appdescription"`
Password string `md:"password"`
}

type MemoryStats struct{
NumGoroutine int
Alloc,
TotalAlloc,
Sys,
Mallocs,
Frees,
LiveObjects uint64
NumGC uint32
}

func PrintMemUsage() string{
var rtm runtime.MemStats
runtime.ReadMemStats(&rtm)
var t MemoryStats

t.NumGoroutine = runtime.NumGoroutine()

// Misc memory stats
t.Alloc = rtm.Alloc
t.TotalAlloc = rtm.TotalAlloc
t.Sys = rtm.Sys
t.Mallocs = rtm.Mallocs
t.Frees = rtm.Frees

// Live objects = Mallocs - Frees
t.LiveObjects = t.Mallocs - t.Frees

//GC stats
t.NumGC = rtm.NumGC

result, _ := json.Marshal(t)
return string(result)
}

func Valid(token string) bool{
if strings.Compare(token, password) == 0{
return true
}
return false
}
131 changes: 131 additions & 0 deletions trigger/ping/trigger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package ping

import (
"fmt"
"strconv"
"encoding/json"
"io"
"net/http"
"github.com/project-flogo/core/trigger"
)

// DefaultPort is the default port for Ping service
const DefaultPort = "9096"

var triggerMd = trigger.NewMetadata(&Settings{})
var statsResult string

func init() {
trigger.Register(&Trigger{}, &Factory{})
}

type Factory struct {
}

// Metadata implements trigger.Factory.Metadata
func (*Factory) Metadata() *trigger.Metadata {
return triggerMd
}

// Trigger is the ping trigger
type Trigger struct {
metadata *trigger.Metadata
config *trigger.Config
response string
Server *http.Server
}

// New implements trigger.Factory.New
func (f *Factory) New(config *trigger.Config) (trigger.Trigger, error) {
type PingResponse struct {
Version string
Appversion string
Appdescription string
}
response := PingResponse{
Version: config.Settings["version"].(string),
Appversion: config.Settings["appversion"].(string),
Appdescription: config.Settings["appdescription"].(string),
}

data, err := json.Marshal(response)
if err != nil {
fmt.Errorf("Ping service data formation error",err)
}

port := strconv.Itoa(config.Settings["port"].(int))
if len(port) == 0 {
port = DefaultPort
}

mux := http.NewServeMux()
server := &http.Server{
Addr: ":" + port,
Handler: mux,
}
trigger := &Trigger{
metadata: f.Metadata(),
config: config,
response: string(data),
Server: server,
}
mux.HandleFunc("/ping", trigger.PingResponseHandlerShort)
mux.HandleFunc("/ping/details", trigger.PingResponseHandlerDetail)
statsResult = PrintMemUsage()
return trigger, nil
}

// Init implements trigger.Init
func (t *Trigger) Initialize(ctx trigger.InitContext) error {
return nil
}

func (t *Trigger) PingResponseHandlerShort(w http.ResponseWriter, req *http.Request) {
token := t.config.Settings["password"].(string)
if(Valid(token)) {
io.WriteString(w, "{\"response\":\"Ping successful\"}\n")
}else{
io.WriteString(w, "Error : Invalid User !! \n")
}
}

//PingResponseHandlerDetail handles simple response
func (t *Trigger) PingResponseHandlerDetail(w http.ResponseWriter, req *http.Request) {
token := t.config.Settings["password"].(string)
if(Valid(token)) {
io.WriteString(w, t.response + "\n")
io.WriteString(w, "Details :\n")
io.WriteString(w, statsResult + "\n")
}else{
io.WriteString(w, "Error : Invalid User !! \n")
}

//Another way to get trace : more details
/*
tr := trace.New("TraceTest", req.URL.Path)
defer tr.Finish()
fmt.Println(reflect.TypeOf(tr).String())
fmt.Println("Trace:")
fmt.Printf("%+v\n", tr)
fmt.Println(tr)*/
}

// Start implements util.Managed.Start
func (t *Trigger) Start() error {
go func() {
if err := t.Server.ListenAndServe(); err != http.ErrServerClosed {
fmt.Errorf("Ping service err:", err)
}
}()
return nil
}

// Stop implements util.Managed.Stop
func (t *Trigger) Stop() error {
if err := t.Server.Shutdown(nil); err != nil {
fmt.Errorf("[mashling-ping-service] Ping service error when stopping:", err)
return err
}
return nil
}

Loading