Skip to content

Commit 98b3480

Browse files
Dean KarnDean Karn
authored andcommitted
some minor code cleanup + use new ansi library for logging middleware example
1 parent b29f7cf commit 98b3480

File tree

4 files changed

+22
-46
lines changed

4 files changed

+22
-46
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
##LARS
22
<img align="right" src="https://raw.githubusercontent.com/go-playground/lars/master/examples/README/test.gif">
3-
![Project status](https://img.shields.io/badge/version-3.6.0-green.svg)
3+
![Project status](https://img.shields.io/badge/version-3.7.0-green.svg)
44
[![Build Status](https://semaphoreci.com/api/v1/projects/4351aa2d-2f94-40be-a6ef-85c248490378/679708/badge.svg)](https://semaphoreci.com/joeybloggs/lars)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/lars/badge.svg?branch=master)](https://coveralls.io/github/go-playground/lars?branch=master)
66
[![Go Report Card](https://goreportcard.com/badge/go-playground/lars)](https://goreportcard.com/report/go-playground/lars)
77
[![GoDoc](https://godoc.org/github.com/go-playground/lars?status.svg)](https://godoc.org/github.com/go-playground/lars)
88
![License](https://img.shields.io/dub/l/vibe-d.svg)
99
[![Gitter](https://badges.gitter.im/go-playground/lars.svg)](https://gitter.im/go-playground/lars?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
1010

11-
LARS is a fast radix-tree based, zero allocation, HTTP router for Go. [ view examples](https://github.com/go-playground/lars/tree/master/examples)
11+
LARS is a fast radix-tree based, zero allocation, HTTP router for Go. [ view examples](https://github.com/go-playground/lars/tree/master/examples). If looking for a more pure Go solution, be sure to check out [pure](https://github.com/go-playground/pure) which is essentially a pure version of lars
1212

1313
Why Another HTTP Router?
1414
------------------------

examples/middleware/logging-recovery/logging_recovery.go

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,15 @@ import (
66
"runtime"
77
"time"
88

9+
"github.com/go-playground/ansi"
910
"github.com/go-playground/lars"
1011
)
1112

12-
// ANSIEscSeq is a predefined ANSI escape sequence
13-
type ANSIEscSeq string
14-
15-
// ANSI escape sequences
16-
// NOTE: in an standard xterm terminal the light colors will appear BOLD instead of the light variant
1713
const (
18-
Black ANSIEscSeq = "\x1b[30m"
19-
DarkGray = "\x1b[30;1m"
20-
Blue = "\x1b[34m"
21-
LightBlue = "\x1b[34;1m"
22-
Green = "\x1b[32m"
23-
LightGreen = "\x1b[32;1m"
24-
Cyan = "\x1b[36m"
25-
LightCyan = "\x1b[36;1m"
26-
Red = "\x1b[31m"
27-
LightRed = "\x1b[31;1m"
28-
Magenta = "\x1b[35m"
29-
LightMagenta = "\x1b[35;1m"
30-
Brown = "\x1b[33m"
31-
Yellow = "\x1b[33;1m"
32-
LightGray = "\x1b[37m"
33-
White = "\x1b[37;1m"
34-
Underscore = "\x1b[4m"
35-
Blink = "\x1b[5m"
36-
Inverse = "\x1b[7m"
37-
Reset = "\x1b[0m"
14+
status500 = ansi.Underline + ansi.Blink + ansi.Red
15+
status400 = ansi.Red
16+
status300 = ansi.Yellow
17+
status = ansi.Green
3818
)
3919

4020
// LoggingAndRecovery handle HTTP request logging + recovery
@@ -46,34 +26,31 @@ func LoggingAndRecovery(c lars.Context) {
4626
if err := recover(); err != nil {
4727
trace := make([]byte, 1<<16)
4828
n := runtime.Stack(trace, true)
49-
log.Printf(" %srecovering from panic: %+v\nStack Trace:\n %s%s", Red, err, trace[:n], Reset)
29+
log.Printf(" %srecovering from panic: %+v\nStack Trace:\n %s%s", ansi.Red, err, trace[:n], ansi.Reset)
5030
HandlePanic(c, trace[:n])
5131
return
5232
}
5333
}()
5434

5535
c.Next()
5636

57-
var color string
58-
37+
color := status
5938
res := c.Response()
6039
req := c.Request()
6140
code := res.Status()
6241

6342
switch {
6443
case code >= http.StatusInternalServerError:
65-
color = Underscore + Blink + Red
44+
color = status500
6645
case code >= http.StatusBadRequest:
67-
color = Red
46+
color = status400
6847
case code >= http.StatusMultipleChoices:
69-
color = Yellow
70-
default:
71-
color = Green
48+
color = status300
7249
}
7350

7451
t2 := time.Now()
7552

76-
log.Printf("%s %d %s[%s%s%s] %q %v %d\n", color, code, Reset, color, req.Method, Reset, req.URL, t2.Sub(t1), res.Size())
53+
log.Printf("%s %d %s[%s%s%s] %q %v %d\n", color, code, ansi.Reset, color, req.Method, ansi.Reset, req.URL, t2.Sub(t1), res.Size())
7754
}
7855

7956
// HandlePanic handles graceful panic by redirecting to friendly error page or rendering a friendly error page.

lars.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ import (
1313
// HTTP Constant Terms and Variables
1414
const (
1515
// CONNECT HTTP method
16-
CONNECT = "CONNECT"
16+
CONNECT = http.MethodConnect
1717
// DELETE HTTP method
18-
DELETE = "DELETE"
18+
DELETE = http.MethodDelete
1919
// GET HTTP method
20-
GET = "GET"
20+
GET = http.MethodGet
2121
// HEAD HTTP method
22-
HEAD = "HEAD"
22+
HEAD = http.MethodHead
2323
// OPTIONS HTTP method
24-
OPTIONS = "OPTIONS"
24+
OPTIONS = http.MethodOptions
2525
// PATCH HTTP method
26-
PATCH = "PATCH"
26+
PATCH = http.MethodPatch
2727
// POST HTTP method
28-
POST = "POST"
28+
POST = http.MethodPost
2929
// PUT HTTP method
30-
PUT = "PUT"
30+
PUT = http.MethodPut
3131
// TRACE HTTP method
32-
TRACE = "TRACE"
32+
TRACE = http.MethodTrace
3333

3434
//-------------
3535
// Media types

node.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import "net/url"
1111
type nodeType uint8
1212

1313
const (
14-
// isStatic nodeType = iota // default
1514
isRoot nodeType = iota + 1
1615
hasParams
1716
matchesAny

0 commit comments

Comments
 (0)