Public source validation / validate (push) Failing after 3m8s
36 lines
1.2 KiB
Go
36 lines
1.2 KiB
Go
package problem
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/itworx/pulse/internal/correlation"
|
|
)
|
|
|
|
type Details struct {
|
|
Type string `json:"type"`
|
|
Title string `json:"title"`
|
|
Status int `json:"status"`
|
|
Code string `json:"code"`
|
|
Detail string `json:"detail"`
|
|
CorrelationID string `json:"correlationId"`
|
|
Fields map[string]string `json:"fields,omitempty"`
|
|
}
|
|
|
|
func Write(response http.ResponseWriter, request *http.Request, status int, code, title, detail string, fields map[string]string) {
|
|
id := correlation.FromContext(request.Context())
|
|
if id == "" {
|
|
id = correlation.New()
|
|
}
|
|
details := Details{Type: "https://pulse.local/problems/" + code, Title: title, Status: status, Code: code, Detail: detail, CorrelationID: id, Fields: fields}
|
|
response.Header().Set("Content-Type", "application/problem+json")
|
|
response.Header().Set("Cache-Control", "private, no-store")
|
|
response.Header().Set(correlation.Header, id)
|
|
response.WriteHeader(status)
|
|
_ = json.NewEncoder(response).Encode(details)
|
|
}
|
|
|
|
func Middleware(next http.Handler) http.Handler {
|
|
return correlation.Middleware(next)
|
|
}
|