Skip to content

Commit 8fad089

Browse files
committed
Add client ip and cloudflare ray id
1 parent 220631a commit 8fad089

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

constant.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const (
66
ContentType = "Content-Type"
77
OriginalURI = "X-Original-URI"
88
RequestId = "X-Request-ID"
9+
ClientIp = "CF-Connecting-IP"
10+
RayId = "CF-RAY"
911
DefaultFormat = "text/html"
1012
DefaultErrorTemplateName = "error.html"
1113
)

hander.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type Response struct {
1212
ErrorCode int `json:"error_code"`
1313
RequestId string `json:"request_id"`
1414
OriginalURI string `json:"original_uri"`
15+
RayId string `json:"ray_id"`
16+
ClientIp string `json:"client_ip"`
1517
Message string `json:"message"`
1618
}
1719

@@ -54,6 +56,8 @@ func errorHandler(t *template.Template) func(http.ResponseWriter, *http.Request)
5456
ErrorCode: code,
5557
RequestId: r.Header.Get(RequestId),
5658
OriginalURI: r.Header.Get(OriginalURI),
59+
ClientIp: r.Header.Get(ClientIp),
60+
RayId: r.Header.Get(RayId),
5761
Message: message,
5862
}
5963

www/error.html

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
font-weight: 300;
5050
}
5151

52+
span {
53+
margin-right: 5px;
54+
}
5255
</style>
5356
</head>
5457
<body>
@@ -61,12 +64,22 @@ <h2>
6164
</h2>
6265
{{- if .RequestId }}
6366
<h3>
64-
Request ID: {{- .RequestId }}
67+
<span>Request ID:</span>{{- .RequestId }}
6568
</h3>
6669
{{- end }}
6770
{{- if .OriginalURI }}
6871
<h3>
69-
Original URI: {{- .OriginalURI }}
72+
<span>Original URI:</span>{{- .OriginalURI }}
73+
</h3>
74+
{{- end }}
75+
{{- if .RayId }}
76+
<h3>
77+
<span>Cloudflare Ray ID:</span>{{- .RayId }}
78+
</h3>
79+
{{- end }}
80+
{{- if .ClientIp }}
81+
<h3>
82+
<span><b>Your IP:</b></span>{{- .ClientIp }}
7083
</h3>
7184
{{- end }}
7285
</div>

0 commit comments

Comments
 (0)