-
Notifications
You must be signed in to change notification settings - Fork 1
/
root.sh
executable file
·53 lines (40 loc) · 1.19 KB
/
root.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env bash
set -eu
STORE="$1"
ROUTE_PATH=${2:-"/"}
# we export to make it available in the tera template
export ROUTE_PATH=${ROUTE_PATH%/}
BASE="$(dirname "$0")"
cd "$BASE"
mkdir -p "$STORE"
meta_out() {
jo "$@" >&4
exec 4>&-
}
META="$(cat <&3)"
METHOD="$(jq -r .method <<<"$META")"
P="$(jq -r .path <<<"$META")"
P=${P%/}
if [[ "$METHOD" == "GET" && "$P" == "${ROUTE_PATH}" ]]; then
meta_out headers="$(jo "content-type"="text/html")"
jo request="$META" | tera --env --env-key ENV -i --template html/index.html --stdin
exit
fi
if [[ "$METHOD" == "GET" && "$P" == "${ROUTE_PATH}/messages" ]]; then
meta_out headers="$(jo "content-type"="text/event-stream")"
exec tail -F $STORE/messages.json |
xcat -- bash -c "sed 's/^/data: /g'; echo"
fi
if [[ "$METHOD" == "POST" && "$P" == "${ROUTE_PATH}/message" ]]; then
meta_out headers="$(jo "content-type"="text/html")"
jq -c --argjson meta "$META" '{
stamp: $meta.stamp,
ip: $meta.remote_ip,
port: $meta.remote_port,
message,
}' >>"$STORE"/messages.json
echo OK
exit
fi
meta_out status=404 headers="$(jo "content-type"="text/html")"
echo "Not Found:" $METHOD $P