Skip to content

Commit

Permalink
Setup eslint configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
hlegay committed May 17, 2021
1 parent 67eb8ed commit 4dd7f70
Show file tree
Hide file tree
Showing 19 changed files with 900 additions and 264 deletions.
68 changes: 68 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"env": {
"node": true,
"browser": true,
"es6": true
},
"extends": [
"eslint:recommended",
"plugin:jest/recommended"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2021,
"sourceType": "module"
},
"plugins": [
"jest"
],
"rules": {
"comma-dangle": ["error", "always-multiline"],
"comma-spacing": ["error", { "before": false, "after": true }],
"indent": ["error", 2],
"keyword-spacing": ["error"],
"linebreak-style": ["error", "unix"],
"max-len": [
"error",
{
"ignoreRegExpLiterals": true,
"ignoreUrls": true,
"ignoreTemplateLiterals": true,
"code": 130
}
],
"no-unused-vars": ["error", { "ignoreRestSiblings": true }],
"object-curly-spacing": ["error", "always"],
"quotes": [
"error",
"single",
{
"avoidEscape": true,
"allowTemplateLiterals": true
}
],
"semi": ["error", "never"],
"sort-imports": [
"warn",
{
"ignoreCase": false,
"ignoreDeclarationSort": false,
"ignoreMemberSort": false,
"memberSyntaxSortOrder": ["none", "all", "multiple", "single"]
}
],
"arrow-parens": ["error", "as-needed"],
"eqeqeq": ["error", "smart"],
"space-in-parens": ["error", "never"],
"no-multiple-empty-lines": ["error", { "max": 1, "maxEOF": 0, "maxBOF": 0 }],
"jest/valid-title": "off",
"jest/no-export": "off",
"jest/expect-expect": "off"
}
}
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ RUN yarn install
RUN groupadd -r pptruser && useradd -r -g pptruser -G audio,video pptruser \
&& mkdir -p /home/pptruser/Downloads \
&& chown -R pptruser:pptruser /home/pptruser

USER pptruser
COPY --chown=pptruser . /app

ENTRYPOINT ["dumb-init", "--"]
CMD ["yarn", "start-dev"]
Expand All @@ -52,5 +54,6 @@ ENV NODE_ENV=production
RUN yarn build

USER pptruser
COPY --chown=pptruser . /app

CMD ["yarn", "start"]
22 changes: 16 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ FORCE_PUSH_OVERRIDE ?= 0
IMAGE_NAME = knplabs/server-side-renderer

.PHONY: dev
dev: .cp-env start
$(MAKE) .install-deps
dev: cp-env start
$(MAKE) install-deps

.PHONY: start
start:
Expand Down Expand Up @@ -32,20 +32,21 @@ push-latest: .validate-tag
docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${IMAGE_NAME}:latest
docker image push ${IMAGE_NAME}:latest

.PHONY: .cp-env
.cp-env:
.PHONY: cp-env
cp-env:
ifeq ($(STAGE),dev)
cp -n .env.dist .env
endif

.PHONY: .install-deps
.install-deps:
.PHONY: install-deps
install-deps:
ifeq ($(STAGE),dev)
docker-compose -f docker-compose.dev.yaml run --rm manager yarn install
else
@echo "You can't install app dependencies on non-dev environments.\n"
@exit 1
endif

.PHONY: .validate-tag
.validate-tag:
ifeq ($(IMAGE_TAG),)
Expand All @@ -61,3 +62,12 @@ ifeq ($(FORCE_PUSH_OVERRIDE),0)
exit 1; \
fi;
endif

.PHONY: lint
lint:
ifeq ($(STAGE),dev)
docker-compose -f docker-compose.dev.yaml run --rm manager yarn lint --fix
else
@echo "You can't run eslint on non-dev environments.\n"
@exit 1
endif
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,59 @@ Server-side-renderer
|-----------|--------|
| `master` | [![CircleCI](https://circleci.com/gh/KnpLabs/server-side-renderer/tree/master.svg?style=svg&circle-token=1feb6f789ade0c11ee8b87e90eadbe9e6778fcb7)](https://circleci.com/gh/KnpLabs/server-side-renderer/tree/master) |

Docker image to server side render html pages using a browser (supports SPAs)
## Description

Docker image to server side render html pages using a browser (supports SPAs)

## Requirements

1. docker >= v19.03
2. docker-compose >= 1.19.0

## Setup on local environment

There is a `.env.dist` at the root directory that can be clone into `.env`.
It's up to you to edit these env vars according to your local environment.

**Note**: the `make dev` command will copy the `.env.dist` file into `.env` if it does not exist.

### Installation

```sh
$ make dev
```

This command will build and start the docker containers and update dependencies.

## Make commands

| Command | Description |
| -------------------- | ---------------------------------------------------------------------------------|
| `dev` | Build and start the stack (Combination of `cp-env`, `start`, `install-deps`) |
| `cp-env` | Copy the `.env.dist` file into `.env` if it does not exist |
| `start` | Start the docker-compose stack |
| `stop` | Stop the docker-compose stack |
| `install-deps` | Install front dependencies |
| `build` | Build docker image (image tag required) |
| `push` | Push docker image on DockerHub (image tag required) |
| `push-latest` | Push docker image with latest tag (image tag required) |

## Make commands for run tests

| Command | Description |
| -------------------- | ------------------|
| `test` | Run unit tests |

## Make commands for run linter

| Command | Description |
| -------------------- | -------------------------------|
| `lint` | Run linter on the application |

## Maintainers

Anyone can contribute to this repository (and it's warmly welcomed!). The following
people maintain and can merge into this library:

- [@alexpozzi](https://github.com/alexpozzi)
- [@hlegay](https://github.com/hlegay)
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"@babel/node": "^7.13.13",
"@babel/preset-env": "^7.14.1",
"babel-jest": "^26.6.3",
"eslint": "^7.26.0",
"eslint-plugin-jest": "^24.3.6",
"jest": "^26.6.3",
"node-fetch": "^2.6.1",
"nodemon": "^2.0.7",
Expand All @@ -26,6 +28,7 @@
"start-dev": "nodemon --watch src/ --ignore 'src/**/*.test.js' --exec babel-node src/index.js",
"build": "babel src/ -d build/ --ignore 'src/**/*.test.js'",
"start": "node build/index.js",
"test": "jest"
"test": "jest",
"lint": "eslint \"./src\""
}
}
98 changes: 49 additions & 49 deletions src/configuration.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { LEVEL_INFO, levels as validLogLevels } from './logger'
import {
__,
T,
allPass,
complement,
compose,
filter,
includes,
isEmpty,
isNil,
map,
path,
pipe,
split,
trim,
unless,
T,
__,
allPass,
complement,
compose,
filter,
includes,
isEmpty,
isNil,
map,
path,
pipe,
split,
trim,
unless,
} from 'ramda'

// isLogConfigurationValid :: Configuration -> Boolean
Expand All @@ -31,52 +31,52 @@ const isWorkerConfigurationValid = T

// validate :: Configuration -> Boolean
const validate = allPass([
isLogConfigurationValid,
isQueueConfigurationValid,
isManagerConfigurationValid,
isWorkerConfigurationValid,
isLogConfigurationValid,
isQueueConfigurationValid,
isManagerConfigurationValid,
isWorkerConfigurationValid,
])

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

// generate :: _ -> Configuration
const generate = () => ({
log: {
level: process.env.LOG_LEVEL || LEVEL_INFO,
log: {
level: process.env.LOG_LEVEL || LEVEL_INFO,
},
queue: {
redis_dsn: process.env.QUEUE_REDIS_DSN,
},
manager: {
enabled: 1 === Number(process.env.MANAGER_ENABLED),
http_server: {
host: process.env.MANAGER_HTTP_SERVER_HOST || '0.0.0.0',
port: Number(process.env.MANAGER_HTTP_SERVER_PORT) || 8080,
},
queue: {
redis_dsn: process.env.QUEUE_REDIS_DSN,
},
manager: {
enabled: 1 === Number(process.env.MANAGER_ENABLED),
http_server: {
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),
renderer: {
authorized_request_domains: commaSeparatedStringToArray(
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 || ''
)
}
},
worker: {
enabled: 1 === Number(process.env.WORKER_ENABLED),
renderer: {
authorized_request_domains: commaSeparatedStringToArray(
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 || '',
),
},
},
})

// createConfiguration :: _ -> Configuration
export default pipe(
generate,
unless(validate, () => { throw new Error('Invalid configuration.') }),
generate,
unless(validate, () => { throw new Error('Invalid configuration.') }),
)
26 changes: 13 additions & 13 deletions src/e2e.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import 'regenerator-runtime/runtime' // needed by the SSR to be able to execute transpiled generator functions like async/await
import fetch from 'node-fetch'

const trimStringForComparison = str => str.replace(/^[\s]+|[\s]+$/gm, '').replace(/\n/g, '');
const trimStringForComparison = str => str.replace(/^[\s]+|[\s]+$/gm, '').replace(/\n/g, '')

describe('e2e :: static', () => {
it(`asserts that the page is rendered`, async () => {
const res = await fetch(`http://manager:8080/render?url=http://nginx/static.html`)
const content = await res.text()
it(`asserts that the page is rendered`, async () => {
const res = await fetch(`http://manager:8080/render?url=http://nginx/static.html`)
const content = await res.text()

expect(res.status).toBe(200)
expect(trimStringForComparison(content)).toBe(trimStringForComparison(`
expect(res.status).toBe(200)
expect(trimStringForComparison(content)).toBe(trimStringForComparison(`
<!DOCTYPE html>
<html lang="en">
<head>
Expand All @@ -21,16 +21,16 @@ describe('e2e :: static', () => {
</body>
</html>
`))
})
})
})

describe('e2e :: dynamic', () => {
it(`asserts that the page is rendered`, async () => {
const res = await fetch(`http://manager:8080/render?url=http://nginx/dynamic.html`)
const content = await res.text()
it(`asserts that the page is rendered`, async () => {
const res = await fetch(`http://manager:8080/render?url=http://nginx/dynamic.html`)
const content = await res.text()

expect(res.status).toBe(200)
expect(trimStringForComparison(content)).toBe(trimStringForComparison(`
expect(res.status).toBe(200)
expect(trimStringForComparison(content)).toBe(trimStringForComparison(`
<!DOCTYPE html>
<html lang="en">
<head>
Expand All @@ -56,5 +56,5 @@ describe('e2e :: dynamic', () => {
</body>
</html>
`))
})
})
})
Loading

0 comments on commit 4dd7f70

Please sign in to comment.