Skip to content

Commit

Permalink
Introduce dev server (#2)
Browse files Browse the repository at this point in the history
* .

* test deploy

* Update build-deploy.yml
  • Loading branch information
atas authored Oct 13, 2023
1 parent d667850 commit f8a89cf
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:

- name: Generate HTML
run: |
system/bin/generate_html.sh
system/bin/generate-html.sh
- name: Upload artifact
uses: actions/upload-artifact@v2
Expand Down
21 changes: 0 additions & 21 deletions .htaccess

This file was deleted.

6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ build-local-image:
docker build system/workflow-image -t atas-ssg-builder:latest

run-local-image:
docker run --rm -it -v $(shell pwd):/github/workspace atas-ssg-builder:latest
docker run --rm -it -v $(shell pwd):/workspace atas-ssg-builder:latest

dev-server:
docker run --rm -it --entrypoint /workspace/system/bin/dev-server-entrypoint.sh -p 8001:80 \
-v $(shell pwd):/workspace atas-ssg-builder:latest

# Updates the GHCR:latest image
update-workflow-image:
Expand Down
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ You can create any .php page at root directory or project or any subdirectory. `
`./my-custom.php` will be `yoursite.com/my-custom`
`./my-custom-dir/my-custom.php` will be `yoursite.com/my-custom-dir/my-custom`

Nginx and Apache `.htaccess` configurations are included to remove `.php` extension from URLs in your local dev
environment.
Nginx configuration includes extensionless-php directives to remove `.php` extension from URLs in your local dev
environment. Don't put `.php` extensions in the URLs and links, just put in files.

## Enabling GitHub Pages

Expand Down Expand Up @@ -95,24 +95,25 @@ AAAA 2606:50c0:8000::153
you can also add `your-github-username.github.io` as a CNAME record to your DNS records instead and still have a
custom domain, instead of the IP addresses above.

## Local Development Environment in a PHP server (Nginx or Apache)
You can use either apache with php or nginx with php-fpm locally to run your site. When run locally, there is no
build process, it's just PHP working server side. I won't dive into setting those up here.
## Local Development Environment

Do `composer update` locally to install dependencies before running locally. Build process does this automatically
but you need to do manually for local development.
You can spin up the docker container and let it serve the PHP site locally, without a build process. You need to
have `docker` installed on your machine.

### For Local Nginx
use the config file in dir `./system/workflow-image/nginx.conf` and follow the comments.
`cd` into the project directory

### For Local Apache
For local Apache, the .htaccess should be enough but also need to configure your virtual host in apache
configuration for the `local_hostname` domain name set in `config.json`.
run:
```
make dev-server
```

Ensure local hostname matches the `local_hostname` in `config.json` file. Add it to hosts file at `/etc/hosts` if
necessary with IP `127.0.0.1`
Alternatively, you don't have `make` installed, you can run below, and adjust the port `8001` to your liking:
```
docker run --rm -it --entrypoint /workspace/system/bin/dev-server-entrypoint.sh -p 8001:80 \
-v $(shell pwd):/workspace ghcr.io/atas/ssg-builder::latest
```

Locally, http works fine, https is used in build process with self-signed cert to generate URLs correctly.
Your local php site will be running at `http://localhost:8001` with instant updates on page refresh, as PHP does.


## Architectural Considerations
Expand Down
2 changes: 0 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
"site_top_h1": "ata's ssg",
"site_top_about_line1": "Simple PHP-based Static Site generator",
"site_top_about_line2": "Supports Markdown and deployed to GitHub Pages by GitHub Actions",
"hostname": "ssg-test.atasasmaz.com",
"local_hostname": "ssg-local.atasasmaz.com",
"email": "[email protected]",
"full_title": "Ata's SSG - a Simple PHP-based SSG for GitHub Pages",
"appended_title": " - Ata's SSG",
Expand Down
2 changes: 1 addition & 1 deletion layout/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
</div>

<footer>
<p>Copyright &copy; 2023 <?= $config->hostname ?></p>
<p>Copyright &copy; 2023
<div class="gdprInfo">
<!-- TODO: Add GDPR info or a GDPR banner if using advanced analytics -->
This site respects your privacy with GDPR-compliant, self-hosted and anonymised analytics without using cookies and sharing data.
Expand Down
8 changes: 4 additions & 4 deletions layout/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
<meta property="og:image:height" content="685"/>
<title><?= $title ?></title>
<?php
if (isLocalHost()) {
if (isBuildRunning()) {
?> <link rel="stylesheet" href="/assets/styles/style.css"> <?php
}
else {
?>
<link rel="stylesheet/less" type="text/css" href="/assets/styles/style.less">
<script src="https://cdn.jsdelivr.net/npm/less" ></script>
<?php
}
else {
?> <link rel="stylesheet" href="/assets/styles/style.css"> <?php
}
?>
<link rel="icon" type="image/jpeg" href="/assets/images/favicon-96.jpg">
<script>
Expand Down
15 changes: 15 additions & 0 deletions system/bin/dev-server-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

# Check if php-fpm is running
if ! pgrep "php-fpm" > /dev/null; then
php-fpm -D
fi

# Check if nginx is running
if ! pgrep "nginx" > /dev/null; then
service nginx start
fi

echo "Dev server should be running at http://localhost:8001 or any other port if you changed the default."

tail -f /dev/null
15 changes: 5 additions & 10 deletions system/bin/generate_html.sh → system/bin/generate-html.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,10 @@ if ! pgrep "nginx" > /dev/null; then
service nginx start
fi

hostname=$(jq -r '.hostname' config.json)

if ! grep -qF "$hostname" "/etc/hosts"; then
echo "$hostname" >> "/etc/hosts"
fi

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

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

# Check if the file exists and is not zero bytes
if [[ ! -s $2 ]]; then
Expand All @@ -53,6 +45,9 @@ function curl_and_save() {
rm -rf html
mkdir -p html

touch build.lock # we are running the build process.
trap "rm -rf build.lock" EXIT

# pages/*.md HTML BUilding
for file in pages/*.md; do
filename=$(basename "$file" .md)
Expand Down
38 changes: 37 additions & 1 deletion system/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,40 @@ function get_all_posts(): array
header('HTTP/1.0 404 Not Found');
include_once '404.php';
exit;
}
}

/**
* Gets the current full hostname with protocol
* @return string
*/
function getCurrentHostname(): string
{
// Check if HTTPS or HTTP is being used
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? "https" : "http";

// Get the server port
$port = $_SERVER['SERVER_PORT'];

// Depending on whether the port is standard for the protocol, include it in the URL
if (($protocol === 'http' && $port == 80) || ($protocol === 'https' && $port == 443)) {
// Standard ports for HTTP and HTTPS, respectively. No need to include the port in the URL.
$currentUrl = "{$_SERVER['HTTP_HOST']}";
} else {
// Non-standard port, include it in the URL.
$currentUrl = "{$_SERVER['HTTP_HOST']}:{$port}";
}

return $currentUrl;
}

/**
* Gets the current full hostname with protocol
* @return string
*/
function getCurrentHostnameWithProtocol(): string
{
// Check if HTTPS or HTTP is being used
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? "https" : "http";

return "{$protocol}://" . getCurrentHostname();
}
21 changes: 3 additions & 18 deletions system/layoutUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ function selectedTabCss(string $tab): string
}

/**
* Update config.json's local_hostname, in local host we use less.js in-browser compilation.
* Are we running like a PHP site or if the build pipeline running this?
* @return bool
*/
function isLocalHost(): bool
function isBuildRunning(): bool
{
global $config;
return $_SERVER['HTTP_HOST'] == $config->local_hostname;
return file_exists(__DIR__ . "/../build.lock");
}

/**
Expand All @@ -38,17 +37,3 @@ function getCurrentFullUrl(): string

return $currentUrl;
}

/**
* Gets the current full hostname with protocol
* @return string
*/
function getCurrentHostnameWithProtocol(): string
{
// Check if HTTPS or HTTP is being used
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? "https" : "http";

// Construct the full URL
$currentUrl = $protocol . "://" . $_SERVER['HTTP_HOST'];
return $currentUrl;
}
2 changes: 1 addition & 1 deletion system/markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function get_md_config(): array
return [
'allow_unsafe_links' => false,
'external_link' => [
'internal_hosts' => [$config->hostname, $config->local_hostname],
'internal_hosts' => [getCurrentHostname()],
'open_in_new_window' => true,
'html_class' => 'external-link',
'nofollow' => '',
Expand Down
44 changes: 37 additions & 7 deletions system/workflow-image/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
server {
# You can use 80 localhost.
# We want ssl during build process to mitigate GitHub Pages so links are correct.
listen 443 ssl;

# for localhost make your local domain match to local_hostname
# in config.json in top level.
# server_name local.atasasmaz.com;

# Comment out for local development
ssl_certificate /etc/nginx/nginx-selfsigned.crt;
ssl_certificate_key /etc/nginx/nginx-selfsigned.key;

# Update your project path for local development
root /workspace;
index index.php index.html;

Expand Down Expand Up @@ -46,3 +39,40 @@ server {
}

}

server {
listen 80;

root /workspace;
index index.php index.html;

error_page 404 /404.php;

location ~ ^/p/(.+)$ {
rewrite ^/p/(.+)$ /post.php?slug=$1 last;
}

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;
}

try_files $uri $uri.html $uri/ @try-render-page;
index index.html index.htm index.php;
}

location @try-render-page {
rewrite ^/([a-zA-Z0-9-]+)$ /page.php?page=$1 last;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000; # assuming PHP-FPM is running on this address
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}

0 comments on commit f8a89cf

Please sign in to comment.