mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-29 10:26:29 +02:00
internal/httputil: add request id to error page (#144)
This commit is contained in:
parent
3d6471c4b3
commit
f68338c888
7 changed files with 31 additions and 29 deletions
|
@ -143,9 +143,9 @@ func newProxyService(opt *config.Options, mux *http.ServeMux) (*proxy.Proxy, err
|
|||
|
||||
func wrapMiddleware(o *config.Options, mux *http.ServeMux) http.Handler {
|
||||
c := middleware.NewChain()
|
||||
c = c.Append(middleware.NewHandler(log.Logger))
|
||||
c = c.Append(middleware.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
|
||||
middleware.FromRequest(r).Debug().
|
||||
c = c.Append(log.NewHandler(log.Logger))
|
||||
c = c.Append(log.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
|
||||
log.FromRequest(r).Debug().
|
||||
Dur("duration", duration).
|
||||
Int("size", size).
|
||||
Int("status", status).
|
||||
|
@ -159,11 +159,11 @@ func wrapMiddleware(o *config.Options, mux *http.ServeMux) http.Handler {
|
|||
if o != nil && len(o.Headers) != 0 {
|
||||
c = c.Append(middleware.SetHeaders(o.Headers))
|
||||
}
|
||||
c = c.Append(middleware.ForwardedAddrHandler("fwd_ip"))
|
||||
c = c.Append(middleware.RemoteAddrHandler("ip"))
|
||||
c = c.Append(middleware.UserAgentHandler("user_agent"))
|
||||
c = c.Append(middleware.RefererHandler("referer"))
|
||||
c = c.Append(middleware.RequestIDHandler("req_id", "Request-Id"))
|
||||
c = c.Append(log.ForwardedAddrHandler("fwd_ip"))
|
||||
c = c.Append(log.RemoteAddrHandler("ip"))
|
||||
c = c.Append(log.UserAgentHandler("user_agent"))
|
||||
c = c.Append(log.RefererHandler("referer"))
|
||||
c = c.Append(log.RequestIDHandler("req_id", "Request-Id"))
|
||||
c = c.Append(middleware.Healthcheck("/ping", version.UserAgent()))
|
||||
return c.Then(mux)
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/pomerium/pomerium/internal/log"
|
||||
"github.com/pomerium/pomerium/internal/templates"
|
||||
)
|
||||
|
||||
|
@ -31,11 +32,16 @@ func CodeForError(err error) int {
|
|||
|
||||
// ErrorResponse renders an error page for errors given a message and a status code.
|
||||
// If no message is passed, defaults to the text of the status code.
|
||||
func ErrorResponse(rw http.ResponseWriter, req *http.Request, message string, code int) {
|
||||
func ErrorResponse(rw http.ResponseWriter, r *http.Request, message string, code int) {
|
||||
if message == "" {
|
||||
message = http.StatusText(code)
|
||||
}
|
||||
if req.Header.Get("Accept") == "application/json" {
|
||||
reqID := ""
|
||||
id, ok := log.IDFromRequest(r)
|
||||
if ok {
|
||||
reqID = id
|
||||
}
|
||||
if r.Header.Get("Accept") == "application/json" {
|
||||
var response struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
@ -45,13 +51,15 @@ func ErrorResponse(rw http.ResponseWriter, req *http.Request, message string, co
|
|||
title := http.StatusText(code)
|
||||
rw.WriteHeader(code)
|
||||
t := struct {
|
||||
Code int
|
||||
Title string
|
||||
Message string
|
||||
Code int
|
||||
Title string
|
||||
Message string
|
||||
RequestID string
|
||||
}{
|
||||
Code: code,
|
||||
Title: title,
|
||||
Message: message,
|
||||
Code: code,
|
||||
Title: title,
|
||||
Message: message,
|
||||
RequestID: reqID,
|
||||
}
|
||||
templates.New().ExecuteTemplate(rw, "error.html", t)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package middleware // import "github.com/pomerium/pomerium/internal/middleware"
|
||||
package log // import "github.com/pomerium/pomerium/internal/log"
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
@ -9,16 +9,9 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pomerium/pomerium/internal/log"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
// FromRequest gets the logger in the request's context.
|
||||
// This is a shortcut for log.Ctx(r.Context())
|
||||
func FromRequest(r *http.Request) *zerolog.Logger {
|
||||
return log.Ctx(r.Context())
|
||||
}
|
||||
|
||||
// NewHandler injects log into requests context.
|
||||
func NewHandler(log zerolog.Logger) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
|
@ -1,4 +1,4 @@
|
|||
package middleware // import "github.com/pomerium/pomerium/internal/middleware"
|
||||
package log // import "github.com/pomerium/pomerium/internal/log"
|
||||
|
||||
import (
|
||||
"bytes"
|
|
@ -1,4 +1,4 @@
|
|||
package middleware // import "github.com/pomerium/pomerium/internal/middleware"
|
||||
package log // import "github.com/pomerium/pomerium/internal/log"
|
||||
|
||||
// The original work was derived from Goji's middleware, source:
|
||||
// https://github.com/zenazn/goji/tree/master/web/middleware
|
||||
|
@ -175,7 +175,6 @@ type http2FancyWriter struct {
|
|||
|
||||
func (f *http2FancyWriter) Flush() {
|
||||
f.wroteHeader = true
|
||||
|
||||
fl := f.basicWriter.ResponseWriter.(http.Flusher)
|
||||
fl.Flush()
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package middleware
|
||||
package log // import "github.com/pomerium/pomerium/internal/log"
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
|
@ -266,7 +266,9 @@ func New() *template.Template {
|
|||
<h1 class="title">{{.Title}}</h1>
|
||||
<section>
|
||||
<p class="message">{{.Message}}.</p>
|
||||
<p class="message">Troubleshoot your <a href="/.pomerium">session</a>.</p>
|
||||
<p class="message">Troubleshoot your <a href="/.pomerium">session</a>.</br>
|
||||
{{if .RequestID}} Request {{.RequestID}} {{end}}
|
||||
</p>
|
||||
</section>
|
||||
</form>
|
||||
</div>
|
||||
|
|
Loading…
Add table
Reference in a new issue