shochu/router.sh

44 lines
600 B
Bash
Raw Permalink Normal View History

2022-01-09 18:38:22 +00:00
#!/bin/bash
_methods=()
_paths=()
_handlers=()
_routeNo=0
_404="_default404"
route() {
_methods+=("$1")
_paths+=("$2")
_handlers+=("$3")
_routeNo=$((_routeNo+1))
}
_default404() {
status 404
endHeaders
echo "File not found"
}
_matchPaths() {
2022-03-30 19:33:14 +00:00
local actual="$1"
local search="$2"
2022-01-09 18:38:22 +00:00
# TODO: add wildcards
test "$actual" = "$search"
return
}
router() {
for ((i=0; i<$_routeNo; i++ )); do
if test "${_methods[$i]}" != "$REQUEST_METHOD"; then
continue
fi
if _matchPaths "$pathInfo" "${_paths[$i]}"; then
"${_handlers[$i]}" # todo add arguments
return
fi
done
"$_404"
}