Skip to content

Commit cde46ce

Browse files
committed
- Updated: Simplified code
1 parent 8ae3a66 commit cde46ce

File tree

2 files changed

+5
-13
lines changed

2 files changed

+5
-13
lines changed

route.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func NewRoute(router *Router) RouteInterface {
5959
return &Route{
6060
router: router,
6161
ms: Matchers([]Matcher{}),
62-
varIndexies: map[string]int{},
62+
varIndexies: make(map[string]int),
6363
}
6464
}
6565

@@ -255,7 +255,6 @@ func (r *Route) ExtractVars(req *http.Request) Vars {
255255
urlSeg := strings.Split(req.URL.Path, "/")
256256

257257
vars := Vars(map[string]string{})
258-
259258
for k, v := range r.varIndexies {
260259
vars[k] = urlSeg[v]
261260
}
@@ -293,7 +292,6 @@ func (r *Route) Headers(pairs ...string) RouteInterface {
293292
}
294293

295294
matcher, err := newHeaderMatcher(pairs...)
296-
297295
if err != nil {
298296
r.err = err
299297
}
@@ -327,7 +325,6 @@ func (r *Route) HeadersRegex(pairs ...string) RouteInterface {
327325
}
328326

329327
matcher, err := newHeaderRegexMatcher(pairs...)
330-
331328
if err != nil {
332329
r.err = err
333330
}

router.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,13 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
7979
if !r.SkipClean {
8080

8181
path := req.URL.Path
82-
8382
if r.UseEncodedPath {
8483
path = req.URL.EscapedPath()
8584
}
8685

8786
// Clean path to canonical form and redirect.
88-
if p := cleanPath(path); p != path {
89-
w.Header().Set("Location", p)
87+
if cleanedPath := cleanPath(path); cleanedPath != path {
88+
w.Header().Set("Location", cleanedPath)
9089
w.WriteHeader(http.StatusMovedPermanently)
9190
return
9291
}
@@ -97,7 +96,6 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
9796
}
9897

9998
route := r.triggerMatching(req)
100-
10199
if route == nil {
102100
r.notFoundHandler().ServeHTTP(w, req)
103101
return
@@ -224,10 +222,9 @@ func (r *Router) Head(path string, handlerFunc func(http.ResponseWriter, *http.R
224222
// and then calls Serve with handler to handle requests
225223
// on incoming connections.
226224
func (r *Router) ListenAndServe(port string, callback func(errs []error)) {
227-
var ok bool
228-
errs := make([]error, 0)
229225

230-
if ok, errs = r.HasErrors(); ok {
226+
ok, errs := r.HasErrors()
227+
if ok {
231228
callback(errs)
232229
return
233230
}
@@ -238,8 +235,6 @@ func (r *Router) ListenAndServe(port string, callback func(errs []error)) {
238235
if 0 != len(errs) {
239236
callback(errs)
240237
}
241-
242-
return
243238
}
244239

245240
// HasErrors checks if any errors exists

0 commit comments

Comments
 (0)