visit
At we build out many APIs using Go and Node some of which are hosted on DigitalOcean and take advantage of Docker containers, but we also use the Amazon Stack, like Lambda, S3, and Cloudfront with Apex & Serverless. We have many tools to help us startup projects, like when developing static sites or when setting up a repository on Github. While working on ash-github I joked about how everything should be written in bash. The more my coworkers and I joked about it, the more the idea grew in my head and soon the idea came to fruition.
xinetd man page
# route based response/ock — text/plain/ock.html — text/html/ock.json — application/json
# query param based response/ock?type=plain — text/plain/ock?type=html — text/html/ock?type=json — application/json
If you request any other route than one listed above you’ll get redirected to /ock. You’ll also have seen that there’s three content-types: text/plain, text/html, and application/json. The logic for the routing and query parameter consumption was originally simple and purely used regular expressions, though the response time was sluggish. I created a performance script for regular expressions & conditionals and tested it out locally and on the server. There was a significant difference, if/else conditionals were ~52.7% faster than regular expressions! Because of this I refactored using conditionals, but sadly there’s no noticeable performance boost with only a 100–200 millisecond difference in the request time.
# favicon/favicon — Beer Emoji/favicon.ico — Beer Emoji
The response itself is constructed using a handful of functions, a few of them are: beertime and respond. The beertime function handles the time calculations and message to be used in the body. The latter constructs and replies with the appropriate response while only consuming two parameters: content-type & body. To calculate your timezone, for proper beer consumption of course, I send a request to a separate service that contains the requestors IP Address which responds with your timezone. I then set the environment variable TZ in the subshell I spawn when executing beertime. The output from beertime is consumed by another function called respond which sets the headers and echoes the whole response back to the client.