Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sw-7163 update routing #335

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
environment:
PORT: 8888
SIRIUS_URL: http://json-server:3000
PREFIX: /supervision/deputies/firm
PREFIX: /supervision/deputies
depends_on:
- json-server

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ require golang.org/x/sync v0.8.0
require (
github.com/brunoscheufler/aws-ecs-metadata-go v0.0.0-20221221133751-67e37ae746cd // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
go.opentelemetry.io/contrib/detectors/aws/ecs v1.30.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0 // indirect
go.opentelemetry.io/contrib/propagators/aws v1.30.0 // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
Expand Down Expand Up @@ -52,6 +54,8 @@ go.opentelemetry.io/contrib/detectors/aws/ecs v1.28.0 h1:m5PcF0VJ5BcX9AQmPQl9nNm
go.opentelemetry.io/contrib/detectors/aws/ecs v1.28.0/go.mod h1:M3swUI/YXREpfrK9HoVP3CnXtxc7EHSDpKNI+Tjr7Mw=
go.opentelemetry.io/contrib/detectors/aws/ecs v1.30.0 h1:tyAMwh9XYbINOpW62iYo2k7ZlPgjEulbEFudzlY8H1I=
go.opentelemetry.io/contrib/detectors/aws/ecs v1.30.0/go.mod h1:NuMawOvkflSsTZAu3iF3ydeF4spFQUnf8DYjK8YdqYI=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0 h1:ZIg3ZT/aQ7AfKqdwp7ECpOK6vHqquXXuyTjIO8ZdmPs=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0/go.mod h1:DQAwmETtZV00skUwgD6+0U89g80NKsJE3DCKeLLPQMI=
go.opentelemetry.io/contrib/propagators/aws v1.27.0 h1:RJexJi4R0S9CpxzuhhzGlTCIpaaK9SJH9g9BFrCWfPE=
go.opentelemetry.io/contrib/propagators/aws v1.27.0/go.mod h1:bqU5Ma1dEQ7VtRbPMUsH8UDTuTMiLJN4W+eUmyNVayc=
go.opentelemetry.io/contrib/propagators/aws v1.28.0 h1:acyTl4oyin/iLr5Nz3u7p/PKHUbLh42w/fqg9LblExk=
Expand Down
3 changes: 1 addition & 2 deletions internal/server/app_vars.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package server

import (
"github.com/gorilla/mux"
"github.com/ministryofjustice/opg-sirius-supervision-firm-deputy-hub/internal/model"
"github.com/ministryofjustice/opg-sirius-supervision-firm-deputy-hub/internal/sirius"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -47,7 +46,7 @@ func NewAppVars(client AppVarsClient, r *http.Request, envVars EnvironmentVars)
return nil
})
group.Go(func() error {
firmId, _ := strconv.Atoi(mux.Vars(r)["id"])
firmId, _ := strconv.Atoi(r.PathValue("firmId"))
firm, err := client.GetFirmDetails(ctx.With(groupCtx), firmId)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/server/change_ecm.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func renderTemplateForChangeECM(client ChangeECMClient, tmpl Template) Handler {
return err
}

return Redirect(fmt.Sprintf("/%d?success=ecm", app.FirmId()))
return Redirect(fmt.Sprintf("/firm/%d?success=ecm", app.FirmId()))

default:
return StatusError(http.StatusMethodNotAllowed)
Expand Down
2 changes: 1 addition & 1 deletion internal/server/manage_firm_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func renderTemplateForManageFirmDetails(client ManageFirmDetailsInformation, tmp
return err
}

return Redirect(fmt.Sprintf("/%d?success=firmDetails", app.FirmId()))
return Redirect(fmt.Sprintf("/firm/%d?success=firmDetails", app.FirmId()))

default:
return StatusError(http.StatusMethodNotAllowed)
Expand Down
6 changes: 3 additions & 3 deletions internal/server/manage_firm_details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func TestPostManageFirm(t *testing.T) {
client := &mockManageFirmDetailsClient{}

w := httptest.NewRecorder()
r, _ := http.NewRequest("POST", "/123", strings.NewReader(""))
r, _ := http.NewRequest("POST", "/firm/123", strings.NewReader(""))
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")

returnedError := renderTemplateForManageFirmDetails(client, nil)(AppVars{FirmDetails: mockFirmDetails}, w, r)

assert.Equal(t, Redirect("/123?success=firmDetails"), returnedError)
assert.Equal(t, Redirect("/firm/123?success=firmDetails"), returnedError)
}

func TestPostManageFirmReturnsError(t *testing.T) {
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestPostManageFirmReturnsError(t *testing.T) {
template := &mockTemplates{}

w := httptest.NewRecorder()
r, _ := http.NewRequest("POST", "/123", strings.NewReader(""))
r, _ := http.NewRequest("POST", "/firm/123", strings.NewReader(""))
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")

err := renderTemplateForManageFirmDetails(client, template)(AppVars{}, w, r)
Expand Down
2 changes: 1 addition & 1 deletion internal/server/manage_pii_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func renderTemplateForManagePiiDetails(client ManagePiiDetailsInformation, tmpl
return err
}

return Redirect(fmt.Sprintf("/%d?success=piiDetails", app.FirmId()))
return Redirect(fmt.Sprintf("/firm/%d?success=piiDetails", app.FirmId()))

default:
return StatusError(http.StatusMethodNotAllowed)
Expand Down
6 changes: 3 additions & 3 deletions internal/server/manage_pii_details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func TestPostManagePii(t *testing.T) {
client := &mockManagePiiDetailsClient{}

w := httptest.NewRecorder()
r, _ := http.NewRequest("POST", "/123", strings.NewReader(""))
r, _ := http.NewRequest("POST", "/firm/123", strings.NewReader(""))
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")

returnedError := renderTemplateForManagePiiDetails(client, nil)(AppVars{FirmDetails: mockFirmDetails}, w, r)

assert.Equal(t, Redirect("/123?success=piiDetails"), returnedError)
assert.Equal(t, Redirect("/firm/123?success=piiDetails"), returnedError)
}

func TestPostManagePiiReturnsError(t *testing.T) {
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestPostManagePiiReturnsError(t *testing.T) {
template := &mockTemplates{}

w := httptest.NewRecorder()
r, _ := http.NewRequest("POST", "/123", strings.NewReader(""))
r, _ := http.NewRequest("POST", "/firm/123", strings.NewReader(""))
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")

err := renderTemplateForManagePiiDetails(client, template)(AppVars{}, w, r)
Expand Down
2 changes: 1 addition & 1 deletion internal/server/request_pii_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func renderTemplateForRequestPiiDetails(client RequestPiiDetailsInformation, tmp
return err
}

return Redirect(fmt.Sprintf("/%d?success=requestPiiDetails", app.FirmId()))
return Redirect(fmt.Sprintf("/firm/%d?success=requestPiiDetails", app.FirmId()))

default:
return StatusError(http.StatusMethodNotAllowed)
Expand Down
6 changes: 3 additions & 3 deletions internal/server/request_pii_details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func TestPostRequestPii(t *testing.T) {
client := &mockRequestPiiDetailsClient{}

w := httptest.NewRecorder()
r, _ := http.NewRequest("POST", "/123", strings.NewReader(""))
r, _ := http.NewRequest("POST", "/firm/123", strings.NewReader(""))
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")

returnedError := renderTemplateForRequestPiiDetails(client, nil)(AppVars{FirmDetails: mockFirmDetails}, w, r)

assert.Equal(t, Redirect("/123?success=requestPiiDetails"), returnedError)
assert.Equal(t, Redirect("/firm/123?success=requestPiiDetails"), returnedError)
}

func TestPostRequestPiiReturnsError(t *testing.T) {
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestPostRequestPiiReturnsError(t *testing.T) {
template := &mockTemplates{}

w := httptest.NewRecorder()
r, _ := http.NewRequest("POST", "/123", strings.NewReader(""))
r, _ := http.NewRequest("POST", "/firm/123", strings.NewReader(""))
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")

err := renderTemplateForRequestPiiDetails(client, template)(AppVars{}, w, r)
Expand Down
59 changes: 20 additions & 39 deletions internal/server/server.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package server

import (
"github.com/gorilla/mux"
"github.com/ministryofjustice/opg-go-common/telemetry"
"html/template"
"io"
"log/slog"
Expand All @@ -11,6 +9,7 @@ import (

"github.com/ministryofjustice/opg-go-common/securityheaders"
"github.com/ministryofjustice/opg-sirius-supervision-firm-deputy-hub/internal/sirius"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

type Client interface {
Expand All @@ -30,46 +29,28 @@ type Template interface {
func New(logger *slog.Logger, client Client, templates map[string]*template.Template, envVars EnvironmentVars) http.Handler {
wrap := wrapHandler(logger, client, templates["error.gotmpl"], envVars)

router := mux.NewRouter().StrictSlash(true)
router.Handle("/health-check", healthCheck())

pageRouter := router.PathPrefix("/{id}").Subrouter()
pageRouter.Use(telemetry.Middleware(logger))

pageRouter.Handle("",
wrap(
renderTemplateForFirmHub(client, templates["firm-hub.gotmpl"])))

pageRouter.Handle("/manage-pii-details",
wrap(
renderTemplateForManagePiiDetails(client, templates["manage-pii-details.gotmpl"])))

pageRouter.Handle("/manage-firm-details",
wrap(
renderTemplateForManageFirmDetails(client, templates["manage-firm-details.gotmpl"])))

pageRouter.Handle("/deputies",
wrap(
renderTemplateForDeputyTab(client, templates["deputies.gotmpl"])))

router.Handle("/health-check", healthCheck())

pageRouter.Handle("/request-pii-details",
wrap(
renderTemplateForRequestPiiDetails(client, templates["request-pii-details.gotmpl"])))

pageRouter.Handle("/change-ecm",
wrap(
renderTemplateForChangeECM(client, templates["change-ecm.gotmpl"])))
mux := http.NewServeMux()

mux.Handle("GET /firm/{firmId}", wrap(renderTemplateForFirmHub(client, templates["firm-hub.gotmpl"])))
mux.Handle("POST /firm/{firmId}", wrap(renderTemplateForFirmHub(client, templates["firm-hub.gotmpl"])))
mux.Handle("GET /firm/{firmId}/manage-pii-details", wrap(renderTemplateForManagePiiDetails(client, templates["manage-pii-details.gotmpl"])))
mux.Handle("POST /firm/{firmId}/manage-pii-details", wrap(renderTemplateForManagePiiDetails(client, templates["manage-pii-details.gotmpl"])))
mux.Handle("GET /firm/{firmId}/manage-firm-details", wrap(renderTemplateForManageFirmDetails(client, templates["manage-firm-details.gotmpl"])))
mux.Handle("POST /firm/{firmId}/manage-firm-details", wrap(renderTemplateForManageFirmDetails(client, templates["manage-firm-details.gotmpl"])))
mux.Handle("GET /firm/{firmId}/deputies", wrap(renderTemplateForDeputyTab(client, templates["deputies.gotmpl"])))
mux.Handle("GET /firm/{firmId}/health-check", healthCheck())
mux.Handle("GET /firm/{firmId}/request-pii-details", wrap(renderTemplateForRequestPiiDetails(client, templates["request-pii-details.gotmpl"])))
mux.Handle("POST /firm/{firmId}/request-pii-details", wrap(renderTemplateForRequestPiiDetails(client, templates["request-pii-details.gotmpl"])))
mux.Handle("GET /firm/{firmId}/change-ecm", wrap(renderTemplateForChangeECM(client, templates["change-ecm.gotmpl"])))
mux.Handle("POST /firm/{firmId}/change-ecm", wrap(renderTemplateForChangeECM(client, templates["change-ecm.gotmpl"])))

static := staticFileHandler(envVars.WebDir)
router.PathPrefix("/assets/").Handler(static)
router.PathPrefix("/javascript/").Handler(static)
router.PathPrefix("/stylesheets/").Handler(static)

router.NotFoundHandler = wrap(notFoundHandler(templates["error.gotmpl"], envVars))
mux.Handle("/assets/", static)
mux.Handle("/javascript/", static)
mux.Handle("/stylesheets/", static)
mux.Handle("/", wrap(notFoundHandler(templates["error.gotmpl"], envVars)))

return http.StripPrefix(envVars.Prefix, securityheaders.Use(router))
return otelhttp.NewHandler(http.StripPrefix(envVars.Prefix, securityheaders.Use(mux)), "supervision-firm-hub")
}

func notFoundHandler(tmplError Template, envVars EnvironmentVars) Handler {
Expand Down
2 changes: 1 addition & 1 deletion web/template/change-ecm.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<a
id="change-ecm-cancel"
class="govuk-link data-emc-cancel"
href="{{ prefix (printf "/%d" .FirmDetails.ID) }}">
href="{{ prefix (printf "/firm/%d" .FirmDetails.ID) }}">
Cancel
</a>
</div>
Expand Down
8 changes: 4 additions & 4 deletions web/template/firm-hub.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<div class="moj-button-menu">
<div class="moj-button-menu__wrapper">
<div class="moj-button-menu__wrapper">
<a href="{{ prefix (printf "/%d/change-ecm" .FirmDetails.ID) }}" role="button" draggable="false" class="govuk-button moj-button-menu__item govuk-button--secondary " data-module="govuk-button">
<a href="{{ prefix (printf "/firm/%d/change-ecm" .FirmDetails.ID) }}" role="button" draggable="false" class="govuk-button moj-button-menu__item govuk-button--secondary " data-module="govuk-button">
Change ECM
</a>
</div>
Expand Down Expand Up @@ -102,7 +102,7 @@
</dd>
</div>
</dl>
<a href="{{ prefix (printf "/%d/manage-firm-details" .FirmDetails.ID) }}" role="button" draggable="false" class="govuk-button govuk-button--secondary" data-module="govuk-button">
<a href="{{ prefix (printf "/firm/%d/manage-firm-details" .FirmDetails.ID) }}" role="button" draggable="false" class="govuk-button govuk-button--secondary" data-module="govuk-button">
Manage firm details
</a>
</div>
Expand Down Expand Up @@ -135,7 +135,7 @@
</div>
</dl>
<a
href="{{ prefix (printf "/%d/manage-pii-details" .FirmDetails.ID) }}"
href="{{ prefix (printf "/firm/%d/manage-pii-details" .FirmDetails.ID) }}"
role="button"
draggable="false"
id="manage-pii-btn"
Expand All @@ -145,7 +145,7 @@
Manage PII information
</a>
<a
href="{{ prefix (printf "/%d/request-pii-details" .FirmDetails.ID) }}"
href="{{ prefix (printf "/firm/%d/request-pii-details" .FirmDetails.ID) }}"
role="button"
id="request-pii-btn"
draggable="false"
Expand Down
8 changes: 4 additions & 4 deletions web/template/layout/navigation.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
<li class="moj-sub-navigation__item">
<a
class="moj-sub-navigation__link"
{{ if eq .Path (printf "/%d" .FirmDetails.ID) }} aria-current="page"{{ end }}
href="{{ prefix (printf "/%d" .FirmDetails.ID) }}">
{{ if eq .Path (printf "/firm/%d" .FirmDetails.ID) }} aria-current="page"{{ end }}
href="{{ prefix (printf "/firm/%d" .FirmDetails.ID) }}">
Firm details
</a>
</li>
<li class="moj-sub-navigation__item">
<a
class="moj-sub-navigation__link"
{{ if eq .Path (printf "/%d/deputies" .FirmDetails.ID) }}aria-current="page"{{ end }}
href="{{ prefix (printf "/%d/deputies" .FirmDetails.ID) }}">
{{ if eq .Path (printf "/firm/%d/deputies" .FirmDetails.ID) }}aria-current="page"{{ end }}
href="{{ prefix (printf "/firm/%d/deputies" .FirmDetails.ID) }}">
Deputies
</a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion web/template/manage-firm-details.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
Save Firm details
</button>

<a id="cancel-manage-firms" class="govuk-link" href="{{ prefix (printf "/%d" .FirmDetails.ID) }}">Cancel</a>
<a id="cancel-manage-firms" class="govuk-link" href="{{ prefix (printf "/firm/%d" .FirmDetails.ID) }}">Cancel</a>

</div>

Expand Down
2 changes: 1 addition & 1 deletion web/template/manage-pii-details.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<a
id="manage-pii-cancel"
class="govuk-link"
href="{{ prefix (printf "/%d" .FirmDetails.ID) }}"
href="{{ prefix (printf "/firm/%d" .FirmDetails.ID) }}"
>Cancel</a
>
</div>
Expand Down
2 changes: 1 addition & 1 deletion web/template/request-pii-details.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<a
id="cancel-request-pii"
class="govuk-link"
href="{{ prefix (printf "/%d" .FirmDetails.ID) }}"
href="{{ prefix (printf "/firm/%d" .FirmDetails.ID) }}"
>Cancel</a
>
</div>
Expand Down