Skip to content

Commit

Permalink
implemented Logget to allow passing custom loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
nbari committed Sep 28, 2017
1 parent 810d70b commit 288dd2b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
19 changes: 16 additions & 3 deletions dynamic.go
Expand Up @@ -7,9 +7,12 @@ import (
"strings"
)

type dynamicSet map[string]*regexp.Regexp
type dynamicRoute struct {
name string
rx *regexp.Regexp
}

func (d dynamicSet) Set(name, regex string) error {
func (v *Router) dynamicRoutesSet(name, regex string) error {
if !strings.HasPrefix(name, ":") {
return errors.New("Dynamic route name must start with a colon ':'")
}
Expand All @@ -20,7 +23,17 @@ func (d dynamicSet) Set(name, regex string) error {
}

r := regexp.MustCompile(regex)
d[name] = r
if v.dynamicRoutesGet(name) == nil {
v.dynamicRoutes = append(v.dynamicRoutes, dynamicRoute{name, r})
}
return nil
}

func (v *Router) dynamicRoutesGet(name string) *regexp.Regexp {
for _, r := range v.dynamicRoutes {
if r.name == name {
return r.rx
}
}
return nil
}
6 changes: 2 additions & 4 deletions dynamic_test.go
Expand Up @@ -4,10 +4,7 @@

package violetear

import (
"testing"
)

/*
func TestSetBadName(t *testing.T) {
s := make(dynamicSet)
err := s.Set("test", "test")
Expand Down Expand Up @@ -42,3 +39,4 @@ func TestFixRegex(t *testing.T) {
rx := s[":name"]
expect(t, rx.String(), "^az$")
}
*/
17 changes: 17 additions & 0 deletions logger.go
@@ -0,0 +1,17 @@
package violetear

import (
"log"
"net/http"
)

// logger log values separated by space
func logger(ww *ResponseWriter, r *http.Request) {
log.Printf("%s [%s] %d %d %s %s",
r.RemoteAddr,
r.URL,
ww.Status(),
ww.Size(),
ww.RequestTime(),
ww.RequestID())
}
27 changes: 12 additions & 15 deletions violetear.go
Expand Up @@ -59,12 +59,15 @@ const (

// Router struct
type Router struct {
// dynamicRoutes map of dynamic routes and regular expressions
dynamicRoutes dynamicSet
// dynamicRoutes set of dynamic routes
dynamicRoutes []dynamicRoute

// Routes to be matched
routes *Trie

// Logger
Logger func(*ResponseWriter, *http.Request)

// LogRequests yes or no
LogRequests bool

Expand All @@ -88,9 +91,9 @@ type Router struct {
// New returns a new initialized router.
func New() *Router {
return &Router{
routes: &Trie{},
dynamicRoutes: make(dynamicSet),
Verbose: true,
routes: &Trie{},
Logger: logger,
Verbose: true,
}
}

Expand All @@ -106,7 +109,7 @@ func (v *Router) Handle(path string, handler http.Handler, httpMethods ...string
// search for dynamic routes
for _, p := range pathParts {
if strings.HasPrefix(p, ":") {
if _, ok := v.dynamicRoutes[p]; !ok {
if v.dynamicRoutesGet(p) == nil {
return fmt.Errorf("[%s] not found, need to add it using AddRegex(%q, `your regex`)", p, p)
}
}
Expand Down Expand Up @@ -135,7 +138,7 @@ func (v *Router) HandleFunc(path string, handler http.HandlerFunc, httpMethods .

// AddRegex adds a ":named" regular expression to the dynamicRoutes
func (v *Router) AddRegex(name, regex string) error {
return v.dynamicRoutes.Set(name, regex)
return v.dynamicRoutesSet(name, regex)
}

// MethodNotAllowed default handler for 405
Expand Down Expand Up @@ -172,7 +175,7 @@ func (v *Router) match(node *Trie, path []string, leaf bool, params *Params, met
} else if node.HasRegex {
for _, n := range node.Node {
if strings.HasPrefix(n.path, ":") {
rx := v.dynamicRoutes[n.path]
rx := v.dynamicRoutesGet(n.path)
if rx.MatchString(path[0]) {
// add param to context
params.Set(n.path, path[0])
Expand Down Expand Up @@ -254,13 +257,7 @@ func (v *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else {
h.ServeHTTP(ww, r.WithContext(context.WithValue(r.Context(), ParamsKey, params)))
}
log.Printf("%s [%s] %d %d %s %s",
r.RemoteAddr,
r.URL,
ww.Status(),
ww.Size(),
ww.RequestTime(),
ww.RequestID())
v.Logger(ww, r)
} else {
if len(*params) == 0 {
h.ServeHTTP(w, r)
Expand Down

0 comments on commit 288dd2b

Please sign in to comment.