An Express-like HTTP router for Bash compatible with CGI 1.1
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
909 B

#!/bin/bash
source exprash.sh;
redirect_stdout 'log';
declare -A cats;
cats[calico]="Calico";
cats[sphynx]="Sphynx";
cats[ragdoll]="Ragdoll";
cats[holland_lop]="Holland Lop";
cats[scottish_fold]="Scottish Fold";
cats[orange]="Orange";
get '/' && {
html='<h1>Cats</h1><ul>';
for key in "${!cats[@]}"; do
html+="<li><a href=\"cats/${key}\">${cats[$key]}</a></li>";
done
html+='</ul>';
printf '%s' "$html" | send;
}
get '/cats/:cat' && {
key=$(param 'cat');
if [[ -v "cats[$key]" ]]; then
if [ "$key" == 'holland_lop' ]; then
next "That's not a cat!";
else
printf '<h1>Your Cat: %s</h1>\n' "${cats[$key]}" | send;
fi;
else
next;
fi;
}
get '/cats/*' && {
printf '<h1>Error: Cannot find that cat</h1>\n' | send;
}
get_error '/cats/*' && {
printf '<h1>%s</h1>' "$(get_error_message)" | send;
}
(use || use_error) && {
printf '<h1>404</h1>' | send;
}