An Express-like HTTP router for Bash compatible with CGI 1.1
Go to file
2022-09-29 23:35:02 -06:00
examples Added route wildcards, error handlers, header management and test suite 2022-09-29 22:52:56 -06:00
src Fixed use of incorrect variable 2022-09-29 23:35:02 -06:00
tests Added route wildcards, error handlers, header management and test suite 2022-09-29 22:52:56 -06:00
.gitignore Added route wildcards, error handlers, header management and test suite 2022-09-29 22:52:56 -06:00
README.md Added route wildcards, error handlers, header management and test suite 2022-09-29 22:52:56 -06:00
run_tests.sh Added route wildcards, error handlers, header management and test suite 2022-09-29 22:52:56 -06:00

Exprash

An Express-like HTTP router for Bash compatible with CGI 1.1

I'm aware that the name of this project evokes a feeling of physical disgust. That is by design.

Usage/Examples

Here is some leaked source code for my brand new Cat website. Do not steal!

#!/bin/bash
source exprash.sh;

redirectStdout '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;
}

getError '/cats/*' && {
  printf '<h1>%s</h1>' "$(errorMessage)" | send;
}

(use || useError) && {
  printf '<h1>404</h1>' | send;
}

License

WTFPL