Skip to content

Commit 4e7b2b2

Browse files
committed
tidy up structure and add migration doc
1 parent 20e3efc commit 4e7b2b2

File tree

3 files changed

+87
-48
lines changed

3 files changed

+87
-48
lines changed

GO_EXAMPLE.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,9 @@ For `dis-design-system-go` to work correctly, we use `go-bindata` to generate a
2424

2525
Update the frontend service's `Makefile` with the following new commands so that `go-bindata` will generate this file:
2626

27-
:warning: The `APP_RENDERER_VERSION` is unlikely to have been set previously :warning:
28-
2927
```Makefile
3028
LOCAL_RENDERER_IN_USE = $(shell grep -c "\"github.com/ONSdigital/dis-design-system-go\" =" go.mod)
3129

32-
APP_RENDERER_VERSION=$(APP_RENDERER_VERSION)
33-
3430
.PHONY: fetch-renderer
3531
fetch-renderer:
3632
ifeq ($(LOCAL_RENDERER_IN_USE), 1)
@@ -53,9 +49,9 @@ generate-prod: fetch-renderer
5349
mv assets/data.go.new assets/data.go
5450
```
5551

56-
Due to having distributed assets that are combined with `go-bindata`, we require the `get-renderer-version` and `fetch-renderer-version` tasks to ensure the version of `dp-renderer` as specified in `go.mod` is used.
52+
Due to having distributed assets that are combined with `go-bindata`, we require the `fetch-renderer` task to ensure the version of `dis-design-system-go` is as specified in `go.mod` is used.
5753

58-
The existing `build` and `debug` tasks should then be updated to use the relevant `generate-` command as a prerequisite:
54+
The `build` and `debug` tasks should use the relevant `generate-` command as a prerequisite:
5955

6056
```Makefile
6157
.PHONY: build
@@ -67,8 +63,6 @@ debug: generate-debug
6763

6864
## Config
6965

70-
:warning: The `RendererVersion` is unlikely to have been set previously :warning:
71-
7266
`config.go` should be updated to include four properties: `PatternLibraryAssetsPath`, `SiteDomain`, `Debug` and `RendererVersion`.
7367

7468
You will also need to add additional logic to `config.go` to handle the path for pattern library assets when running `make debug` or the published assets in a `prod` build.
@@ -128,7 +122,7 @@ func get() (*Config, error) {
128122

129123
You will need the `RenderClient` interface in order to implement the methods that `dis-design-system-go` exposes.
130124

131-
```golang
125+
```go
132126
type RenderClient interface {
133127
BuildPage(w io.Writer, pageModel interface{}, templateName string)
134128
NewBasePageModel() model.Page
@@ -139,7 +133,7 @@ type RenderClient interface {
139133

140134
Example handler:
141135

142-
```golang
136+
```go
143137
func getCookiePreferencePage(w http.ResponseWriter, rendC RenderClient, cp cookies.Policy, isUpdated bool, lang string) {
144138
// create a new base page model that inject SiteDomain and PatternLibraryAssetsPath into the page struct
145139
basePage := rendC.NewBasePageModel()
@@ -149,13 +143,14 @@ func getCookiePreferencePage(w http.ResponseWriter, rendC RenderClient, cp cooki
149143

150144
// send the mapped data, with ResponseWriter and template name defined by the actual template file name (e.g. cookies-preferences.tmpl) to the render lib
151145
rendC.BuildPage(w, m, "cookies-preferences")
146+
}
152147
```
153148

154149
## Mappers
155150

156151
Example mapper:
157152

158-
```golang
153+
```go
159154
import (
160155
"dp-frontend-cookie-controller/model"
161156

@@ -172,11 +167,11 @@ func CreateCookieSettingPage(basePage core.Page, policy cookies.Policy, isUpdate
172167
}
173168
```
174169

175-
### Rendering error pages
170+
## Rendering error pages
176171

177-
We use a HTTP middleware handler to intercept error status in our controllers then call `BuildErrorPage` to render an error page. To set up the middleware we use [Alice](https://github.com/justinas/alice) when instantiating the router in our controllers. See [README](/README.md#instantiation) for setting up the render client that we pass to the middleware.
172+
We use a HTTP middleware handler to intercept error status in our controllers then call `BuildErrorPage` to render an error page. To set up the middleware we use [Alice](https://github.com/justinas/alice) when instantiating the router in our controllers.
178173

179-
```golang
174+
```go
180175
import "github.com/ONSdigital/dis-design-system-go/middleware/renderror"
181176

182177
middleware := []alice.Constructor{

MIGRATION.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Migration
2+
3+
This guide assumes you are using an app that currently consumes `dp-design-system` and `dp-renderer`.
4+
5+
From within your consuming frontend service:
6+
7+
1. Get the go module:
8+
9+
```shell
10+
go get github.com/ONSdigital/dis-design-system-go
11+
```
12+
13+
1. Find and replace statements that relate to `dp-renderer` and replace with `dis-design-system-go` in `.go` files and the `Makefile`
14+
15+
e.g.
16+
17+
```diff
18+
- "github.com/ONSdigital/dp-renderer/v2/model"
19+
+ "github.com/ONSdigital/dis-design-system-go/model"
20+
```
21+
22+
1. Add the `APP_RENDERER_VERSION` into the `build` and `debug` Makefile targets
23+
24+
For example the `debug` target might look similar to:
25+
26+
```Makefile
27+
.PHONY: debug
28+
debug: generate-debug
29+
go build -tags 'debug' -race -o $(BINPATH)/dp-frontend-homepage-controller -ldflags "-X main.BuildTime=$(BUILD_TIME) -X main.GitCommit=$(GIT_COMMIT) -X main.Version=$(VERSION)"
30+
HUMAN_LOG=1 DEBUG=1 APP_RENDERER_VERSION=$(APP_RENDERER_VERSION) $(BINPATH)/dp-frontend-homepage-controller
31+
```
32+
33+
1. Retrieve the environment variable `APP_RENDERER_VERSION` that you have set in the previous step and expose it in `config.go`.
34+
35+
Change the s3 directory to `dis-design-system-go`.
36+
37+
```go
38+
type Config struct {
39+
// Other config here
40+
RendererVersion string `envconfig:"APP_RENDERER_VERSION"`
41+
}
42+
43+
func get() (*Config, error) {
44+
if cfg != nil {
45+
return cfg, nil
46+
}
47+
48+
// other code
49+
50+
cfg = &Config{
51+
// Some other default config here
52+
RendererVersion: "v0.1.0", // OPTIONALLY - set to a stable base
53+
}
54+
55+
// other code
56+
57+
if cfg.Debug {
58+
cfg.PatternLibraryAssetsPath = "http://localhost:9002/dist/assets"
59+
} else {
60+
cfg.PatternLibraryAssetsPath = fmt.Sprintf("//cdn.ons.gov.uk/dis-design-system-go/%s", cfg.RendererVersion)
61+
}
62+
63+
// other code
64+
}
65+
```
66+
67+
1. Test your changes by running `make debug` from the consuming app. Follow the steps in the [README](README.md#generate-the-css-and-js) to generate the css/js locally.

README.md

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Beta rendering library for Dissemination frontend go microservices. `dis-design-
66

77
* Run `make help` to see full list of make targets
88

9+
## CSS/JS
10+
911
### Install dependencies
1012

1113
To build, run, deploy, test and audit the app you will need some additional tooling:
@@ -50,11 +52,11 @@ No further dependencies other than those defined in `go.mod`
5052

5153
* Once built, you can find assets stored on the web server, default location is [localhost:9002/dist/assets/](http://localhost:9002/dist/assets/)
5254

53-
### Go library
55+
## Go library
5456

5557
`dis-design-system-go` also acts as a Go library which contains template helpers, model structs and components that can be used by the consuming frontend app to serve HTML consistently across the ONS website. To make use of the `go` rendering, you will need to install it within your `go` frontend app.
5658

57-
#### Installation
59+
### Installation
5860

5961
Other than `dis-design-system-go` itself, you will need a utility that can combine service-specific and `dis-design-system-go` assets. We currently use `go-bindata` for this process. From the consuming frontend app, run the following commands to install:
6062

@@ -64,49 +66,24 @@ Other than `dis-design-system-go` itself, you will need a utility that can combi
6466
6567
* `go-bindata`: `go get github.com/kevinburke/go-bindata`
6668

67-
#### Instantiation
68-
69-
Assuming you have `go-bindata` set up to generate the relevant asset helper functions, you can instantiate the renderer with a default client (in this case, the default client is [`unrolled`](https://github.com/unrolled/render)).
70-
71-
```go
72-
rend := render.NewWithDefaultClient(asset.Asset, asset.AssetNames, cfg.PatternLibraryAssetsPath, cfg.SiteDomain)
73-
```
74-
75-
You can also instantiate a `Render` struct without a default client by using `New()`. This requires a rendering client that fulfills the `Renderer` interface to be passed in as well.
76-
77-
```go
78-
rend := render.New(rendereringClient, patternLibPath, siteDomain)
79-
```
80-
81-
#### Mapping data and building a page
82-
83-
When mapping data to a page model, you can use `NewBasePageModel` to instantiate a base page model with its `PatternLibraryAssetsPath` and `SiteDomain` properties auto-populated via the `Render` struct:
84-
85-
```go
86-
basePage := rendC.NewBasePageModel()
87-
mappedPageData := mapper.CreateExamplePage(basePage)
88-
```
69+
* No further dependencies other than those defined in `go.mod`
8970

90-
In order to generate HTML from a page model and template, use `BuildPage`, passing in the `ResponseWriter`, mapped data, and the name of the template:
91-
92-
```go
93-
rend.BuildPage(w, mappedPageData, "name-of-template-file-without-extension")
94-
```
71+
## Worked example
9572

96-
If an error occurs during page build, either because of an incorrect template name or incorrect data mapping, `dp-renderer` will write an error via an `errorResponse` struct.
73+
See the [go example](GO_EXAMPLE.md) guide for an example implementation.
9774

9875
## Using design patterns or components in your service
9976

10077
See [PATTERNS](PATTERNS.md) for details.
10178

102-
## Worked example
103-
104-
See the [go example](GO_EXAMPLE.md) guide for a worked example. This guide can also be used to support migrating from `dp-renderer` and `dp-design-system` to `dis-design-system-go`.
105-
10679
## Contributing
10780

10881
See [CONTRIBUTING](CONTRIBUTING.md) for details.
10982

83+
## Migrating
84+
85+
See [MIGRATION](MIGRATION.md) for details on how to migrate from `dp-design-system` and `dp-renderer` to the `dis-design-system-go`.
86+
11087
## License
11188

11289
Copyright © 2025, Office for National Statistics (<https://www.ons.gov.uk>)

0 commit comments

Comments
 (0)