|
| 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 | +} |
0 commit comments