Skip to content

Commit

Permalink
Modularise folders (#31)
Browse files Browse the repository at this point in the history
* refactor to modularise layout
* update readme
* add section on updating deps
  • Loading branch information
tamsinforbes authored Dec 1, 2023
1 parent 89ecda5 commit 7dbda54
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 17 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ This repository contains an example application demonstrating Operations Enginee

1. [Prerequisites](#prerequisites)
1. [How to use](#how-to-use)
1. [Updating dependencies](#updating-dependencies)
1. [Generic settings](#generic-settings)
1. [Deployment](#deployment)
1. [Contributing](#contributing)
1. [Contact](#contact)
Expand Down Expand Up @@ -48,6 +50,10 @@ We rely on naming conventions to facilitate use of this template. All Operations
- Create Cloud Platform namespaces called `<TEAM_NAME>-<NAME>-<ENV>` for example, `operations-engineering-example-dev` and `operations-engineering-example-prod` and link to the repo. Follow instructions here: [Creating a Cloud Platform environment](https://user-guide.cloud-platform.service.justice.gov.uk/documentation/getting-started/env-create.html#creating-a-cloud-platform-environment).
- The app is available at `<TEAM_NAME>-<NAME>-<ENV>.cloud-platform.service.justice.gov.uk`

### Updating dependencies

This app is set up with dependabot to automatically raise PRs to update dependencies. Note that the `govuk-frontend` package version is hardcoded in `build.py` and `application/templates/components/base.html`, which must be updated manually if another version is required. The version of `govuk-frontend` is determined by the version of `govuk-frontend-jijna` set in the `requirements.txt` file. The current verison of `govuk-frontend` is recorded in `application/static/VERSION.txt`. For example, `govuk-frontend-jinja` 2.7.0 requires `govuk-frontend` 4.7.0.

### Generic settings

The flask app itself is deliberately generically named `application` and does not need to be changed. If you choose to change it then corresponding changes are required in `build.py`, `Dockerfile`, `makefile` and possibly elsewhere. The app is hardcoded to run and listen on port `1551` as appuser `1051` (see `Dockerfile` and `compose.yaml`).
Expand Down Expand Up @@ -81,9 +87,9 @@ The production namespace on [Cloud Platform](https://user-guide.cloud-platform.s

To deploy the app to the production namespace do the following:

- on the `main` branch.
- create a new tag using `git tag vx.y.z` where `x.y.z` is the version number. Please follow [semantic versioning](https://semver.org/).
- push the tag to the remote repository using `git push origin --tags`
- Switch to the `main` branch and ensure it is up to date.
- Create a new tag using `git tag vx.y.z` where `x.y.z` is the new version number, and the `v` prefix provides the match criterion to trigger deployment. Please follow [semantic versioning](https://semver.org/).
- Push the tag to the remote repository using `git push origin --tags`

This triggers the `deploy-to-prod` GitHub workflow to create a release and deploy the app to the production namespace.

Expand Down
18 changes: 17 additions & 1 deletion application/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@

from config import Config

# from application.main.views import (main)

from application.main.middleware.error_handler import (
server_forbidden,
page_not_found,
too_many_requests,
unknown_server_error,
gateway_timeout,
)


app = Flask(__name__)
app.config.from_object(Config)

# app.register_blueprint(main)

app.jinja_loader = ChoiceLoader(
[
PackageLoader("application"),
Expand All @@ -22,7 +34,11 @@
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True

app.register_error_handler(403, server_forbidden)
app.register_error_handler(404, page_not_found)
app.register_error_handler(500, unknown_server_error)
app.register_error_handler(504, gateway_timeout)

@app.route("/", methods=["GET", "POST"])
def index():
return render_template("index.html")
return render_template("pages/index.html")
Empty file added application/main/__init__.py
Empty file.
Empty file.
27 changes: 27 additions & 0 deletions application/main/middleware/error_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import logging

from flask import (
render_template,
)

logger = logging.getLogger(__name__)

def server_forbidden(err):
logger.error("server_forbidden(): %s", err)
return render_template("pages/errors/403.html"), 403

def page_not_found(err):
logger.error("A request was made to a page that doesn't exist %s", err)
return render_template("pages/errors/404.html"), 404

def too_many_requests(err):
logger.error("Too many attempts to access this page %s", err)
return render_template("pages/errors/429.html"), 429

def unknown_server_error(err):
logger.error("An unknown server error occurred: %s", err)
return render_template("pages/errors/500.html"), 500

def gateway_timeout(err):
logger.error("A gateway timeout error occurred: %s", err)
return render_template("pages/errors/504.html"), 504
12 changes: 12 additions & 0 deletions application/main/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import datetime
import logging
import os
from collections import Counter
from functools import wraps
from urllib.parse import quote_plus, urlencode

from flask import (Blueprint, abort, current_app, jsonify, redirect,
render_template, render_template_string, request, session,
url_for)

# Empty as yet
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "base.html" %}
{% extends "components/base.html" %}

{%- from 'govuk_frontend_jinja/components/back-link/macro.html' import govukBackLink -%}
{%- from 'govuk_frontend_jinja/components/inset-text/macro.html' import govukInsetText -%}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "base.html" %}
{% extends "components/base.html" %}

{%- from 'govuk_frontend_jinja/components/back-link/macro.html' import govukBackLink -%}
{%- from 'govuk_frontend_jinja/components/table/macro.html' import govukTable -%}
Expand Down
17 changes: 17 additions & 0 deletions application/templates/pages/errors/403.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "components/base.html" %}

{% block pageTitle %}
Web server forbids you from accessing the page you’re trying to open (403 error)
{% endblock %}

{% set mainClasses = "govuk-main-wrapper--l" %}

{% block content %}
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl">Web server forbids you from accessing the page you’re trying to open (403
error)</h1>
<p class="govuk-body">If you entered a web address, please check it was correct.</p>
</div>
</div>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{% extends "base.html" %}
{% extends "components/base.html" %}

{% block pageTitle %}Page not found – {{config['SERVICE_NAME']}} – GOV.UK{% endblock %}
{% block pageTitle %}
Page not found – {{config['SERVICE_NAME']}} – GOV.UK (404 error)
{% endblock %}

{% set mainClasses = "govuk-main-wrapper--l" %}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{% extends "base.html" %}
{% extends "components/base.html" %}

{% block pageTitle %}Too many requests – {{config['SERVICE_NAME']}} – GOV.UK{% endblock %}
{% block pageTitle %}
Too many requests – {{config['SERVICE_NAME']}} – GOV.UK (429 error)
{% endblock %}

{% set mainClasses = "govuk-main-wrapper--l" %}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{% extends "base.html" %}
{% extends "components/base.html" %}

{% block pageTitle %}Sorry, there is a problem with the service – {{config['SERVICE_NAME']}} – GOV.UK{% endblock %}
{% block pageTitle %}
Sorry, there is a problem with the service – {{config['SERVICE_NAME']}} – GOV.UK (500 error)
{% endblock %}

{% set mainClasses = "govuk-main-wrapper--l" %}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{% extends "base.html" %}
{% extends "components/base.html" %}

{% block pageTitle %}Sorry, the service is unavailable – {{config['SERVICE_NAME']}} – GOV.UK{% endblock %}
{% block pageTitle %}
Sorry, the service is unavailable – {{config['SERVICE_NAME']}} – GOV.UK (503 error)
{% endblock %}

{% set mainClasses = "govuk-main-wrapper--l" %}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "base.html" %}
{% extends "components/base.html" %}

{% block beforeContent %}
{{ super() }}
Expand All @@ -9,7 +9,7 @@
<html>
<body>

<h1>Good afternoon!</h1>
<h1>Good morning!</h1>

<p>I hope you're having a lovely day.</p>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "base.html" %}
{% extends "components/base.html" %}

{%- from 'govuk_frontend_jinja/components/back-link/macro.html' import govukBackLink -%}

Expand Down

0 comments on commit 7dbda54

Please sign in to comment.