Start setting up structure for generating URLs
This is to eventually avoid adding URLs through hard-coded strings to our templates.
This commit is contained in:
parent
9b1d75d47f
commit
bb9dd1754f
2 changed files with 37 additions and 22 deletions
11
rmo/mock.go
11
rmo/mock.go
|
|
@ -22,13 +22,6 @@ type ContentDistrict struct {
|
|||
URLLogo string
|
||||
URLWebsite string
|
||||
}
|
||||
type ContentURL struct {
|
||||
Nuisance string
|
||||
NuisanceSubmitComplete string
|
||||
Status string
|
||||
Tegola string
|
||||
Water string
|
||||
}
|
||||
type ContentMock struct {
|
||||
District ContentDistrict
|
||||
MapboxToken string
|
||||
|
|
@ -48,7 +41,7 @@ func addMockRoutes(r chi.Router) {
|
|||
r.Get("/status", renderMock(mockStatusT))
|
||||
}
|
||||
|
||||
func makeContentURL(slug string) ContentURL {
|
||||
func makeContentURLMock(slug string) ContentURL {
|
||||
return ContentURL{
|
||||
Nuisance: makeURLMock(slug, "nuisance"),
|
||||
NuisanceSubmitComplete: makeURLMock(slug, "nuisance-submit-complete"),
|
||||
|
|
@ -77,7 +70,7 @@ func renderMock(t *html.BuiltTemplate) func(http.ResponseWriter, *http.Request)
|
|||
},
|
||||
MapboxToken: config.MapboxToken,
|
||||
ReportID: "abcd-1234-5678",
|
||||
URL: makeContentURL(slug),
|
||||
URL: makeContentURLMock(slug),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
|
|||
48
rmo/root.go
48
rmo/root.go
|
|
@ -15,7 +15,16 @@ type ContentPrivacy struct {
|
|||
Site string
|
||||
URLReport string
|
||||
}
|
||||
type ContentRoot struct{}
|
||||
type ContentRoot struct {
|
||||
URL ContentURL
|
||||
}
|
||||
type ContentURL struct {
|
||||
Nuisance string
|
||||
NuisanceSubmitComplete string
|
||||
Status string
|
||||
Tegola string
|
||||
Water string
|
||||
}
|
||||
|
||||
var (
|
||||
PrivacyT = buildTemplate("privacy", "base")
|
||||
|
|
@ -23,6 +32,14 @@ var (
|
|||
TermsT = buildTemplate("terms", "base")
|
||||
)
|
||||
|
||||
func boolFromForm(r *http.Request, k string) bool {
|
||||
s := r.PostFormValue(k)
|
||||
if s == "on" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func getPrivacy(w http.ResponseWriter, r *http.Request) {
|
||||
html.RenderOrError(
|
||||
w,
|
||||
|
|
@ -48,27 +65,26 @@ func getRobots(w http.ResponseWriter, r *http.Request) {
|
|||
fmt.Fprint(w, "Allow: /\n")
|
||||
}
|
||||
func getTerms(w http.ResponseWriter, r *http.Request) {
|
||||
htmlpage.RenderOrError(
|
||||
html.RenderOrError(
|
||||
w,
|
||||
TermsT,
|
||||
ContentRoot{},
|
||||
)
|
||||
}
|
||||
|
||||
// Respond with an error that is visible to the user
|
||||
func respondError(w http.ResponseWriter, m string, e error, s int) {
|
||||
log.Warn().Int("status", s).Err(e).Str("user message", m).Msg("Responding with an error")
|
||||
http.Error(w, m, s)
|
||||
}
|
||||
|
||||
func boolFromForm(r *http.Request, k string) bool {
|
||||
s := r.PostFormValue(k)
|
||||
if s == "on" {
|
||||
return true
|
||||
func makeContentURL(slug string) ContentURL {
|
||||
return ContentURL{
|
||||
Nuisance: makeURL("nuisance"),
|
||||
NuisanceSubmitComplete: makeURL("nuisance-submit-complete"),
|
||||
Status: makeURL("status"),
|
||||
Tegola: config.MakeURLTegola("/"),
|
||||
Water: makeURL("water"),
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func makeURL(p string) string {
|
||||
return config.MakeURLReport("/%s", p)
|
||||
}
|
||||
func postFormValueOrNone(r *http.Request, k string) string {
|
||||
v := r.PostFormValue(k)
|
||||
if v == "" {
|
||||
|
|
@ -76,3 +92,9 @@ func postFormValueOrNone(r *http.Request, k string) string {
|
|||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Respond with an error that is visible to the user
|
||||
func respondError(w http.ResponseWriter, m string, e error, s int) {
|
||||
log.Warn().Int("status", s).Err(e).Str("user message", m).Msg("Responding with an error")
|
||||
http.Error(w, m, s)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue