Skip to content

Commit

Permalink
Merge pull request #233 from Bitspark/kk-trigger-in-http
Browse files Browse the repository at this point in the history
Make it possible to serve operators with trigger `in` ports
  • Loading branch information
td5r committed Oct 7, 2019
2 parents 180f637 + 0fe9925 commit 0deb53f
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions cmd/slang/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
Expand Down Expand Up @@ -116,22 +118,34 @@ func runHttpPost(operator *core.Operator, bind string) {
HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
var incoming interface{}

if err := json.NewDecoder(req.Body).Decode(&incoming); err != nil {
err := json.NewDecoder(req.Body).Decode(&incoming)
switch {
// We do not have a POST-Body but we could still serve a result
// for the case when the `In` is a trigger.
case err == io.EOF:
if isQuasiTrigger(operator.Main().In()) {
operator.Main().In().Push(true)
outgoing := operator.Main().Out().Pull()
responseWithOk(resp, outgoing)
} else {
responseWithError(resp, errors.New("Missing data"), http.StatusBadRequest)
}
// We have an error while decoding the response
case err != nil:
responseWithError(resp, err, http.StatusBadRequest)
return
}

incoming = core.CleanValue(incoming)
if err := inDef.VerifyData(incoming); err != nil {
responseWithError(resp, err, http.StatusBadRequest)
return
// Everything is fine, validate the values and pass it to the running operator
default:
incoming = core.CleanValue(incoming)
if err := inDef.VerifyData(incoming); err != nil {
responseWithError(resp, err, http.StatusBadRequest)
return
}
operator.Main().In().Push(incoming)
outgoing := operator.Main().Out().Pull()
responseWithOk(resp, outgoing)
}

operator.Main().In().Push(incoming)

outgoing := operator.Main().Out().Pull()

responseWithOk(resp, outgoing)
})

operator.Main().Out().Bufferize()
Expand Down

0 comments on commit 0deb53f

Please sign in to comment.