Skip to content

Commit 112678b

Browse files
Merge branch 'main' into EDU-2000-Updates-delta-docs
2 parents 40c0260 + cc2e85e commit 112678b

47 files changed

Lines changed: 3662 additions & 311 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.circleci/config.yml

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ jobs:
8787
environment:
8888
PORT: 3001
8989
STACK: heroku-24
90-
ENABLE_BASIC_AUTH: "true"
9190
steps:
9291
- checkout
9392
- attach_workspace:
@@ -112,49 +111,31 @@ jobs:
112111
name: Test content request auth tokens
113112
command: |
114113
export PATH="$PWD/bin:$PWD/buildpack/build/.heroku-buildpack-nginx/ruby/bin:$PATH"
115-
mkdir -p logs/nginx
116114
./bin/assert-content-auth.sh
117-
- run:
118-
name: Test nginx configuration
119-
command: |
120-
export PATH="$PWD/bin:$PWD/buildpack/build/.heroku-buildpack-nginx/ruby/bin:$PATH"
121-
erb config/nginx.conf.erb > config/nginx.conf
122-
mkdir -p logs/nginx
123-
nginx -p . -c config/nginx.conf -t
124-
- run:
125-
name: Start nginx
126-
command: |
127-
export PATH="$PWD/bin:$PWD/buildpack/build/.heroku-buildpack-nginx/ruby/bin:$PATH"
128-
mkdir -p logs/nginx
129-
SKIP_HTTPS=false ./bin/start-nginx
130-
background: true
131-
- run:
132-
name: Wait for nginx
133-
command: |
134-
until $(curl --retry 10 --output /dev/null --silent --head --fail http://localhost:3001/); do
135-
printf '.'
136-
sleep 1
137-
done
138115
- run:
139116
name: Test basic auth for review apps
140117
command: |
118+
export PATH="$PWD/bin:$PWD/buildpack/build/.heroku-buildpack-nginx/ruby/bin:$PATH"
141119
./bin/assert-basic-auth.sh /docs/getting-started/quickstart
142120
- run:
143121
name: Smoke test trailing slash redirects
144122
command: |
123+
export PATH="$PWD/bin:$PWD/buildpack/build/.heroku-buildpack-nginx/ruby/bin:$PATH"
145124
./bin/assert-success.sh /docs/getting-started/quickstart
146125
./bin/assert-redirect.sh /docs/getting-started/quickstart/ \
147-
https://localhost/docs/getting-started/quickstart
126+
http://localhost/docs/getting-started/quickstart
148127
- run:
149128
name: Smoke test redirect_from redirects
150129
command: |
130+
export PATH="$PWD/bin:$PWD/buildpack/build/.heroku-buildpack-nginx/ruby/bin:$PATH"
151131
./bin/assert-redirect.sh /docs/how-ably-works \
152132
http://localhost/docs/key-concepts
153133
./bin/assert-redirect.sh /docs/rest/history \
154134
http://localhost/docs/storage-history/history
155135
- run:
156136
name: Test website redirects
157137
command: |
138+
export PATH="$PWD/bin:$PWD/buildpack/build/.heroku-buildpack-nginx/ruby/bin:$PATH"
158139
./bin/assert-redirect.sh /tutorials https://ably.com/tutorials
159140
160141
workflows:

.github/workflows/review-app.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
}
5757
5858
- name: Manage Heroku Review App
59-
uses: fastruby/manage-heroku-review-app@v1
59+
uses: fastruby/manage-heroku-review-app@v1.3
6060
with:
6161
action: ${{ (github.event.action == 'labeled' && github.event.label.name == 'review-app' && 'create') || 'destroy' }}
6262
env:

bin/assert-basic-auth.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/bash
22

3+
source "$(dirname "$0")/nginx-utils.sh"
4+
trap stop_nginx EXIT
5+
36
#
47
# A utility script to assert basic auth is working correctly
58
#
@@ -14,6 +17,10 @@ if [[ -z $URI_PATH ]]; then
1417
exit 1
1518
fi
1619

20+
export ENABLE_BASIC_AUTH=true
21+
22+
start_nginx
23+
1724
# Test without credentials (should get 401)
1825
NO_AUTH_CODE=$(curl --header "X-Forwarded-Proto: https" \
1926
--silent --output /dev/null \

bin/assert-content-auth.sh

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
#!/usr/bin/env bash
1+
#!/bin/bash
2+
3+
source "$(dirname "$0")/nginx-utils.sh"
4+
trap stop_nginx EXIT
25

36
set -euo pipefail
47
# set -x # uncomment to help debugging
@@ -10,58 +13,6 @@ set -euo pipefail
1013
# Make sure basic auth is disabled for these tests as it will interfere with the content request auth tokens
1114
export ENABLE_BASIC_AUTH=false
1215

13-
# First we define some utility functions to help us in the test
14-
15-
wait_for_nginx_pid_file() {
16-
local operation=$1 # "start" or "stop"
17-
local count=0
18-
19-
while ( [ "$operation" = "start" ] && [ ! -f /tmp/nginx.pid ] ) || \
20-
( [ "$operation" = "stop" ] && [ -f /tmp/nginx.pid ] ); do
21-
# nginx operations are quick so we can be impatient here
22-
sleep 0.1
23-
count=$((count + 1))
24-
if [ $count -ge 100 ]; then # 0.1s * 100 = 10s
25-
echo "Error: Timeout waiting for nginx to $operation"
26-
exit 1
27-
fi
28-
done
29-
}
30-
31-
start_nginx() {
32-
# Start nginx in the background
33-
./bin/start-nginx &
34-
35-
# Wait for nginx to start successfully (pid file is created)
36-
wait_for_nginx_pid_file "start"
37-
38-
# Get the PID from the nginx.pid file
39-
NGINX_PID=$(cat /tmp/nginx.pid)
40-
41-
# Check if nginx is still running
42-
if ! kill -0 $NGINX_PID 2>/dev/null; then
43-
echo "Error: Failed to start nginx"
44-
exit 1
45-
fi
46-
}
47-
48-
stop_nginx() {
49-
# Read the PID from the file
50-
if [ -f /tmp/nginx.pid ]; then
51-
NGINX_PID=$(cat /tmp/nginx.pid)
52-
# Kill nginx if it's still running
53-
if kill -0 $NGINX_PID 2>/dev/null; then
54-
kill $NGINX_PID
55-
fi
56-
57-
# Wait for nginx to stop (pid file to be removed)
58-
wait_for_nginx_pid_file "stop"
59-
fi
60-
}
61-
62-
# Set up trap to stop nginx on script exit or failure
63-
trap stop_nginx EXIT
64-
6516
# Function to run a single test case
6617
run_test() {
6718
local path="$1"

bin/assert-redirect.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/bash
22

3+
source "$(dirname "$0")/nginx-utils.sh"
4+
trap stop_nginx EXIT
5+
36
#
47
# A utility script to assert redirects served up by nginx
58
#
@@ -16,6 +19,8 @@ if [[ -z $FROM || -z $TO ]]; then
1619
exit 1
1720
fi
1821

22+
start_nginx
23+
1924
# Add basic auth if enabled
2025
AUTH_OPTS=""
2126
if [[ "${ENABLE_BASIC_AUTH}" == "true" ]]; then

bin/assert-success.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/bash
22

3+
source "$(dirname "$0")/nginx-utils.sh"
4+
trap stop_nginx EXIT
5+
36
#
47
# A utility script to assert successful responses served up by nginx
58
#
@@ -14,6 +17,8 @@ if [[ -z $URI_PATH ]]; then
1417
exit 1
1518
fi
1619

20+
start_nginx
21+
1722
# Add basic auth if enabled
1823
AUTH_OPTS=""
1924
if [[ "${ENABLE_BASIC_AUTH}" == "true" ]]; then

bin/nginx-utils.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bash
2+
3+
# bin/nginx-utils.sh
4+
# Shared utility functions for starting, stopping, and waiting for nginx in test scripts.
5+
# Usage: source this file in your script, then call start_nginx, stop_nginx, etc.
6+
7+
wait_for_nginx_pid_file() {
8+
local operation=$1 # "start" or "stop"
9+
local count=0
10+
11+
while ( [ "$operation" = "start" ] && [ ! -f /tmp/nginx.pid ] ) || \
12+
( [ "$operation" = "stop" ] && [ -f /tmp/nginx.pid ] ); do
13+
sleep 0.1
14+
count=$((count + 1))
15+
if [ $count -ge 100 ]; then # 0.1s * 100 = 10s
16+
echo "Error: Timeout waiting for nginx to $operation" >&2
17+
return 1
18+
fi
19+
done
20+
return 0
21+
}
22+
23+
start_nginx() {
24+
mkdir -p logs/nginx
25+
./bin/start-nginx &
26+
if ! wait_for_nginx_pid_file "start"; then
27+
echo "Error: nginx did not start" >&2
28+
return 1
29+
fi
30+
NGINX_PID=$(cat /tmp/nginx.pid)
31+
if ! kill -0 $NGINX_PID 2>/dev/null; then
32+
echo "Error: Failed to start nginx (PID $NGINX_PID not running)" >&2
33+
return 1
34+
fi
35+
return 0
36+
}
37+
38+
stop_nginx() {
39+
if [ -f /tmp/nginx.pid ]; then
40+
NGINX_PID=$(cat /tmp/nginx.pid)
41+
if kill -0 $NGINX_PID 2>/dev/null; then
42+
kill $NGINX_PID
43+
fi
44+
if ! wait_for_nginx_pid_file "stop"; then
45+
echo "Error: nginx did not stop" >&2
46+
return 1
47+
fi
48+
fi
49+
return 0
50+
}

config/.htpasswd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
preview:$2y$05$lbdXr/rHqkB2cDDZx4x9OOagjzznFBnD3ZOijLD6B7sz..7pmgkrC
1+
preview:$apr1$IHsSHCLH$qJMp.hhrLSPDB/.f/yJUn/

config/nginx.conf.erb

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,22 @@ http {
2424

2525
server_tokens off;
2626

27+
log_format logfmtably
28+
'nginx '
29+
'request_time=$request_time '
30+
'request_id=$http_x_request_id '
31+
'status=$status '
32+
'remote_addr=$remote_addr '
33+
'remote_port=$remote_port '
34+
'server_addr=$server_addr '
35+
'bytes_sent=$bytes_sent body_bytes_sent=$body_bytes_sent '
36+
'request_length=$request_length '
37+
'scheme=$scheme request_method=$request_method '
38+
'request_uri="$request_uri" host="$http_host" '
39+
'referrer="$http_referer" user_agent="$http_user_agent" '
40+
'content_type="$sent_http_content_type" accept="$http_accept"';
2741
log_format l2met 'measure#nginx.service=$request_time request_id=$http_x_request_id';
28-
access_log <%= ENV['NGINX_ACCESS_LOG_PATH'] || 'logs/nginx/access.log' %> l2met;
42+
access_log <%= ENV['NGINX_ACCESS_LOG_PATH'] || 'logs/nginx/access.log' %> logfmtably;
2943
error_log <%= ENV['NGINX_ERROR_LOG_PATH'] || 'logs/nginx/error.log' %> notice;
3044

3145
include mime.types;
@@ -39,7 +53,7 @@ http {
3953
map_hash_bucket_size 8192;
4054

4155
# Creates a map of redirects for us
42-
map $uri $redirected_url {
56+
map $request_uri $redirected_url {
4357
default "none";
4458
include website-redirects.conf;
4559
include client-lib-development-guide-redirects.conf;
@@ -207,10 +221,10 @@ http {
207221
<% if content_request_protected %>
208222
# Serve the file if it exists, otherwise try to authenticate
209223
# (.html requests won't match here, they'll go to the @html_auth location)
210-
try_files $uri @html_auth;
224+
try_files $request_uri @html_auth;
211225
<% else %>
212226
# Serve the file if it exists, try index.html for paths without a trailing slash, otherwise 404
213-
try_files $uri $uri/index.html $uri/ =404;
227+
try_files $request_uri $request_uri/index.html $request_uri/ =404;
214228
<% end %>
215229
}
216230

@@ -222,14 +236,14 @@ http {
222236
# .html request without authentication, so handle it here by 404ing or redirecting to the canonical host
223237
# depending on whether CONTENT_REQUEST_CANONICAL_HOST is set or not
224238
<% if host = ENV['CONTENT_REQUEST_CANONICAL_HOST'] %>
225-
return 301 <%= ENV['SKIP_HTTPS'] == 'true' ? '$scheme' : 'https' %>://<%= host %>$uri;
239+
return 301 <%= ENV['SKIP_HTTPS'] == 'true' ? '$scheme' : 'https' %>://<%= host %>$request_uri;
226240
<% else %>
227241
return 404;
228242
<% end %>
229243
}
230244

231245
# If the request is authenticated, break out of the location block and serve the file
232-
try_files $uri.html $uri/index.html $uri/ =404;
246+
try_files $request_uri.html $request_uri/index.html $request_uri/ =404;
233247
}
234248

235249
# Don't serve files with the .html extension here, send them to the canonical location
@@ -242,7 +256,7 @@ http {
242256
# .html request without authentication, so handle it here by 404ing or redirecting to the canonical host
243257
# depending on whether CONTENT_REQUEST_CANONICAL_HOST is set or not
244258
<% if host = ENV['CONTENT_REQUEST_CANONICAL_HOST'] %>
245-
return 301 <%= ENV['SKIP_HTTPS'] == 'true' ? '$scheme' : 'https' %>://<%= host %>$uri;
259+
return 301 <%= ENV['SKIP_HTTPS'] == 'true' ? '$scheme' : 'https' %>://<%= host %>$request_uri;
246260
<% else %>
247261
return 404;
248262
<% end %>

0 commit comments

Comments
 (0)