Skip to content

Commit

Permalink
fix: enable livereload on error page too
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 committed May 23, 2023
1 parent f800fb9 commit e5a2f01
Show file tree
Hide file tree
Showing 19 changed files with 222 additions and 202 deletions.
38 changes: 19 additions & 19 deletions internal/auth/team_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (h *webHandlers) addTeamHandlers(r *mux.Router) {
func (h *webHandlers) newTeam(w http.ResponseWriter, r *http.Request) {
org, err := decode.Param("organization_name", r)
if err != nil {
html.Error(w, err.Error(), http.StatusUnprocessableEntity)
h.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}

Expand All @@ -40,7 +40,7 @@ func (h *webHandlers) newTeam(w http.ResponseWriter, r *http.Request) {
func (h *webHandlers) createTeam(w http.ResponseWriter, r *http.Request) {
var opts CreateTeamOptions
if err := decode.All(&opts, r); err != nil {
html.Error(w, err.Error(), http.StatusUnprocessableEntity)
h.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}

Expand All @@ -51,7 +51,7 @@ func (h *webHandlers) createTeam(w http.ResponseWriter, r *http.Request) {
return
}
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err.Error(), http.StatusInternalServerError)
return
}

Expand All @@ -62,18 +62,18 @@ func (h *webHandlers) createTeam(w http.ResponseWriter, r *http.Request) {
func (h *webHandlers) getTeam(w http.ResponseWriter, r *http.Request) {
teamID, err := decode.Param("team_id", r)
if err != nil {
html.Error(w, err.Error(), http.StatusUnprocessableEntity)
h.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}

team, err := h.svc.GetTeamByID(r.Context(), teamID)
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err.Error(), http.StatusInternalServerError)
return
}
members, err := h.svc.ListTeamMembers(r.Context(), teamID)
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err.Error(), http.StatusInternalServerError)
return
}

Expand All @@ -82,14 +82,14 @@ func (h *webHandlers) getTeam(w http.ResponseWriter, r *http.Request) {
// retrieve the list.
subject, err := internal.SubjectFromContext(r.Context())
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var users []*User
if subject.CanAccessSite(rbac.ListUsersAction) {
users, err = h.svc.ListUsers(r.Context())
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
Expand Down Expand Up @@ -119,13 +119,13 @@ func (h *webHandlers) updateTeam(w http.ResponseWriter, r *http.Request) {
UpdateTeamOptions
}
if err := decode.All(&params, r); err != nil {
html.Error(w, err.Error(), http.StatusUnprocessableEntity)
h.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}

team, err := h.svc.UpdateTeam(r.Context(), params.TeamID, params.UpdateTeamOptions)
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err.Error(), http.StatusInternalServerError)
return
}

Expand All @@ -136,13 +136,13 @@ func (h *webHandlers) updateTeam(w http.ResponseWriter, r *http.Request) {
func (h *webHandlers) listTeams(w http.ResponseWriter, r *http.Request) {
org, err := decode.Param("organization_name", r)
if err != nil {
html.Error(w, err.Error(), http.StatusUnprocessableEntity)
h.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}

teams, err := h.svc.ListTeams(r.Context(), org)
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err.Error(), http.StatusInternalServerError)
return
}

Expand All @@ -162,18 +162,18 @@ func (h *webHandlers) listTeams(w http.ResponseWriter, r *http.Request) {
func (h *webHandlers) deleteTeam(w http.ResponseWriter, r *http.Request) {
teamID, err := decode.Param("team_id", r)
if err != nil {
html.Error(w, err.Error(), http.StatusUnprocessableEntity)
h.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}

team, err := h.svc.GetTeamByID(r.Context(), teamID)
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = h.svc.DeleteTeam(r.Context(), teamID)
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err.Error(), http.StatusInternalServerError)
return
}

Expand All @@ -184,12 +184,12 @@ func (h *webHandlers) deleteTeam(w http.ResponseWriter, r *http.Request) {
func (h *webHandlers) addTeamMember(w http.ResponseWriter, r *http.Request) {
var params TeamMembershipOptions
if err := decode.All(&params, r); err != nil {
html.Error(w, err.Error(), http.StatusUnprocessableEntity)
h.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}

if err := h.svc.AddTeamMembership(r.Context(), params); err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err.Error(), http.StatusInternalServerError)
return
}

Expand All @@ -200,12 +200,12 @@ func (h *webHandlers) addTeamMember(w http.ResponseWriter, r *http.Request) {
func (h *webHandlers) removeTeamMember(w http.ResponseWriter, r *http.Request) {
var params TeamMembershipOptions
if err := decode.All(&params, r); err != nil {
html.Error(w, err.Error(), http.StatusUnprocessableEntity)
h.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}

if err := h.svc.RemoveTeamMembership(r.Context(), params); err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err.Error(), http.StatusInternalServerError)
return
}

Expand Down
6 changes: 3 additions & 3 deletions internal/auth/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func (h *webHandlers) addHandlers(r *mux.Router) {
func (h *webHandlers) listOrganizationUsers(w http.ResponseWriter, r *http.Request) {
name, err := decode.Param("name", r)
if err != nil {
html.Error(w, err.Error(), http.StatusUnprocessableEntity)
h.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}

users, err := h.svc.ListOrganizationUsers(r.Context(), name)
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err.Error(), http.StatusInternalServerError)
return
}

Expand All @@ -55,7 +55,7 @@ func (h *webHandlers) listOrganizationUsers(w http.ResponseWriter, r *http.Reque
func (h *webHandlers) profileHandler(w http.ResponseWriter, r *http.Request) {
user, err := internal.SubjectFromContext(r.Context())
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err.Error(), http.StatusInternalServerError)
return
}
h.Render("profile.tmpl", w, struct {
Expand Down
6 changes: 3 additions & 3 deletions internal/authenticator/oauth_authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (a *oauthAuthenticator) ResponseHandler(w http.ResponseWriter, r *http.Requ

client, err := a.NewClient(r.Context(), token)
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
html.Error(w, err.Error(), http.StatusInternalServerError, false)
return
}

Expand All @@ -44,15 +44,15 @@ func (a *oauthAuthenticator) ResponseHandler(w http.ResponseWriter, r *http.Requ
// Get cloud user
cuser, err := client.GetUser(ctx)
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
html.Error(w, err.Error(), http.StatusInternalServerError, false)
return
}

err = a.StartSession(w, r, tokens.StartSessionOptions{
Username: &cuser.Name,
})
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
html.Error(w, err.Error(), http.StatusInternalServerError, false)
return
}
}
10 changes: 5 additions & 5 deletions internal/authenticator/oidc_authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,36 +88,36 @@ func (o oidcAuthenticator) ResponseHandler(w http.ResponseWriter, r *http.Reques
// Extract the ID Token from OAuth2 token.
rawIDToken, ok := token.Extra("id_token").(string)
if !ok {
html.Error(w, "id_token missing", http.StatusInternalServerError)
html.Error(w, "id_token missing", http.StatusInternalServerError, false)
return
}

// Parse and verify ID Token payload.
idt, err := o.verifier.Verify(r.Context(), rawIDToken)
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
html.Error(w, err.Error(), http.StatusInternalServerError, false)
return
}

// Extract custom claims
var claims oidcClaims
if err := idt.Claims(&claims); err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
html.Error(w, err.Error(), http.StatusInternalServerError, false)
return
}

// Get claims user
user, err := o.getUserFromClaims(claims)
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
html.Error(w, err.Error(), http.StatusInternalServerError, false)
return
}

err = o.StartSession(w, r, tokens.StartSessionOptions{
Username: &user.Name,
})
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
html.Error(w, err.Error(), http.StatusInternalServerError, false)
return
}
}
Expand Down
13 changes: 10 additions & 3 deletions internal/http/html/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const errorTemplateContent = `
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>error | otf</title>
{{ if .DevMode }}<script src="http://localhost:35729/livereload.js"></script>{{ end }}
<style>
pre {
margin: auto;
Expand All @@ -25,15 +26,21 @@ const errorTemplateContent = `
</style>
</head>
<body>
<pre>{{ . }}</pre>
<pre>{{ .Error }}</pre>
</body>
</html>
`

var errorTemplate = template.Must(template.New("error").Parse(errorTemplateContent))

func Error(w http.ResponseWriter, err string, code int) {
func Error(w http.ResponseWriter, err string, code int, devMode bool) {
w.WriteHeader(code)

errorTemplate.Execute(w, err)
errorTemplate.Execute(w, struct {
Error string
DevMode bool
}{
Error: err,
DevMode: devMode,
})
}
15 changes: 9 additions & 6 deletions internal/http/html/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@ type (
templateRenderer
}

renderer struct {
templateRenderer
}

// pageRenderer renders an html page using the named template.
pageRenderer interface {
Render(name string, w http.ResponseWriter, page any)
}
// renderer locates and renders a template.

// templateRenderer locates and renders a template (a lower-level
// alternative to pageRenderer for rendering partial content).
templateRenderer interface {
RenderTemplate(name string, w io.Writer, data any) error
Error(w http.ResponseWriter, err string, code int)
}

renderer struct {
templateRenderer
}
)

Expand All @@ -66,7 +69,7 @@ func (r *renderer) Render(name string, w http.ResponseWriter, page any) {
// purge flash messages from cookie store prior to rendering template
purgeFlashes(w)
if err := r.RenderTemplate(name, w, page); err != nil {
Error(w, err.Error(), http.StatusInternalServerError)
r.Error(w, err.Error(), http.StatusInternalServerError)
}
}

Expand Down
5 changes: 5 additions & 0 deletions internal/http/html/renderer_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package html

import (
"io"
"net/http"
)

// devRenderer reads templates from disk. Intended for development purposes.
Expand All @@ -17,3 +18,7 @@ func (r *devRenderer) RenderTemplate(name string, w io.Writer, data any) error {

return renderTemplateFromCache(cache, name, w, data)
}

func (r *devRenderer) Error(w http.ResponseWriter, err string, code int) {
Error(w, err, code, true)
}
5 changes: 5 additions & 0 deletions internal/http/html/renderer_embedded.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package html
import (
"html/template"
"io"
"net/http"
)

// embeddedRenderer renders templates embedded in the go bin. Uses cache for
Expand All @@ -29,3 +30,7 @@ func newEmbeddedRenderer() (*embeddedRenderer, error) {
func (r *embeddedRenderer) RenderTemplate(name string, w io.Writer, data any) error {
return renderTemplateFromCache(r.cache, name, w, data)
}

func (r *embeddedRenderer) Error(w http.ResponseWriter, err string, code int) {
Error(w, err, code, false)
}
4 changes: 2 additions & 2 deletions internal/logs/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (h *webHandlers) tailRun(w http.ResponseWriter, r *http.Request) {
Offset int `schema:"offset,required"`
}
if err := decode.All(&params, r); err != nil {
html.Error(w, err.Error(), http.StatusUnprocessableEntity)
html.Error(w, err.Error(), http.StatusUnprocessableEntity, false)
return
}

Expand All @@ -51,7 +51,7 @@ func (h *webHandlers) tailRun(w http.ResponseWriter, r *http.Request) {
Offset: params.Offset,
})
if err != nil {
html.Error(w, err.Error(), http.StatusInternalServerError)
html.Error(w, err.Error(), http.StatusInternalServerError, false)
return
}

Expand Down
Loading

0 comments on commit e5a2f01

Please sign in to comment.