Skip to content

Commit

Permalink
gofmt everything.
Browse files Browse the repository at this point in the history
  • Loading branch information
keep94 committed Sep 5, 2020
1 parent 5737f25 commit e28b93f
Show file tree
Hide file tree
Showing 4 changed files with 442 additions and 447 deletions.
112 changes: 56 additions & 56 deletions loggers/loggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,105 +8,105 @@
package loggers

import (
"bufio"
"net"
"net/http"
"net/url"
"strings"
"bufio"
"net"
"net/http"
"net/url"
"strings"
)

// Snapshot provides a basic snapshot of a request.
type Snapshot struct {
// Copied from Request.RemoteAddr
RemoteAddr string
// Copied from Request.Method
Method string
// Copied from Request.Proto
Proto string
// Copied from Request.URL
URL *url.URL
// The http referer
Referer string
// The user agent
UserAgent string
// Copied from Request.RemoteAddr
RemoteAddr string
// Copied from Request.Method
Method string
// Copied from Request.Proto
Proto string
// Copied from Request.URL
URL *url.URL
// The http referer
Referer string
// The user agent
UserAgent string
}

func NewSnapshot(r *http.Request) *Snapshot {
urlSnapshot := *r.URL
return &Snapshot{
RemoteAddr: r.RemoteAddr,
Method: r.Method,
Proto: r.Proto,
URL: &urlSnapshot,
Referer: r.Referer(),
UserAgent: r.UserAgent()}
urlSnapshot := *r.URL
return &Snapshot{
RemoteAddr: r.RemoteAddr,
Method: r.Method,
Proto: r.Proto,
URL: &urlSnapshot,
Referer: r.Referer(),
UserAgent: r.UserAgent()}
}

// Capture provides a basic capture of a response
type Capture struct {
// The underlying ResponseWriter
http.ResponseWriter
status int
size int
statusSet bool
// The underlying ResponseWriter
http.ResponseWriter
status int
size int
statusSet bool
}

// Support hijacking if underlying response writer supports it
func (c *Capture) Hijack() (net.Conn, *bufio.ReadWriter, error) {
c.statusSet = true
return c.ResponseWriter.(http.Hijacker).Hijack()
c.statusSet = true
return c.ResponseWriter.(http.Hijacker).Hijack()
}

// The HTTP status code of the response
func (c *Capture) Status() int {
return c.status
return c.status
}

// The size of the resposne
func (c *Capture) Size() int {
return c.size
return c.size
}

func (c *Capture) Write(b []byte) (int, error) {
result, err := c.ResponseWriter.Write(b)
c.size += result
c.maybeSetStatus(http.StatusOK)
return result, err
result, err := c.ResponseWriter.Write(b)
c.size += result
c.maybeSetStatus(http.StatusOK)
return result, err
}

func (c *Capture) WriteHeader(status int) {
c.ResponseWriter.WriteHeader(status)
c.maybeSetStatus(status)
c.ResponseWriter.WriteHeader(status)
c.maybeSetStatus(status)
}

// HasStatus returns true if response has a status.
func (c *Capture) HasStatus() bool {
return c.statusSet
return c.statusSet
}

func (c *Capture) maybeSetStatus(status int) {
if !c.statusSet {
c.status = status
c.statusSet = true
}
if !c.statusSet {
c.status = status
c.statusSet = true
}
}

// ApacheUser formats user info in a request in apache style. That is missing
// or empty user name is formatted as '-'
func ApacheUser(user *url.Userinfo) string {
result := "-"
if user != nil {
if name := user.Username(); name != "" {
result = name
}
}
return result
result := "-"
if user != nil {
if name := user.Username(); name != "" {
result = name
}
}
return result
}

// StripPort strips the port number off of a remote address
func StripPort(remoteAddr string) string {
if index := strings.LastIndex(remoteAddr, ":"); index != -1 {
return remoteAddr[:index]
}
return remoteAddr
if index := strings.LastIndex(remoteAddr, ":"); index != -1 {
return remoteAddr[:index]
}
return remoteAddr
}
26 changes: 12 additions & 14 deletions loggers/loggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,25 @@
package loggers_test

import (
"net/url"
"testing"
"github.com/keep94/weblogs/loggers"
"github.com/keep94/weblogs/loggers"
"net/url"
"testing"
)

func TestApacheUser(t *testing.T) {
verifyString(t, "-", loggers.ApacheUser(nil))
verifyString(t, "-", loggers.ApacheUser(url.User("")))
verifyString(t, "tom", loggers.ApacheUser(url.User("tom")))
verifyString(t, "-", loggers.ApacheUser(nil))
verifyString(t, "-", loggers.ApacheUser(url.User("")))
verifyString(t, "tom", loggers.ApacheUser(url.User("tom")))
}

func TestStripPort(t *testing.T) {
verifyString(t, "[::1]", loggers.StripPort("[::1]:4050"))
verifyString(t, "10.0.1.3", loggers.StripPort("10.0.1.3:25972"))
verifyString(t, "10.0.1.3", loggers.StripPort("10.0.1.3"))
verifyString(t, "[::1]", loggers.StripPort("[::1]:4050"))
verifyString(t, "10.0.1.3", loggers.StripPort("10.0.1.3:25972"))
verifyString(t, "10.0.1.3", loggers.StripPort("10.0.1.3"))
}

func verifyString(t *testing.T, expected, actual string) {
if expected != actual {
t.Errorf("Want: %s, Got: %s", expected, actual)
}
if expected != actual {
t.Errorf("Want: %s, Got: %s", expected, actual)
}
}


Loading

0 comments on commit e28b93f

Please sign in to comment.