Skip to content

Commit

Permalink
works
Browse files Browse the repository at this point in the history
  • Loading branch information
atas committed Oct 13, 2023
1 parent 038ee28 commit 8a22602
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 19 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# You would only need to use these if you want to build your own custom builder image.
# Otherwise, by default the workflow uses the pre-built image from ghcr.io/atas/ssg-builder:latest

.PHONY: build-local-image update-workflow-image run-local-image

build-local-image:
docker build system/workflow-image -t atas-ssg-builder:latest

Expand All @@ -10,8 +12,9 @@ update-workflow-image:
docker buildx build --platform linux/amd64 system/workflow-image -t ghcr.io/atas/ssg-builder:latest
docker push ghcr.io/atas/ssg-builder:latest


.PHONY: run-local-image
tag-workflow-image:
docker tag ghcr.io/atas/ssg-builder:latest ghcr.io/atas/ssg-builder:$(shell date +%Y%m%d%H%M)
docker push ghcr.io/atas/ssg-builder:$(shell date +%Y%m%d%H%M)

run-local-image:
docker run --rm -it -v $(shell pwd):/github/workspace atas-ssg-builder:latest
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,12 @@ Ensure local hostname matches the `local_hostname` in `config.json` file. Add it
necessary with IP `127.0.0.1`

Locally, http works fine, https is used in build process with self-signed cert to generate URLs correctly.


## Architectural Considerations

### Caching ./pages and ./posts for performance in local env

If you have a lot of pages and posts, local development environment will be likely slow. We can introduce caching to
speed it up, but if you have over a hundred pages and posts, you are probably better off with one of the more
advanced static site generators instead of this little tool.
38 changes: 24 additions & 14 deletions system/bin/generate_html.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#!/bin/bash

echo "Creating symbolic link /workspace to $(pwd)"
ln -s "$(pwd)" /workspace
# Create a symbolic link to the current directory
# if it doesn't exist
if [[ ! -L "/workspace" ]]; then
echo "Creating symbolic link /workspace to $(pwd)"
ln -s "$(pwd)" /workspace
fi

# Check if php-fpm is running
if ! pgrep "php-fpm" > /dev/null; then
Expand All @@ -21,24 +25,29 @@ fi

echo 127.0.0.1 "$hostname" >> /etc/hosts

# Curl the URL $1 and save it to $2
function curl_and_save() {
echo Hitting URL "$1"
curl -k "$1" > "$2"
echo "curl_and_save https://$hostname$1 > $2"
curl -ks --fail "https://$hostname$1" > "$2"

# Check if the file exists and is not zero bytes
if [[ ! -s $2 ]]; then
echo "Error: The file does not exist or is zero bytes."
exit 1
fi

# Check if the file is missing the specific text string
if ! grep -q "<!-- Built with Ata's SSG https://www.github.com/atas/ssg -->" "$2"; then
echo "Error: The file does not contain the attribution HTML comment OR something else is wrong in the output. As the MIT license requirement, please do not remove it to help other fellow developers discover this neat tool."
# Check if file exists
if [[ ! -e "$2" ]]; then
echo "Error: File $2 does not exist."
exit 1
fi
}

curl_and_save "https://$hostname/" "html/index.html"
# Check if file size is zero
if [[ ! -s "$2" ]]; then
echo "Error: File $2 is empty."
exit 1
fi
}

#region Building HTML files
rm -rf html
Expand All @@ -47,20 +56,21 @@ mkdir -p html
# pages/*.md HTML BUilding
for file in pages/*.md; do
filename=$(basename "$file" .md)
curl_and_save "https://$hostname/$filename" "html/$filename.html"
curl_and_save "/$filename" "html/$filename.html"
done

# posts/*.md HTML Building
mkdir -p html/p
for file in posts/*.md; do
filename=$(basename "$file" .md)
slug=$(awk -F': ' '/^slug:|^[ \t]*"slug":/ {gsub(/["\r]/, "", $2); print $2}' $file)
curl_and_save "https://$hostname/p/$slug" "html/p/$slug.html"
curl_and_save "/p/$slug" "html/p/$slug.html"
done

# **/*.php Any php file HTML Building
find . -name "*.php" ! -path "./system/*" | while read -r file; do
if [[ "$file" == "index.php" || "$file" == "page.php" || "$file" != "post.php" ]]; then
find . -name "*.php" ! -path "./system/*" ! -path "./vendor/*" ! -path "./layout/*" | while read -r file; do
file="${file#./}" #Remove the leading ./
if [[ "$file" == "page.php" || "$file" == "post.php" ]]; then #pages and posts are handled above separately
continue;
fi

Expand All @@ -72,7 +82,7 @@ find . -name "*.php" ! -path "./system/*" | while read -r file; do
mkdir -p "$(dirname "$html_file")"

filename=$(basename "$file" .php)
curl_and_save "https://$hostname/$without_extension" "$html_file"
curl_and_save "/$without_extension" "$html_file"
done

#endregion
Expand Down
6 changes: 4 additions & 2 deletions system/workflow-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ RUN apt-get update && apt-get install -y \
git \
jq \
procps \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install gd pdo_mysql
vim \
&& docker-php-ext-configure gd --with-freetype --with-jpeg

# Remove default Nginx configuration
RUN rm -rf /etc/nginx/sites-available/default \
Expand All @@ -29,6 +29,8 @@ RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/nginx

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN echo "alias ll='ls -lha'" >> /root/.bashrc

LABEL org.opencontainers.image.source = "https://github.com/atas/ssg"

ENTRYPOINT ["/entrypoint.sh"]
2 changes: 1 addition & 1 deletion system/workflow-image/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ server {
location / {
# If matching .php file found, rewrite to that, e.g. custom-page.php for /custom-page
if (-f $request_filename.php) {
rewrite /$1.php last;
rewrite ^(.*)$ $1.php last;
}

try_files $uri $uri.html $uri/ @try-render-page;
Expand Down

0 comments on commit 8a22602

Please sign in to comment.