-
Notifications
You must be signed in to change notification settings - Fork 0
/
real-fake-backend.sh
executable file
·65 lines (54 loc) · 1.63 KB
/
real-fake-backend.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
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(readlink -f "$0" | xargs dirname)"
readonly SCRIPT_DIR
readonly HOST_DEFAULT="127.0.0.1"
readonly PORT_DEFAULT="8080"
HTTP_RESPONSE_FILE_DEFAULT="${SCRIPT_DIR}/static/http_response_200.txt"
readonly HTTP_RESPONSE_FILE_DEFAULT
serve() {
local host="$1"
local port="$2"
local http_response="$3"
while true; do
echo "$http_response" | nc -N -l "$host" "$port"
done
}
usage() {
cat << EOF
Usage: $(basename "$0") [OPTIONS]
Run a fake server bound to specified host and port and returning a file defined HTTP response
Options:
-H, --host <HOST> Default 127.0.0.1
-p, --port <PORT> Default 8080
-f, --http-file <HTTP RESPONSE FILE> Default $HTTP_RESPONSE_FILE_DEFAULT
Example:
$(basename "$0") -H 127.0.0.1 -p 8081 -f /home/alice/http_response_custom.txt
EOF
}
main() {
local host="$HOST_DEFAULT"
local port="$PORT_DEFAULT"
local http_response_file="$HTTP_RESPONSE_FILE_DEFAULT"
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-H|--host)
host="$2"
shift && shift;;
-p|--port)
port="$2"
shift && shift;;
-f|--http-file)
http_response_file="$2"
shift && shift;;
*)
usage
exit 1
esac
done
printf "Serving HTTP with netcat [host=%s] [port=%s] [http-file=%s]\n" "$host" "$port" "$http_response_file"
printf "Avaiblable at http://%s:%s\n" "$host" "$port"
serve "$host" "$port" "$(<"$http_response_file")"
}
main "$@"