snootforge

snootclub/boop

routes for snoots

snoot.club/boop

routes for snoots

boop is a library and program for handling routes in snoots.

given a snoot like cute.snoot.club, boop takes care of the routing of little sub-apps (or boops) (such as that created by @snootclub/create-canvas-sketch).

the contract boop relies on is that each of those sub-apps has a build script and a watch script available. if the sub-app does not handle a (request, response) it is expected to produce a website directory when built or watched. you can run boop watch in your main snoot directory, and all the boops in the boops/ directory will have their watch script run too. same goes for build.

boop(1)

the boop command takes one argument, the command. the command can be watch, install, or build. all this does is run the matching npm command in each of the snoot’s boops.

the boop command has a little tiny optional magic. if it is run inside an npm lifecycle, it can be run with no command argument and will take the current lifecycle as its command. this lets your snoot’s package.json’s scripts node look like this:

  1. "scripts": {
  2. "install": "boop",
  3. "build": "boop",
  4. "watch": "boop",
  5. "start": "micro index.js -l tcp://0.0.0.0:80"
  6. }

and i think that’s beautiful

boop(3)

the boop library exports a function that takes (request, response), so if you’re not doing anything else in your snoot but boops then your index.js file can be as smol as:

  1. module.exports = require("@snootclub/boop")

then it tries its hardest to boop your snoot. here’s what it does, given a request like: get chee.snoot.club/smile:

first it will check if there is a boops/smile/ directory in your snoot.

if there is a boops/smile/, it will check if that directory has a package.json with a main javascript file.

if there is a package.json#main, it will import the package and pass it (request, response) where request is a clone of the original request, with the url updated to remove /smile.

if there is not a package.json#main, it will check for a boops/smile/website directory and try to serve the request statically.

if all of these fail, then it will try to serve statically from the snoot’s main website/ directory.

if that fails, 404.

advanced usage!!

so if you want to take tighter control of your snoot, but still use boop, there are plenty of options.

in the simplest case you could do something like:

  1. // index.js
  2. let boop = require("@snootclub/boop")
  3. module.exports = (request, response) => {
  4. if (request.url == "/something-i-can-handle-alone") {
  5. return something(request, response)
  6. }
  7. return boop(request, response)
  8. }

lower level, there are route.js and symbols.js.

  1. // index.js
  2. let route = require("@snootclub/boop/route")
  3. let symbols = require("@snootclub/boop/symbols")
  4. module.exports = (request, response) => {
  5. let booped = route(request, response)
  6. switch (booped.type) {
  7. case symbols.static:
  8. let {
  9. // the boop's fully resolved website directory
  10. websiteDirectory,
  11. // the new request object with boop name removed from the url
  12. request,
  13. response
  14. } = booped
  15. return "this resolved to a static boop"
  16. case symbols.module:
  17. let {
  18. // the imported module
  19. module,
  20. // the result of calling module with (request, response)
  21. result,
  22. request,
  23. response
  24. } = booped
  25. return "this resolved to a module boop"
  26. case symbols.none:
  27. return "not a boop"
  28. }
  29. }

example

here’s how a booped snoot might look:

index.js:

  1. let boop = require("@snootclub/boop")
  2. module.exports = (request, response) =>
  3. boop(request, response)

package.json:

  1. {
  2. "name": "booper",
  3. "dependencies": {
  4. "micro": "latest",
  5. "@snootclub/boop": "latest"
  6. },
  7. "scripts": {
  8. "build": "boop",
  9. "watch": "boop",
  10. "install": "boop",
  11. "start": "micro index.js -l tcp://0.0.0.0:80"
  12. }
  13. }

website/index.html:

  1. <!doctype html>
  2. <h1>i am snoot</h1>

boops/honk/package.json:

  1. {
  2. "name": "honk",
  3. "dependencies": {
  4. "parcel-bundler": "latest",
  5. "@snootclub/boop": "latest"
  6. },
  7. "scripts": {
  8. "build": "parcel build index.html -d website",
  9. "watch": "parcel watch index.html -d website"
  10. }
  11. }

boops/honk/index.html:

  1. <!doctype html>
  2. <link rel=stylesheet href=./cascade.styl>
  3. <h1>
  4. i am honk
  5. </h1>

boops/honk/cascade.styl

  1. body
  2. background #f88
  3. color #e46
  4. text-align center
  5. h1
  6. font-size 2em