Skip to content

Commit 1747c65

Browse files
committed
write the docs
1 parent 47f2646 commit 1747c65

File tree

6 files changed

+153
-15
lines changed

6 files changed

+153
-15
lines changed

core.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ writeHttpResponse() {
367367
end_headers
368368
output() {
369369
while true; do
370-
inotifywait -e MODIFY -r pages &> /dev/null
370+
inotifywait -e MODIFY -r pages static &> /dev/null
371371
event "reload"
372372
done
373373
}

examples/docs/data/0-index.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,31 @@ lang: en-US
55

66
## Quick Start
77

8-
Create a new bash stack app:
8+
create a new bash stack app:
99
```
1010
curl -SsL create.bashsta.cc | bash
1111
```
1212

13-
## How?
13+
## Dependencies
1414

15-
well, there are a few dependencies:
15+
there are a few dependencies:
1616

1717
- bash 4.0+
1818
- [tcpserver](http://cr.yp.to/ucspi-tcp/tcpserver.html) (from ucspi-tcp package)
19-
- coreutils (tested mostly with gnu so far)
19+
- gnu coreutils
2020

21-
check out the examples folder for.. examples
21+
optionally...
22+
23+
- nodejs (for [tailwind](https://tailwindcss.com/) support)
24+
- docker (for deployment)
25+
26+
## Overview
27+
28+
Bash Stack uses **file-based routing**. All of the application routes should exist as
29+
`.sh` files in the `pages/` folder of the project.
30+
31+
Whenever an HTTP request is made, the framework will locate the corresponding script
32+
and execute it. Anything written to `stdout` by the script will be written to the HTTP response body.
33+
34+
Bash Stack also pairs well with [htmx](https://htmx.org/), which is included by default.
35+
We strongly recommend familizarizing yourself with [their examples](https://htmx.org/examples/) before proceeding.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Configuration
3+
lang: en-US
4+
...
5+
6+
## General Options
7+
8+
You can define any environment variables you want in `config.sh`. There are a few that the framework
9+
will check:
10+
11+
- `HIDE_LOGO` - if this is set, the "powered by bash" logo will be hidden
12+
- `NO_STYLES` - if this is set, no CSS will be linked in the `<head>`
13+
14+
## Tailwind
15+
16+
In order to enable the tailwind support, the environment variable `TAILWIND` must be set (this will
17+
be set by default in `config.sh` if you use the tailwind starter template)
18+
19+
There is also an [example tailwind-enabled project](https://github.com/cgsdev0/bash-stack/tree/main/examples/tailwind) available.

examples/docs/data/2-routing.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
title: Routing
3+
lang: en-US
4+
...
5+
6+
## Matching
7+
8+
Routes are matched with the following priority (highest at the top):
9+
10+
- Simple routes
11+
- Dynamic routes
12+
- Catch-all routes
13+
14+
For a more thorough example, please check out the [example router project](https://github.com/cgsdev0/bash-stack/tree/main/examples/router).
15+
16+
## Simple Routes
17+
18+
Simple routes are routes with no variables. For example:
19+
20+
```
21+
pages
22+
├── index.sh
23+
└── list.sh
24+
```
25+
26+
Both of these are simple routes, matching requests to `/` and `/list`.
27+
28+
## Dynamic Routes
29+
30+
Dynamic routes have one or more variables, represented by files with square brackets.
31+
32+
```
33+
pages
34+
├── multiple
35+
│   └── [variables]
36+
│   └── [route].sh
37+
```
38+
39+
As an example, both requests to `/multiple/a/b` and `/multiple/1/2` would match this route.
40+
41+
## Catch-All Routes
42+
43+
Catch-all routes have match one or more route segments.
44+
45+
```
46+
pages
47+
├── catchall
48+
│   └── [...example].sh
49+
```
50+
51+
As an example, both requests to `/catchall/a` and `/catchall/a/b` would match this route.
52+
53+
You can also make a catch-all segment **optional** to match 0 or more segments, like so:
54+
55+
```
56+
pages
57+
├── catchall
58+
│   └── [[...example]].sh
59+
```
60+
61+
This route would match all of the following request paths:
62+
63+
- `/catchall`
64+
- `/catchall/`
65+
- `/catchall/a`
66+
- `/catchall/a/b/c`
67+
68+
## Static Files
69+
70+
By default, bash stack will server files in the `static/` folder. These files are available by making
71+
`GET` requests to `/static/{filename}`.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: API Reference
3+
lang: en-US
4+
...
5+
6+
## Directives
7+
8+
The first line of each route script is checked for a "directive". These are special
9+
comments that let you override the way a route is handled internally.
10+
11+
These are the directives that currently exist:
12+
13+
- `# sse` - this route will be used for [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) [(example)](https://github.com/cgsdev0/bash-stack/blob/main/examples/jeopardy-buzzer/pages/sse/%5Busertype%5D/%5Broom%5D.sh)
14+
- `# allow-uploads` - this route will allow file uploads via a multipart encoded form. [(example)](https://github.com/cgsdev0/bash-stack/blob/main/examples/file-upload/pages/upload.sh)
15+
- `# headers` - this route will be responsible for writing its own headers section, in addition to the response body. [(example)](https://github.com/cgsdev0/bash-stack/blob/main/examples/jeopardy-buzzer/pages/login.sh)
16+
17+
18+
## Request Variables
19+
20+
The following variables are available in all route handlers:
21+
22+
- `REQUEST_METHOD` - the HTTP verb used
23+
- `REQUEST_PATH` - the relative path of the request
24+
- `REQUEST_QUERY` - the raw (unparsed) query string
25+
26+
The framework will automatically parse the request and populate the following associative arrays:
27+
28+
- `HTTP_HEADERS` - The parsed request headers
29+
- `QUERY_PARAMS` - The parsed query parameter string
30+
- `FORM_DATA` - The parsed form data (if applicable)
31+
- `PATH_VARS` - The parsed variable names for dynamic and catch-all routes
32+
- `COOKIES` - The parsed cookies from the request headers
33+
34+
The following are only used if you are writing an upload handler:
35+
36+
- `FILE_UPLOADS` - A mapping of input names -> tmp files
37+
- `FILE_UPLOAD_TYPES` - A mapping of input names -> file upload types (according to the request)
38+
- `FILE_UPLOAD_NAMES` - A mapping of input names -> original filenames

examples/docs/pages/[[...route]].sh

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,18 @@ if [[ ! -f "$MARKDOWN_FILE" ]]; then
1010
return $(status_code 404)
1111
fi
1212

13-
TOC=""
14-
if [[ "$STRIPPED" != "$INDEX" ]]; then
15-
TOC="--toc"
16-
else
1713
if [[ ! -f cache/global_toc ]] || [[ ! -z "$NO_CACHE" ]]; then
18-
GLOBAL_TOC="<ul>"
14+
GLOBAL_TOC="<div id='sidebar'><ul>"
1915
for FILE in data/*.md; do
2016
FNAME="$(basename $FILE)"
2117
FNAME="${FNAME%.md}"
2218
GENERATED="$(pandoc --from markdown --to html --standalone --wrap=preserve --highlight-style=breezeDark $FILE --toc)"
2319
GLOBAL_TOC+="<li>"
24-
GLOBAL_TOC+="<a href='#'>$(echo "$GENERATED" | xmllint --html --xpath '//*[@id="title-block-header"]/h1/text()' - 2> /dev/null)</a>"
20+
GLOBAL_TOC+="<a href='/$FNAME'>$(echo "$GENERATED" | xmllint --html --xpath '//*[@id="title-block-header"]/h1/text()' - 2> /dev/null)</a>"
2521
GLOBAL_TOC+="$(echo "$GENERATED" | xmllint --html --xpath '//*[@id="TOC"]/ul' - 2> /dev/null | sed 's@href="@href="/'$FNAME'@g')"
2622
GLOBAL_TOC+="</li>"
2723
done
28-
GLOBAL_TOC+="</ul>"
24+
GLOBAL_TOC+="</ul></div>"
2925
GLOBAL_TOC="$(echo "$GLOBAL_TOC" | tr -d '\n')"
3026

3127
# save to cache
@@ -37,8 +33,8 @@ else
3733
debug "cache hit"
3834
GLOBAL_TOC="$(cat cache/global_toc)"
3935
fi
40-
fi
36+
4137
htmx_page << EOF
42-
$(pandoc --from markdown --to html --standalone --wrap=preserve --highlight-style=breezeDark $MARKDOWN_FILE $TOC \
38+
$(pandoc --from markdown --to html --standalone --wrap=preserve --highlight-style=breezeDark $MARKDOWN_FILE \
4339
| sed "s@</header>@</header>$GLOBAL_TOC@")
4440
EOF

0 commit comments

Comments
 (0)