-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reply to Web API run with result (#2108)
* make Web API `code/run` pass `webReply :: WebInvocationState -> IO ()` to TUI * send InProgress or Rejected * once REPLDone send Complete * add remote REPL shell script * closes #1426 This uses the "IO wrapper" trick from #2098 - thus we can be sure that TUI will be the producer and Web API the consumer. Example: ```bash cabal run swarm -O0 -- --scenario blank # in another terminal curl -XGET --header "Content-Type: text/plain;charset=utf-8" localhost:5357/code/run --data "1 + 1" ``` ```JSON {"InProgress":[]} {"Complete":"it1 : Int = 2"} ``` You can also test it with the shell script, that tries to act as a REPL and strips the JSON unless it gets an error: ``` ❯ scripts/remote-repl.sh > move > scan down it0 : Unit + Text = inl () > grab // exception written to log is not shown here > grob { "ParseError": "1:1: Unbound variable grob\n" } > ```
- Loading branch information
Showing
8 changed files
with
240 additions
and
59 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/usr/bin/env bash | ||
|
||
PORT="5357" | ||
HOST="localhost" | ||
|
||
function help { | ||
echo "Swarm remote REPL" | ||
echo | ||
echo "This is a shortcut for sending to Swarm REPL via cURL and recieving responses:" | ||
echo "curl -XGET --header \"Content-Type: text/plain;charset=utf-8\" --data \"build {}\" $HOST:$PORT/code/run" | ||
echo | ||
echo "Options:" | ||
echo " -p PORT --port PORT Specify the port, default is $PORT." | ||
echo " -n NAME --hostaname NAME Specify the hostaname, default is $HOST." | ||
echo " -h --help Show this helpful text." | ||
} | ||
|
||
function parse_args { | ||
while [[ $# -gt 0 ]]; do | ||
case $1 in | ||
-p|--port) | ||
PORT="$2" | ||
shift # past argument | ||
shift # past value | ||
;; | ||
-n|--hostname) | ||
HOST="$2" | ||
shift # past argument | ||
shift # past value | ||
;; | ||
-h|--help) | ||
help | ||
exit 0 | ||
;; | ||
*) | ||
echo "Unknown argument $1" | ||
shift # past argument | ||
;; | ||
esac | ||
done | ||
} | ||
|
||
IN_PROGRESS="" | ||
function print_elipsis { | ||
echo -n "..." | ||
IN_PROGRESS="\r \r" | ||
} | ||
|
||
function remove_elipsis { | ||
echo -ne "$IN_PROGRESS" | ||
IN_PROGRESS="" | ||
} | ||
|
||
function repl { | ||
while true; do | ||
remove_elipsis | ||
read -r -e -p "> " expr | ||
curl -N -s -XGET --header "Content-Type: text/plain;charset=utf-8" --data "$expr" "$HOST:$PORT/code/run" | while read -r line ; do | ||
remove_elipsis | ||
if jq -e 'has("InProgress")' <<< "$line" > /dev/null; then | ||
print_elipsis | ||
elif jq -e 'has("Complete")' <<< "$line" > /dev/null; then | ||
RESULT="$(jq -r '.Complete' <<< "$line")" | ||
[ -n "$RESULT" ] && echo "$RESULT"; | ||
elif jq -e 'has("Rejected")' <<< "$line" > /dev/null; then | ||
jq -C '.Rejected' <<< "$line" | ||
else | ||
echo "$line" | ||
fi | ||
done | ||
done | ||
} | ||
|
||
function main { | ||
parse_args "$@" | ||
repl | ||
} | ||
|
||
main "$@" |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
-- | | ||
-- SPDX-License-Identifier: BSD-3-Clause | ||
-- | ||
-- The type of commands sent from Web API handlers to the Controller, | ||
-- and the type of replies. | ||
module Swarm.TUI.Model.WebCommand ( | ||
WebCommand (..), | ||
WebInvocationState (..), | ||
RejectionReason (..), | ||
) where | ||
|
||
import Data.Aeson (FromJSON (..), ToJSON (..), genericParseJSON, genericToJSON) | ||
import Data.Text (Text) | ||
import GHC.Generics (Generic) | ||
import Swarm.Util.JSON (optionsMinimize) | ||
|
||
data WebCommand = RunWebCode {webEntry :: Text, webReply :: WebInvocationState -> IO ()} | ||
|
||
data RejectionReason = NoActiveGame | AlreadyRunning | ParseError String | ||
deriving (Eq, Ord, Show, Generic) | ||
|
||
data WebInvocationState = Rejected RejectionReason | InProgress | Complete String | ||
deriving (Eq, Ord, Show, Generic) | ||
|
||
-- -------------------------- | ||
-- ToJSON/FromJSON Instances | ||
-- -------------------------- | ||
|
||
instance ToJSON RejectionReason where | ||
toJSON = genericToJSON optionsMinimize | ||
|
||
instance FromJSON RejectionReason where | ||
parseJSON = genericParseJSON optionsMinimize | ||
|
||
instance ToJSON WebInvocationState where | ||
toJSON = genericToJSON optionsMinimize | ||
|
||
instance FromJSON WebInvocationState where | ||
parseJSON = genericParseJSON optionsMinimize |
Oops, something went wrong.