Skip to content

Commit 79da4c4

Browse files
joeybloggsjoeybloggs
authored andcommitted
minor code cleanup + auto handle OPTION requests set to false by default.
1 parent 53f3ddb commit 79da4c4

File tree

4 files changed

+19
-24
lines changed

4 files changed

+19
-24
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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.1-green.svg)
3+
![Project status](https://img.shields.io/badge/version-3.2.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)

context.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ type Ctx struct {
7474
index int
7575
formParsed bool
7676
multipartFormParsed bool
77-
l *LARS
7877
}
7978

8079
var _ context.Context = &Ctx{}
@@ -84,7 +83,6 @@ func NewContext(l *LARS) *Ctx {
8483

8584
c := &Ctx{
8685
params: make(Params, l.mostParams),
87-
l: l,
8886
}
8987

9088
c.response = newResponse(nil, c)
@@ -421,7 +419,7 @@ func (c *Ctx) Inline(r io.Reader, filename string) (err error) {
421419
// json.NewDecoder(io.LimitReader(c.request.Body, maxMemory)).Decode(v).
422420
func (c *Ctx) Decode(includeFormQueryParams bool, maxMemory int64, v interface{}) (err error) {
423421

424-
c.l.initFormDecoder()
422+
initFormDecoder()
425423

426424
typ := c.request.Header.Get(ContentType)
427425

@@ -441,19 +439,19 @@ func (c *Ctx) Decode(includeFormQueryParams bool, maxMemory int64, v interface{}
441439

442440
if err = c.ParseForm(); err == nil {
443441
if includeFormQueryParams {
444-
err = c.l.formDecoder.Decode(v, c.request.Form)
442+
err = formDecoder.Decode(v, c.request.Form)
445443
} else {
446-
err = c.l.formDecoder.Decode(v, c.request.PostForm)
444+
err = formDecoder.Decode(v, c.request.PostForm)
447445
}
448446
}
449447

450448
case MultipartForm:
451449

452450
if err = c.ParseMultipartForm(maxMemory); err == nil {
453451
if includeFormQueryParams {
454-
err = c.l.formDecoder.Decode(v, c.request.Form)
452+
err = formDecoder.Decode(v, c.request.Form)
455453
} else {
456-
err = c.l.formDecoder.Decode(v, c.request.MultipartForm.Value)
454+
err = formDecoder.Decode(v, c.request.MultipartForm.Value)
457455
}
458456
}
459457
}

lars.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,6 @@ type LARS struct {
152152
// if enabled automatically handles OPTION requests; manually configured OPTION
153153
// handlers take precidence. default true
154154
automaticallyHandleOPTIONS bool
155-
156-
// form decoder + once initialization
157-
formDecoder *form.Decoder
158-
formDecoderInit sync.Once
159155
}
160156

161157
// RouteMap contains a single routes full path
@@ -179,6 +175,9 @@ var (
179175
automaticOPTIONSHandler = func(c Context) {
180176
c.Response().WriteHeader(http.StatusOK)
181177
}
178+
179+
formDecoder *form.Decoder
180+
formDecoderInit sync.Once
182181
)
183182

184183
// New Creates and returns a new lars instance
@@ -197,7 +196,7 @@ func New() *LARS {
197196
http405: []HandlerFunc{methodNotAllowedHandler},
198197
redirectTrailingSlash: true,
199198
handleMethodNotAllowed: false,
200-
automaticallyHandleOPTIONS: true,
199+
automaticallyHandleOPTIONS: false,
201200
}
202201

203202
l.routeGroup.lars = l
@@ -213,19 +212,19 @@ func New() *LARS {
213212
return l
214213
}
215214

215+
func initFormDecoder() {
216+
formDecoderInit.Do(func() {
217+
formDecoder = form.NewDecoder()
218+
})
219+
}
220+
216221
// BuiltInFormDecoder returns the built in form decoder github.com/go-playground/form
217222
// in order for custom type to be registered.
218223
func (l *LARS) BuiltInFormDecoder() *form.Decoder {
219224

220-
l.initFormDecoder()
221-
222-
return l.formDecoder
223-
}
225+
initFormDecoder()
224226

225-
func (l *LARS) initFormDecoder() {
226-
l.formDecoderInit.Do(func() {
227-
l.formDecoder = form.NewDecoder()
228-
})
227+
return formDecoder
229228
}
230229

231230
// RegisterCustomHandler registers a custom handler that gets wrapped by HandlerFunc
@@ -305,6 +304,7 @@ func (l *LARS) Serve() http.Handler {
305304

306305
// Conforms to the http.Handler interface.
307306
func (l *LARS) serveHTTP(w http.ResponseWriter, r *http.Request) {
307+
308308
c := l.pool.Get().(*Ctx)
309309

310310
c.parent.RequestStart(w, r)

node.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,6 @@ walk: // Outer loop for walking the tree
377377
if n.handler != nil {
378378
handler = n.handler.chain
379379
handlerName = n.handler.handlerName
380-
}
381-
382-
if handler != nil {
383380
return
384381
} else if len(n.children) == 1 {
385382
// No handle found. Check if a handle for this path

0 commit comments

Comments
 (0)