Skip to content

Commit b268647

Browse files
pasdams8sg
authored andcommitted
Refactor template to separate packages, extract request handlers and add REST api
1 parent 30543ee commit b268647

34 files changed

Lines changed: 993 additions & 820 deletions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package config
2+
3+
import (
4+
"os"
5+
)
6+
7+
func ConsulDC() string {
8+
val := os.Getenv("consul_dc")
9+
if len(val) == 0 {
10+
val = "dc1"
11+
}
12+
return val
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package config
2+
3+
import (
4+
"os"
5+
)
6+
7+
func ConsulURL() string {
8+
val := os.Getenv("consul_url")
9+
if len(val) == 0 {
10+
val = "consul.faasflow:8500"
11+
}
12+
return val
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package config
2+
3+
import (
4+
"os"
5+
)
6+
7+
// GatewayURL return the gateway address from env
8+
func GatewayURL() string {
9+
gateway := os.Getenv("gateway")
10+
if gateway == "" {
11+
gateway = "gateway.openfaas:8080"
12+
}
13+
return gateway
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package config
2+
3+
import (
4+
"strconv"
5+
"time"
6+
)
7+
8+
func parseIntOrDurationValue(val string, fallback time.Duration) time.Duration {
9+
if len(val) > 0 {
10+
parsedVal, parseErr := strconv.Atoi(val)
11+
if parseErr == nil && parsedVal >= 0 {
12+
return time.Duration(parsedVal) * time.Second
13+
}
14+
}
15+
16+
duration, durationErr := time.ParseDuration(val)
17+
if durationErr != nil {
18+
return fallback
19+
}
20+
return duration
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"time"
6+
)
7+
8+
func ReadTimeout() time.Duration {
9+
return parseIntOrDurationValue(os.Getenv("read_timeout"), 10*time.Second)
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package config
2+
3+
import (
4+
"os"
5+
)
6+
7+
// TraceServer get the traceserver address
8+
func TraceServer() string {
9+
traceServer := os.Getenv("trace_server")
10+
if traceServer == "" {
11+
traceServer = "jaeger.faasflow:5775"
12+
}
13+
return traceServer
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"time"
6+
)
7+
8+
func WriteTimeout() time.Duration {
9+
return parseIntOrDurationValue(os.Getenv("write_timeout"), 10*time.Second)
10+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package executor
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"io/ioutil"
7+
"log"
8+
"net/http"
9+
10+
"github.com/s8sg/faas-flow/sdk/executor"
11+
)
12+
13+
func executeFlowHandler(w http.ResponseWriter, req *http.Request, id string, ex executor.Executor) ([]byte, error) {
14+
log.Printf("Requested %s %s\n", req.Method, req.URL)
15+
log.Printf("Executing flow %s\n", id)
16+
17+
var stateOption executor.ExecutionStateOption
18+
19+
body, err := ioutil.ReadAll(req.Body)
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
// TODO: fix this
25+
openFaasEx := ex.(*openFaasExecutor)
26+
27+
state := req.Header.Get("X-Faas-Flow-State")
28+
callbackURL := req.Header.Get("X-Faas-Flow-Callback-Url")
29+
openFaasEx.callbackURL = callbackURL
30+
if state == "" {
31+
rawRequest := &executor.RawRequest{}
32+
rawRequest.Data = body
33+
rawRequest.Query = req.URL.RawQuery
34+
rawRequest.AuthSignature = req.Header.Get("X-Hub-Signature")
35+
// Check if any request Id is passed
36+
if id != "" {
37+
rawRequest.RequestId = id
38+
}
39+
stateOption = executor.NewRequest(rawRequest)
40+
41+
} else {
42+
if id == "" {
43+
return nil, errors.New("request ID not set in partial request")
44+
}
45+
46+
openFaasEx.openFaasEventHandler.header = req.Header
47+
partialState, err := executor.DecodePartialReq(body)
48+
if err != nil {
49+
return nil, errors.New("failed to decode partial state")
50+
}
51+
stateOption = executor.PartialRequest(partialState)
52+
}
53+
54+
// Create a flow executor, OpenFaaSExecutor implements executor
55+
flowExecutor := executor.CreateFlowExecutor(ex, nil)
56+
resp, err := flowExecutor.Execute(stateOption)
57+
if err != nil {
58+
return nil, fmt.Errorf("failed to execute request. %s", err.Error())
59+
}
60+
headers := w.Header()
61+
headers["X-Faas-Flow-Reqid"] = []string{flowExecutor.GetReqId()}
62+
headers["X-Faas-Flow-Callback-Url"] = []string{callbackURL}
63+
64+
return resp, nil
65+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package executor
2+
3+
import "github.com/s8sg/faas-flow/sdk"
4+
5+
var (
6+
stateStore sdk.StateStore
7+
dataStore sdk.DataStore
8+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package executor
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
8+
"github.com/s8sg/faas-flow/sdk/executor"
9+
)
10+
11+
func flowStateHandler(w http.ResponseWriter, req *http.Request, id string, ex executor.Executor) ([]byte, error) {
12+
log.Printf("Get flow state: %s\n", id)
13+
14+
flowExecutor := executor.CreateFlowExecutor(ex, nil)
15+
state, err := flowExecutor.GetState(id)
16+
if err != nil {
17+
log.Printf(err.Error())
18+
return nil, fmt.Errorf("failed to get request state for %s, check if request is active", id)
19+
}
20+
21+
return []byte(state), nil
22+
}

0 commit comments

Comments
 (0)