Skip to content

Commit

Permalink
Add browser request redirections
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpozzi committed May 25, 2021
1 parent d69b463 commit 8b58540
Show file tree
Hide file tree
Showing 13 changed files with 536 additions and 310 deletions.
2 changes: 1 addition & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ MANAGER_HTTP_SERVER_HOST=
WORKER_ENABLED=1
WORKER_RENDERER_AUTHORIZED_REQUEST_DOMAINS=
WORKER_RENDERER_AUTHORIZED_REQUEST_RESOURCES=
WORKER_RENDERER_REDIRECTED_DOMAINS=
WORKER_RENDERER_REDIRECTED_DOMAINS=external-nginx|nginx
4 changes: 4 additions & 0 deletions docker-compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ services:
image: nginx:1.20.0
volumes:
- ./fixtures/nginx/html:/usr/share/nginx/html:ro
- ./fixtures/nginx/templates:/etc/nginx/templates:ro

test:
image: knplabs/server-side-renderer:test-runner-dev
build:
context: .
target: dev
depends_on:
- manager
- nginx
command: yarn test
volumes:
- ./:/app
Expand Down
8 changes: 8 additions & 0 deletions docker-compose.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ services:
- redis
environment:
- MANAGER_ENABLED=1
- WORKER_ENABLED=0
- QUEUE_REDIS_DSN=redis://redis:6379

worker:
Expand All @@ -20,8 +21,10 @@ services:
depends_on:
- redis
environment:
- MANAGER_ENABLED=0
- WORKER_ENABLED=1
- QUEUE_REDIS_DSN=redis://redis:6379
- WORKER_RENDERER_REDIRECTED_DOMAINS=external-nginx|nginx

redis:
image: redis:6.2.2-buster
Expand All @@ -30,12 +33,17 @@ services:
image: nginx:1.20.0
volumes:
- ./fixtures/nginx/html:/usr/share/nginx/html:ro
- ./fixtures/nginx/templates:/etc/nginx/templates:ro

test:
image: knplabs/server-side-renderer:test-runner-test
build:
context: .
target: dev
depends_on:
- manager
- worker
- nginx
command: yarn test
volumes:
- ./src:/app/src:ro
Expand Down
23 changes: 23 additions & 0 deletions fixtures/nginx/html/redirection.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>A dynamic HTML page (redirections)</title>
</head>
<body>
<h1>A dynamic HTML page (redirections)</h1>
<script>
window.onload = event => {
fetch('http://external-nginx/dynamic.json')
.then(response => response.json())
.then(data => {
const newParagraph = document.createElement("p");
const newContent = document.createTextNode(data.content);
newParagraph.appendChild(newContent);

document.body.append(newParagraph)
});
};
</script>
</body>
</html>
25 changes: 25 additions & 0 deletions fixtures/nginx/templates/default.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
server {
listen 80;
listen [::]:80;
server_name localhost;

location / {
root /usr/share/nginx/html;
index index.html index.htm;

if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';

return 204;
}

add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
56 changes: 41 additions & 15 deletions src/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,49 @@ import {
__,
allPass,
anyPass,
both,
complement,
compose,
equals,
filter,
includes,
isEmpty,
isNil,
map,
path,
pipe,
reduce,
split,
trim,
unless,
} from 'ramda'

// isDefined :: Mixed -> Boolean
const isDefined = both(complement(isNil), complement(isEmpty))

// isLogConfigurationValid :: Configuration -> Boolean
const isLogConfigurationValid = compose(includes(__, validLogLevels), path(['log', 'level']))

// isQueueConfigurationValid :: Configuration -> Boolean
const isQueueConfigurationValid = compose(complement(isEmpty), path(['queue', 'redis_dsn']))
const isQueueConfigurationValid = compose(isDefined, path(['queue', 'redis_dsn']))

// isManagerConfigurationValid :: Configuration -> Boolean
const isManagerConfigurationValid = T

// isWorkerConfigurationValid :: Configuration -> Boolean
const isWorkerConfigurationValid = T
const isWorkerConfigurationValid = pipe(
path(['worker', 'renderer', 'domain_redirections']),
reduce(
(acc, { from, to }) => unless(
equals(false),
allPass([
() => isDefined(from),
() => isDefined(to),
]),
)(acc),
true,
),
)

// validate :: Configuration -> Boolean
const validate = allPass([
Expand All @@ -38,40 +56,48 @@ const validate = allPass([
isWorkerConfigurationValid,
])

// commaSeparatedStringToArray :: String -> String[]
const commaSeparatedStringToArray = pipe(
split(','),
// stringToArray :: String -> String -> [String]
const stringToArray = separator => pipe(
split(separator),
map(trim),
filter(complement(anyPass([isNil, isEmpty]))),
)

// commaSeparatedStringToArray :: String -> [String]
const commaSeparatedStringToArray = stringToArray(',')

// pipeSeparatedStringToArray :: String -> [String]
const pipeSeparatedStringToArray = stringToArray('|')

// generate :: _ -> Configuration
const generate = () => ({
log: {
level: process.env.LOG_LEVEL || LEVEL_INFO,
level: process.env.LOG_LEVEL ?? LEVEL_INFO,
},
queue: {
redis_dsn: process.env.QUEUE_REDIS_DSN,
},
manager: {
enabled: 1 === Number(process.env.MANAGER_ENABLED),
enabled: 1 === Number(process.env.MANAGER_ENABLED ?? 1),
http_server: {
host: process.env.MANAGER_HTTP_SERVER_HOST || '0.0.0.0',
port: Number(process.env.MANAGER_HTTP_SERVER_PORT) || 8080,
host: process.env.MANAGER_HTTP_SERVER_HOST ?? '0.0.0.0',
port: Number(process.env.MANAGER_HTTP_SERVER_PORT ?? 8080),
},
},
worker: {
enabled: 1 === Number(process.env.WORKER_ENABLED),
enabled: 1 === Number(process.env.WORKER_ENABLED ?? 1),
renderer: {
authorized_request_domains: commaSeparatedStringToArray(
process.env.WORKER_RENDERER_AUTHORIZED_REQUEST_DOMAINS || '*',
process.env.WORKER_RENDERER_AUTHORIZED_REQUEST_DOMAINS ?? '*',
),
authorized_request_resources: commaSeparatedStringToArray(
process.env.WORKER_RENDERER_AUTHORIZED_REQUEST_RESOURCES || '*',
),
domain_redirections: commaSeparatedStringToArray(
process.env.WORKER_RENDERER_REDIRECTED_DOMAINS || '',
process.env.WORKER_RENDERER_AUTHORIZED_REQUEST_RESOURCES ?? '*',
),
domain_redirections: pipe(
commaSeparatedStringToArray,
map(pipeSeparatedStringToArray),
map(([from, to]) => ({ from, to })),
)(process.env.WORKER_RENDERER_REDIRECTED_DOMAINS ?? ''),
},
},
})
Expand Down
Loading

0 comments on commit 8b58540

Please sign in to comment.