-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhttp_server_ex
executable file
·40 lines (31 loc) · 1.1 KB
/
http_server_ex
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
#!/bin/bash
#
# Example that corresponds to "HTTP Server" on https://go.dev/play/.
# License for the corresponding code https://go.dev/LICENSE?m=text.
readonly DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. ${DIR}/../../gobash
function hello() {
local -r res="${1}"
local -r req="${2}"
shift 2
$res write "Hello, playground"
}
function main() {
local -r http=$(Http "127.0.0.1" "9003")
$http handle_func "/hello" "${DIR}/$(basename ${BASH_SOURCE})" "hello"
log_i "Starting server..."
$http listen_and_serve
sleep 2
log_i "Sending request and reading response..."
curl "http://127.0.0.1:9003/hello" 2>&1 || \
{ log_e "Could not access the server."; return $EC; }
$http kill_and_wait || \
{ log_e "Issue on the server side."; return $EC; }
}
# "if" is needed as the script is loaded when `hello` is executed.
if [[ "${0}" == *"http_server_ex" ]]; then
http_enabled || \
{ echo "http is not enabled"; exit 0; }
unset LOG_FILE
main "$@"
fi