servies
is a micro framework written in bash. It uses
netcat
to listen for and serve HTTP requests. Defining
request handlers is simply done by running a function with the same
name as the HTTP method, and passing it a url and command to execute.
For example, here's how you would create a new "/greet/" endpoint
that you could pass a name to and use in your response:
get"/greet/:name" echo'hi $name, how are you??'
As you can see, you can add path variables to your urls using syntax
that you might find familiar. Any word that starts with a
:
will be treated as a path variable which is then
available to you in the command.
You have full control over the details of your response body.
Anything sent to stdout
by your handler function or
command will be sent back as the response body. By default, your
response will be sent back as a 200 OK
plain/text
response, but there's also a status and a
header command that you can use to customize the status code and to
add any header you want:
get"/authenticated/:name" authenticate_then_get_data authenticate_then_get_data() { header"Content-Type" "application/json" if ["$name" ="marcos" ]; then status200 "OK" echo"{\"number\": 7}" else status403 "Forbidden" echo"{\"error\": \"invalid name!\"}" fi }
You get the following HTTP commands: get
,
post
, put
, and patch
. All of
which have this signature: name (name: string, ...cmd:
string)
. Along with that there is status
and
header
, and not_found
which is called when
no routes match an incoming request. This method can be overwritten
by you.
Getting started is easy. First, make sure you have a newer version of
bash install. This may mean using Linux. So first get Linux. Second,
download
and extract the code. Next run make dependencies
to
download external libraries we depend on. You can run make
install
to make the servies
command available
from anywhere, but this is not required. Once this is done you can
run ./servies sample.sh
to start the sample web app or
pass in your own source file to run your own app.
All help is appreciated. And even though this is a joke project there
are still some very valuable lessons to learn here about bash as a
language and projects in a language with a few restrictions. To
contribute, first make sure you've done everything in the "Getting
started" section and run make lint test
before pushing
your code. Unit tests are written using
expect
and
shellcheck
is the static analyser.