mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-30 19:06:33 +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 {
|
func wrapMiddleware(o *config.Options, mux *http.ServeMux) http.Handler {
|
||||||
c := middleware.NewChain()
|
c := middleware.NewChain()
|
||||||
c = c.Append(middleware.NewHandler(log.Logger))
|
c = c.Append(log.NewHandler(log.Logger))
|
||||||
c = c.Append(middleware.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
|
c = c.Append(log.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
|
||||||
middleware.FromRequest(r).Debug().
|
log.FromRequest(r).Debug().
|
||||||
Dur("duration", duration).
|
Dur("duration", duration).
|
||||||
Int("size", size).
|
Int("size", size).
|
||||||
Int("status", status).
|
Int("status", status).
|
||||||
|
@ -159,11 +159,11 @@ func wrapMiddleware(o *config.Options, mux *http.ServeMux) http.Handler {
|
||||||
if o != nil && len(o.Headers) != 0 {
|
if o != nil && len(o.Headers) != 0 {
|
||||||
c = c.Append(middleware.SetHeaders(o.Headers))
|
c = c.Append(middleware.SetHeaders(o.Headers))
|
||||||
}
|
}
|
||||||
c = c.Append(middleware.ForwardedAddrHandler("fwd_ip"))
|
c = c.Append(log.ForwardedAddrHandler("fwd_ip"))
|
||||||
c = c.Append(middleware.RemoteAddrHandler("ip"))
|
c = c.Append(log.RemoteAddrHandler("ip"))
|
||||||
c = c.Append(middleware.UserAgentHandler("user_agent"))
|
c = c.Append(log.UserAgentHandler("user_agent"))
|
||||||
c = c.Append(middleware.RefererHandler("referer"))
|
c = c.Append(log.RefererHandler("referer"))
|
||||||
c = c.Append(middleware.RequestIDHandler("req_id", "Request-Id"))
|
c = c.Append(log.RequestIDHandler("req_id", "Request-Id"))
|
||||||
c = c.Append(middleware.Healthcheck("/ping", version.UserAgent()))
|
c = c.Append(middleware.Healthcheck("/ping", version.UserAgent()))
|
||||||
return c.Then(mux)
|
return c.Then(mux)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/pomerium/pomerium/internal/log"
|
||||||
"github.com/pomerium/pomerium/internal/templates"
|
"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.
|
// 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.
|
// 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 == "" {
|
if message == "" {
|
||||||
message = http.StatusText(code)
|
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 {
|
var response struct {
|
||||||
Error string `json:"error"`
|
Error string `json:"error"`
|
||||||
}
|
}
|
||||||
|
@ -48,10 +54,12 @@ func ErrorResponse(rw http.ResponseWriter, req *http.Request, message string, co
|
||||||
Code int
|
Code int
|
||||||
Title string
|
Title string
|
||||||
Message string
|
Message string
|
||||||
|
RequestID string
|
||||||
}{
|
}{
|
||||||
Code: code,
|
Code: code,
|
||||||
Title: title,
|
Title: title,
|
||||||
Message: message,
|
Message: message,
|
||||||
|
RequestID: reqID,
|
||||||
}
|
}
|
||||||
templates.New().ExecuteTemplate(rw, "error.html", t)
|
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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
@ -9,16 +9,9 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pomerium/pomerium/internal/log"
|
|
||||||
"github.com/rs/zerolog"
|
"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.
|
// NewHandler injects log into requests context.
|
||||||
func NewHandler(log zerolog.Logger) func(http.Handler) http.Handler {
|
func NewHandler(log zerolog.Logger) func(http.Handler) http.Handler {
|
||||||
return func(next 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 (
|
import (
|
||||||
"bytes"
|
"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:
|
// The original work was derived from Goji's middleware, source:
|
||||||
// https://github.com/zenazn/goji/tree/master/web/middleware
|
// https://github.com/zenazn/goji/tree/master/web/middleware
|
||||||
|
@ -175,7 +175,6 @@ type http2FancyWriter struct {
|
||||||
|
|
||||||
func (f *http2FancyWriter) Flush() {
|
func (f *http2FancyWriter) Flush() {
|
||||||
f.wroteHeader = true
|
f.wroteHeader = true
|
||||||
|
|
||||||
fl := f.basicWriter.ResponseWriter.(http.Flusher)
|
fl := f.basicWriter.ResponseWriter.(http.Flusher)
|
||||||
fl.Flush()
|
fl.Flush()
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package middleware
|
package log // import "github.com/pomerium/pomerium/internal/log"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
|
@ -266,7 +266,9 @@ func New() *template.Template {
|
||||||
<h1 class="title">{{.Title}}</h1>
|
<h1 class="title">{{.Title}}</h1>
|
||||||
<section>
|
<section>
|
||||||
<p class="message">{{.Message}}.</p>
|
<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>
|
</section>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Add table
Reference in a new issue