Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Go Httpbin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Nix
Go Httpbin
Commits
10626018
Commit
10626018
authored
7 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Add status code, body size, and request duration to logs
parent
9fb635b7
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
cmd/go-httpbin/main.go
+3
-1
3 additions, 1 deletion
cmd/go-httpbin/main.go
httpbin/middleware.go
+50
-7
50 additions, 7 deletions
httpbin/middleware.go
with
53 additions
and
8 deletions
cmd/go-httpbin/main.go
+
3
−
1
View file @
10626018
...
@@ -27,6 +27,8 @@ func main() {
...
@@ -27,6 +27,8 @@ func main() {
flag
.
DurationVar
(
&
maxDuration
,
"max-duration"
,
httpbin
.
DefaultMaxDuration
,
"Maximum duration a response may take"
)
flag
.
DurationVar
(
&
maxDuration
,
"max-duration"
,
httpbin
.
DefaultMaxDuration
,
"Maximum duration a response may take"
)
flag
.
Parse
()
flag
.
Parse
()
log
.
SetFlags
(
log
.
Ldate
|
log
.
Lmicroseconds
|
log
.
LUTC
)
// Command line flags take precedence over environment vars, so we only
// Command line flags take precedence over environment vars, so we only
// check for environment vars if we have default values for our command
// check for environment vars if we have default values for our command
// line flags.
// line flags.
...
@@ -62,6 +64,6 @@ func main() {
...
@@ -62,6 +64,6 @@ func main() {
})
})
listenAddr
:=
net
.
JoinHostPort
(
"0.0.0.0"
,
strconv
.
Itoa
(
port
))
listenAddr
:=
net
.
JoinHostPort
(
"0.0.0.0"
,
strconv
.
Itoa
(
port
))
log
.
Printf
(
"
listening on port
%s"
,
listenAddr
)
log
.
Printf
(
"
addr=
%s"
,
listenAddr
)
log
.
Fatal
(
http
.
ListenAndServe
(
listenAddr
,
h
.
Handler
()))
log
.
Fatal
(
http
.
ListenAndServe
(
listenAddr
,
h
.
Handler
()))
}
}
This diff is collapsed.
Click to expand it.
httpbin/middleware.go
+
50
−
7
View file @
10626018
...
@@ -4,15 +4,9 @@ import (
...
@@ -4,15 +4,9 @@ import (
"fmt"
"fmt"
"log"
"log"
"net/http"
"net/http"
"time"
)
)
func
logger
(
h
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
h
.
ServeHTTP
(
w
,
r
)
log
.
Printf
(
"%s %s %s"
,
r
.
RemoteAddr
,
r
.
Method
,
r
.
URL
)
})
}
func
cors
(
h
http
.
Handler
)
http
.
Handler
{
func
cors
(
h
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
origin
:=
r
.
Header
.
Get
(
"Origin"
)
origin
:=
r
.
Header
.
Get
(
"Origin"
)
...
@@ -56,3 +50,52 @@ func limitRequestSize(maxSize int64, h http.Handler) http.Handler {
...
@@ -56,3 +50,52 @@ func limitRequestSize(maxSize int64, h http.Handler) http.Handler {
h
.
ServeHTTP
(
w
,
r
)
h
.
ServeHTTP
(
w
,
r
)
})
})
}
}
// metaResponseWriter implements is an http.ResponseWriter and http.Flusher
// that records its status code and body size for logging purposes.
type
metaResponseWriter
struct
{
w
http
.
ResponseWriter
status
int
size
int
}
func
(
mw
*
metaResponseWriter
)
Write
(
b
[]
byte
)
(
int
,
error
)
{
size
,
err
:=
mw
.
w
.
Write
(
b
)
mw
.
size
+=
size
return
size
,
err
}
func
(
mw
*
metaResponseWriter
)
WriteHeader
(
s
int
)
{
mw
.
w
.
WriteHeader
(
s
)
mw
.
status
=
s
}
func
(
mw
*
metaResponseWriter
)
Flush
()
{
f
:=
mw
.
w
.
(
http
.
Flusher
)
f
.
Flush
()
}
func
(
mw
*
metaResponseWriter
)
Header
()
http
.
Header
{
return
mw
.
w
.
Header
()
}
func
(
mw
*
metaResponseWriter
)
Status
()
int
{
if
mw
.
status
==
0
{
return
http
.
StatusOK
}
return
mw
.
status
}
func
(
mw
*
metaResponseWriter
)
Size
()
int
{
return
mw
.
size
}
func
logger
(
h
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
mw
:=
&
metaResponseWriter
{
w
:
w
}
t
:=
time
.
Now
()
h
.
ServeHTTP
(
mw
,
r
)
duration
:=
time
.
Now
()
.
Sub
(
t
)
log
.
Printf
(
"status=%d method=%s uri=%q size=%d duration=%s"
,
mw
.
Status
(),
r
.
Method
,
r
.
URL
.
RequestURI
(),
mw
.
Size
(),
duration
)
})
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment