Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood authored Nov 12, 2024
0 parents commit 1516265
Show file tree
Hide file tree
Showing 60 changed files with 8,026 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .babelrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"sourceType": "unambiguous",
"presets": [
[
"@babel/preset-env",
{
"targets": {
"chrome": 114
}
}
]
],
"plugins": []
}
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.dockerignore
.git
.gitkeep
docker-compose.yml
Dockerfile
README.md
node_modules
.babelrc.json
.eslintrc.js
.flake8
.gitignore
.prettierignore
stylelint.config.mjs
24 changes: 24 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: ["eslint:recommended"],
overrides: [
{
env: {
node: true,
},
files: [".eslintrc.{js,cjs}"],
parserOptions: {
sourceType: "script",
},
},
],
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
rules: {},
ignorePatterns: ["webpack.config.js", "*.min.js"],
};
6 changes: 6 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[flake8]
ignore = E203, E266, E501, W503, F403, F401
exclude = venv*,__pycache__,node_modules,migrations
max-line-length = 80
max-complexity = 12
select = B,C,E,F,W,T4,B9
11 changes: 11 additions & 0 deletions .github/actions/node-tests/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Run Node tests

runs:
using: "composite"
steps:
- name: Install Node dependencies
run: npm install
shell: bash
- name: Run Node tests
run: npm run test:all
shell: bash
23 changes: 23 additions & 0 deletions .github/actions/python-tests/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Run Python tests

runs:
using: "composite"
steps:
- uses: actions/setup-python@v5
with:
python-version: 3.12
- uses: snok/install-poetry@v1
with:
version: 1.8.1
virtualenvs-create: true
virtualenvs-in-project: true
virtualenvs-path: .venv
- name: Install Poetry dependencies
run: poetry install --no-interaction --no-root --with dev
shell: bash
- name: Collect statics assets
run: poetry run python manage.py collectstatic
shell: bash
- name: Run Python tests
run: poetry run python manage.py test
shell: bash
24 changes: 24 additions & 0 deletions .github/workflows/branch-cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Clean up feature branch

on:
delete:

jobs:
delete:
if: github.event.ref_type == 'branch' && startsWith(github.event.ref, 'feature/')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Get tag from deleted branch
id: version-tag
run: echo "VERSION=$(echo "${{ github.event.ref }}" | sed -e 's,/,-,g')" >> "$GITHUB_OUTPUT"
- name: Debug
run: echo "Clean up Docker image ${{ vars.DOCKER_IMAGE_NAME }}:${{ steps.version-tag.outputs.VERSION }}"
- name: Delete image
if: ${{ steps.version-tag.outputs.VERSION }}
uses: bots-house/[email protected]
with:
owner: ${{ github.repository_owner }}
name: ${{ vars.DOCKER_IMAGE_NAME }}
token: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ steps.version-tag.outputs.VERSION }}
52 changes: 52 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Build and deploy

on:
workflow_dispatch:
push:

concurrency:
group: cd-${{ github.ref }}

jobs:
test-node:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests
uses: ./.github/actions/node-tests

test-python:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests
uses: ./.github/actions/python-tests

version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Get tag
id: version-tag
uses: nationalarchives/ds-docker-actions/.github/actions/get-version-tag@main
outputs:
version: ${{ steps.version-tag.outputs.version-tag }}

build:
runs-on: ubuntu-latest
needs:
- test-python
- test-node
- version
permissions:
packages: write
contents: read
steps:
- uses: actions/checkout@v4
- name: Build Docker image
uses: nationalarchives/ds-docker-actions/.github/actions/docker-build@main
with:
version: ${{ needs.version.outputs.version }}
latest: ${{ github.ref == 'refs/heads/main' }}
github-token: ${{ secrets.GITHUB_TOKEN }}
docker-image-name: ${{ vars.DOCKER_IMAGE_NAME }}
22 changes: 22 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Pull request

on:
pull_request:
types:
- opened
- synchronize

jobs:
test-node:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests
uses: ./.github/actions/node-tests

test-python:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests
uses: ./.github/actions/python-tests
16 changes: 16 additions & 0 deletions .github/workflows/remove-untagged.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Remove untagged container images

on:
workflow_dispatch:
schedule:
- cron: "0 3 * * 1"

jobs:
remove-untagged:
runs-on: ubuntu-latest
steps:
- name: Remove untagged Docker images
uses: nationalarchives/ds-docker-actions/.github/actions/remove-untagged@main
with:
docker-image-name: ${{ vars.DOCKER_IMAGE_NAME }}
github-token: ${{ secrets.GITHUB_TOKEN }}
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.DS_Store
__pycache__
.pytest_cache
db.sqlite3
/static
/node_modules
/app/static/assets/*
/app/static/*.css
/app/static/*.css.map
/app/static/*.min.js
/app/static/*.min.js.map
!.gitkeep
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lts/jod
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/*.html
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
ARG IMAGE=ghcr.io/nationalarchives/tna-python-django
ARG IMAGE_TAG=latest

FROM "$IMAGE":"$IMAGE_TAG"

ENV NPM_BUILD_COMMAND=compile
ARG BUILD_VERSION
ENV BUILD_VERSION="$BUILD_VERSION"

# Copy in the application code
COPY --chown=app . .

# Install dependencies
RUN tna-build

# Copy in the static assets from TNA Frontend
RUN mkdir /app/app/static/assets; \
cp -r /app/node_modules/@nationalarchives/frontend/nationalarchives/assets/* /app/app/static/assets; \
poetry run python /app/manage.py collectstatic --no-input --clear

# Delete source files, tests and docs
RUN rm -fR /app/src /app/test /app/docs

# Run the application
CMD ["tna-run", "config.wsgi:application"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 The National Archives, UK

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# TNA Python Flask Application

## Quickstart

```sh
# Build and start the container
docker compose up -d
```

### Add the static assets

During the first time install, your `app/static/assets` directory will be empty.

As you mount the project directory to the `/app` volume, the static assets from TNA Frontend installed inside the container will be "overwritten" by your empty directory.

To add back in the static assets, run:

```sh
docker compose exec app cp -r /app/node_modules/@nationalarchives/frontend/nationalarchives/assets /app/app/static
```

### Run tests

```sh
docker compose exec dev poetry run python /app/manage.py test
```

### Format and lint code

```sh
docker compose exec dev format
```

## Environment variables

In addition to the [base Docker image variables](https://github.com/nationalarchives/docker/blob/main/docker/tna-python-django/README.md#environment-variables), this application has support for:

| Variable | Purpose | Default |
| ------------------------ | --------------------------------------------------------- | --------------------------------------------------------- |
| `DJANGO_SETTINGS_MODULE` | The configuration to use | `config.settings.production` |
| `ALLOWED_HOSTS` | A comma-separated list of allowed hosts | _none_ on production and staging, `*` on develop and test |
| `USE_X_FORWARDED_HOST` | Use the `X-Forwarded-Host` header in preference to `Host` | `False` |
| `DEBUG` | If true, allow debugging | `False` |
| `COOKIE_DOMAIN` | The domain to save cookie preferences against | _none_ |
| `DATABASE_NAME` | The name of the Postgres database | _none_ |
| `DATABASE_USER` | The username needed to access the Postgres database | _none_ |
| `DATABASE_PASSWORD` | The password needed to access the Postgres database | _none_ |
| `DATABASE_HOST` | The Postgres database host | _none_ |
| `DATABASE_PORT` | The Postgres database port | `5432` |
| `CSP_IMG_SRC` | A comma separated list of CSP rules for `img-src` | `'self'` |
| `CSP_SCRIPT_SRC` | A comma separated list of CSP rules for `script-src` | `'self'` |
| `CSP_SCRIPT_SRC_ELEM` | A comma separated list of CSP rules for `script-src-elem` | `'self'` |
| `CSP_STYLE_SRC` | A comma separated list of CSP rules for `style-src` | `'self'` |
| `CSP_STYLE_SRC_ELEM` | A comma separated list of CSP rules for `style-src-elem` | `'self'` |
| `CSP_FONT_SRC` | A comma separated list of CSP rules for `font-src` | `'self'` |
| `CSP_CONNECT_SRC` | A comma separated list of CSP rules for `connect-src` | `'self'` |
| `CSP_MEDIA_SRC` | A comma separated list of CSP rules for `media-src` | `'self'` |
| `CSP_WORKER_SRC` | A comma separated list of CSP rules for `worker-src` | `'self'` |
| `CSP_FRAME_SRC` | A comma separated list of CSP rules for `frame-src` | `'self'` |
| `GA4_ID` | The Google Analytics 4 ID | _none_ |
Empty file added app/__init__.py
Empty file.
19 changes: 19 additions & 0 deletions app/healthcheck/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.http import HttpResponse
from django.urls import path


def healthcheck(request):
return HttpResponse("ok")


app_name = "healthcheck"
urlpatterns = [
path(
"",
healthcheck,
),
path(
"live/",
healthcheck,
),
]
Loading

0 comments on commit 1516265

Please sign in to comment.