Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Commit

Permalink
chore: add api code (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed Jan 24, 2022
1 parent 42d3118 commit 4523326
Show file tree
Hide file tree
Showing 52 changed files with 26,095 additions and 16 deletions.
171 changes: 171 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
###########################################################
### STANDARD ENV VARIABLES ################################
###########################################################

# Set to "production" for live system
NODE_ENV=development

### SERVICE ###############################################

# All REQUIRED variables

# NOTE: Needs to be kept at "0.0.0.0" if using Docker
# Defaults to resolving to localhost if not set
SERVICE_HOST=
SERVICE_PORT=8204

### CORS ##################################################

# Set "Access-Control-Allow-Origin" response header.
# Origin can be comma-delimited string, string, or boolean;
# if set to boolean true then reflect request origin
# (NOTE: this is NOT recommended for production as it enables reflection exploits).
# Defaults to false and disables CORS if not set
CORS_ORIGIN=

# Set "Access-Control-Allow-Headers" response header.
# Expects comma-delimited string i.e. "Content-Type, Authorization";
# Defaults to reflecting the headers specified in the
# request's "Access-Control-Request-Headers" header
CORS_ALLOWED_HEADERS="Accept, Accept-Encoding, Accept-Language, Authorization, Content-Type, Origin, X-Forwarded-For, X-Requested-With"

# Set "Access-Control-Allow-Credentials" response header
# Expects true or to be unset
CORS_ALLOW_CREDENTIALS=

# Set "Access-Control-Expose-Headers" response header.
# Expects comma-delimited string i.e. "Content-Range, X-Content-Range" or to be unset
CORS_EXPOSED_HEADERS=

# Set "Access-Control-Max-Age" response header.
# Expects integer or to be unset
CORS_MAX_AGE=

### DOCKER ################################################

# The max amount of processing time the Docker container
# can use of CPU.
# As an example, setting to 0.98 represents 98% of CPU
# processing time of a single core
# Defaults to 0 (use all available processing time)
DOCKER_APP_CPUS=

# The max amount of memory the Docker container can use.
# As an example, "50M"
# Defaults to 0 (use all available memory)
DOCKER_APP_MEMORY=

# Refer to https://docs.docker.com/compose/compose-file/compose-file-v3/#restart
# for options.
# Defaults to "unless-stopped"
DOCKER_RESTART_POLICY=

### HTTPS #################################################

# Will use PFX file/passphrase over SSL cert/key if both specified.
# Defaults to serving over HTTP if not set
HTTPS_PFX_PASSPHRASE=
HTTPS_PFX_FILE_PATH=
HTTPS_SSL_CERT_PATH=
HTTPS_SSL_KEY_PATH=

# Enable HTTP/2 support with HTTP/1.x fallback.
# Requires cert/key supporting TLS 1.2 or above.
# Expects true or to be unset
HTTPS_HTTP2_ENABLED=

### LOGGER ################################################

# Level can be "trace", "debug", "info", "warn", "error", or "fatal".
# Defaults to "info"
LOG_LEVEL=

# See https://momentjs.com/docs/#/displaying/format/ for
# date format options.
# Requires LOG_ROTATION_FILENAME to be set.
# Defaults to "YYYY-MM-DD"
LOG_ROTATION_DATE_FORMAT=

# Filename including full path used by the stream, example:
# "./logs/ydh-community-contacts-api-%DATE%.log"
# Defaults to STDOUT.
# Ignored if deployed with Docker Compose, as Docker's
# daemon handles logging and rotations
LOG_ROTATION_FILENAME=

# Frequency can be "daily", "custom", or "test". Defaults to "daily".
# "test" rotates every minute, set to "custom" if you want to rotate
# based on LOG_ROTATION_DATE_FORMAT.
# Requires LOG_ROTATION_FILENAME to be set.
# Ignored if deployed with Docker Compose.
# Defaults to "daily"
LOG_ROTATION_FREQUENCY=

# Max number of logs to keep. If not set, it will not remove past logs.
# Requires LOG_ROTATION_FILENAME to be set.
# Defaults to "10" if deployed with Docker Compose
LOG_ROTATION_MAX_LOGS=

# Max size of the file after which it will rotate. It can
# be combined with frequency or date format. The size units
# are "k", "m" and "g". Units need to directly follow
# a number e.g. 1g, 100m, 20k.
# Requires LOG_ROTATION_FILENAME to be set.
# Defaults to "100m" if deployed with Docker Compose
LOG_ROTATION_MAX_SIZE=

### PROCESS LOAD HANDLING #################################

# If one of the below thresholds is reached then the server
# will return a 503 response until the load has been relieved

# The max event loop delay in milliseconds
# Defaults to 0 (disabled)
PROC_LOAD_MAX_EVENT_LOOP_DELAY=

# The max Event Loop Utilization (ELU)
# As an example, setting to 0.98 represents 98%
# Defaults to 0 (disabled)
PROC_LOAD_MAX_EVENT_LOOP_UTILIZATION=

# The max heap size in bytes
# Defaults to 0 (disabled)
PROC_LOAD_MAX_HEAP_USED_BYTES=

# The max Resident Set Size (RSS) in bytes
# Defaults to 0 (disabled)
PROC_LOAD_MAX_RSS_BYTES=

### RATE LIMITING #########################################

# List of IPs that are excluded from rate limiting
RATE_LIMIT_EXCLUDED_ARRAY=

# Maximum number of connections per minute per client
# Defaults to 1000
RATE_LIMIT_MAX_CONNECTIONS_PER_MIN=

###########################################################
### PROJECT SPECIFIC ENV VARIABLES ########################
###########################################################

### API KEYS ##############################################

# Example: [{"service": "test", "value": "testtoken"}]
# "value" is the only mandatory value in each object in array.
# Leaving empty will disable bearer token auth
AUTH_BEARER_TOKEN_ARRAY=

### DATABASE CONNECTION ###################################

# All REQUIRED variables apart from DB_CLIENT

# Accepted values are "mssql" and "postgresql".
# Defaults to "mssql" if not set
DB_CLIENT=

# Examples:
# "mssql:https://username:password@localhost/database"
# "postgresql:https://username:password@localhost:5432/database"
# "Server=localhost,1433;Database=database;User Id=username;Password=password;Encrypt=true"
DB_CONNECTION_STRING=
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ on:
branches:
- master
paths-ignore:
- 'docs/**'
- '*.md'
- "docs/**"
- "*.md"
pull_request:
branches:
- master
paths-ignore:
- 'docs/**'
- '*.md'
- "docs/**"
- "*.md"
types: [opened, ready_for_review, reopened, synchronize]

jobs:
Expand Down Expand Up @@ -77,7 +77,7 @@ jobs:
postgres:
image: postgres:14-alpine
env:
POSTGRES_DB: community-contacts_crud_api
POSTGRES_DB: community_contacts_api
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
ports:
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This was built for use by interface engine channels/workflows that automatically
## Prerequisites

- [Node.js](https://nodejs.org/en/) >=14.0.0 (if running outside of Docker)
- [SQL Server](https://www.microsoft.com/en-gb/sql-server/sql-server-downloads) or [PostgreSQL](https://www.postgresql.org/download/) (either as services/instances or Docker containers)
- [SQL Server](https://www.microsoft.com/en-gb/sql-server/sql-server-downloads) >=13.x or [PostgreSQL](https://www.postgresql.org/download/) (either as services/instances or Docker containers)

## Setup

Expand All @@ -32,6 +32,8 @@ Perform the following steps before deployment:
3. Make a copy of `.env.template` in the root directory and rename it to `.env`
4. Configure the application using the environment variables in `.env`

**Note:** You will need to create a database prior to using it in the `DB_CONNECTION_STRING` environment variable (this does not apply if using the included Docker Compose file to deploy)

**Note:** Set the following environment variables in `.env` to meet NHS Digital's recommendation to retain 6 months' worth of logs:

- `LOG_ROTATION_DATE_FORMAT="YYYY-MM-DD"`
Expand Down Expand Up @@ -109,6 +111,10 @@ Contributions are welcome, and any help is greatly appreciated!
See [CONTRIBUTING.md](./CONTRIBUTING.md) for details on how to get started.
Please adhere to this project's [Code of Conduct](./CODE_OF_CONDUCT.md) when contributing.

## Acknowledgements

- **Michael McCormack** - MSSQL query optimisation

## License

`ydh-community-contacts-api` is licensed under the [MIT](./LICENSE) license.
14 changes: 7 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
services:
db:
container_name: ydh-community-contacts-crud-api-db
container_name: ydh-community-contacts-api-db
image: postgres:14-alpine
environment:
POSTGRES_DB: community-contacts_crud_api
POSTGRES_DB: community_contacts_api
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
ports:
- 5432:5432
volumes:
- ydh-community-contacts-crud-api-db-data:/var/lib/postgresql/data
- ydh-community-contacts-api-db-data:/var/lib/postgresql/data

app:
container_name: ydh-community-contacts-crud-api
container_name: ydh-community-contacts-api
build:
context: .
deploy:
Expand All @@ -22,7 +22,7 @@ services:
memory: "${DOCKER_APP_MEMORY:-0}"
env_file: .env
environment:
DB_CONNECTION_STRING: "postgresql:https://postgres:password@ydh-community-contacts-crud-api-db:5432/community-contacts_crud_api"
DB_CONNECTION_STRING: "postgresql:https://postgres:password@ydh-community-contacts-api-db:5432/community_contacts_api"
LOG_ROTATION_DATE_FORMAT:
LOG_ROTATION_FILENAME:
NODE_ENV: production
Expand All @@ -38,7 +38,7 @@ services:
# See https://github.com/opencontainers/image-spec/blob/master/annotations.md
org.opencontainers.image.authors: Frazer Smith <[email protected]>
org.opencontainers.image.licenses: MIT
org.opencontainers.image.url: https://github.com/Fdawgs/ydh-community-contacts-crud-api
org.opencontainers.image.url: https://github.com/Fdawgs/ydh-community-contacts-api
logging:
options:
max-file: "${LOG_ROTATION_MAX_LOGS:-10}"
Expand All @@ -49,5 +49,5 @@ services:
command: ["npm", "run", "start:migrate"]

volumes:
ydh-community-contacts-crud-api-db-data:
ydh-community-contacts-api-db-data:
external: false
1 change: 1 addition & 0 deletions migrations/mssql/001.do.set_ansi_nulls.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SET ANSI_NULLS ON
1 change: 1 addition & 0 deletions migrations/mssql/002.do.set_quoted_identifier.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SET QUOTED_IDENTIFIER ON
1 change: 1 addition & 0 deletions migrations/mssql/003.do.create_schema_lookup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IF NOT EXISTS ( SELECT name FROM sys.schemas WHERE name = N'lookup' ) EXEC('CREATE SCHEMA lookup');
11 changes: 11 additions & 0 deletions migrations/mssql/004.do.create_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
IF OBJECT_ID('lookup.contacts', 'U') IS NULL CREATE TABLE lookup.contacts
(
id uniqueidentifier NOT NULL DEFAULT newid(),
match_type VARCHAR(100) NOT NULL,
match_value VARCHAR(100) NOT NULL,
match_receiver VARCHAR(MAX) NOT NULL,
telecom NVARCHAR(MAX) NOT NULL,
created DATETIME2 NOT NULL DEFAULT CURRENT_TIMESTAMP,
last_updated DATETIME2 DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT ck_destination_match PRIMARY KEY (match_type, match_value)
);
13 changes: 13 additions & 0 deletions migrations/postgres/001.do.create_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE SCHEMA IF NOT EXISTS lookup;

CREATE TABLE IF NOT EXISTS lookup.contacts
(
id UUID NOT NULL DEFAULT GEN_RANDOM_UUID(),
match_type VARCHAR(100) NOT NULL,
match_value VARCHAR(100) NOT NULL,
match_receiver VARCHAR NOT NULL,
telecom JSONB NOT NULL,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT ck_destination_match PRIMARY KEY (match_type, match_value)
);
Loading

0 comments on commit 4523326

Please sign in to comment.