Browse Source

Added as default middleware and removed ip for now

pull/545/head
Weslei Juan Novaes Pereira 6 years ago
parent
commit
9d3b8585b4
  1. 7
      .idea/workspace.xml
  2. 7
      go.mod
  3. 2
      go.sum
  4. 20
      logger.go
  5. 2
      logger_test.go
  6. 5
      mux.go

7
.idea/workspace.xml

@ -2,7 +2,9 @@ @@ -2,7 +2,9 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="b10d9bd6-ec23-43f3-bef7-13a524307117" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/go.mod" beforeDir="false" afterPath="$PROJECT_DIR$/go.mod" afterDir="false" />
<change beforePath="$PROJECT_DIR$/go.sum" beforeDir="false" afterPath="$PROJECT_DIR$/go.sum" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
@ -34,6 +36,11 @@ @@ -34,6 +36,11 @@
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="settings.editor.selected.configurable" value="preferences.lookFeel" />
</component>
<component name="RecentsManager">
<key name="MoveFile.RECENT_KEYS">
<recent name="C:\movidesk\mux\main" />
</key>
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>

7
go.mod

@ -1,5 +1,8 @@ @@ -1,5 +1,8 @@
module github.com/gorilla/mux
module gorilla/mux
go 1.12
require github.com/stretchr/testify v1.4.0
require (
github.com/gorilla/mux v1.7.3
github.com/stretchr/testify v1.4.0
)

2
go.sum

@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

20
logger.go

@ -32,8 +32,6 @@ type LogFormatterParams struct { @@ -32,8 +32,6 @@ type LogFormatterParams struct {
StatusCode int
// Latency is how much time the server cost to process a certain request.
Latency time.Duration
// ClientIP equals Context's ClientIP method.
ClientIP string
// Method is the HTTP method given to the request.
Method string
// Path is a path the client requests.
@ -97,7 +95,7 @@ func Logger(next http.Handler) http.Handler { @@ -97,7 +95,7 @@ func Logger(next http.Handler) http.Handler {
}
func LoggerWithConfig(c LogConfig) MiddlewareFunc {
return func (next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
out := c.Output
if out == nil {
out = os.Stdout
@ -120,13 +118,12 @@ func LoggerWithConfig(c LogConfig) MiddlewareFunc { @@ -120,13 +118,12 @@ func LoggerWithConfig(c LogConfig) MiddlewareFunc {
stop := time.Now()
p := LogFormatterParams{
Request: r,
TimeStamp: stop,
Latency: stop.Sub(start),
ClientIP: "",
Method: r.Method,
Request: r,
TimeStamp: stop,
Latency: stop.Sub(start),
Method: r.Method,
StatusCode: sw.status,
Path: path,
Path: path,
}
fmt.Fprintf(out, formatter(p))
@ -143,12 +140,11 @@ func formatter(p LogFormatterParams) string { @@ -143,12 +140,11 @@ func formatter(p LogFormatterParams) string {
p.Latency = p.Latency - p.Latency%time.Second
}
return fmt.Sprintf("[MUX] %v |%s %3d %s| %13v | %15s |%s %-7s %s %s\n",
return fmt.Sprintf("[MUX] %v |%s %3d %s| %13v |%s %-7s %s %s\n",
p.TimeStamp.Format("2006/01/02 - 15:04:05"),
statusColor, p.StatusCode, resetColor,
p.Latency,
p.ClientIP,
methodColor, p.Method, resetColor,
p.Path,
)
}
}

2
logger_test.go

@ -16,7 +16,7 @@ func testHandler(status int) http.HandlerFunc { @@ -16,7 +16,7 @@ func testHandler(status int) http.HandlerFunc {
func TestLogger(t *testing.T) {
buffer := new(bytes.Buffer)
r := NewRouter()
r.Use(LoggerWithConfig(LogConfig{Output:buffer}))
r.Use(LoggerWithConfig(LogConfig{Output: buffer}))
r.HandleFunc("/example", testHandler(http.StatusOK)).
Methods("GET", "POST", "PUT", "DELETE", "OPTIONS")

5
mux.go

@ -23,7 +23,10 @@ var ( @@ -23,7 +23,10 @@ var (
// NewRouter returns a new router instance.
func NewRouter() *Router {
return &Router{namedRoutes: make(map[string]*Route)}
r := &Router{namedRoutes: make(map[string]*Route)}
r.Use(Logger)
return r
}
// Router registers routes to be matched and dispatches a handler.

Loading…
Cancel
Save