Skip to content
This repository was archived by the owner on Sep 21, 2022. It is now read-only.

Commit 4d0e8b5

Browse files
nicola-spbgernest
authored andcommitted
Little refactoring (#85)
* refactoring code Rework registering of methods if any and peoper lookup of supported templates in SimpleView
1 parent d017ba3 commit 4d0e8b5

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

router/routes.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -320,23 +320,18 @@ func (r *Router) add(activeRoute *route, ctrlfn func() controller.Controller, mi
320320
}
321321
}
322322
}
323-
// register methods if any
324-
if len(activeRoute.methods) > 0 {
325-
r.HandleFunc(activeRoute.pattern, func(w http.ResponseWriter, req *http.Request) {
326-
ctx := base.NewContext(w, req)
327-
r.prepareContext(ctx)
328-
chain := chainMiddleware(ctx, m...)
329-
chain.ThenFunc(r.wrapController(ctx, activeRoute.fn, ctrlfn())).ServeHTTP(w, req)
330-
}).Methods(activeRoute.methods...)
331-
return nil
332-
}
333-
r.HandleFunc(activeRoute.pattern, func(w http.ResponseWriter, req *http.Request) {
323+
route := r.HandleFunc(activeRoute.pattern, func(w http.ResponseWriter, req *http.Request) {
334324
ctx := base.NewContext(w, req)
335325
r.prepareContext(ctx)
336326
chain := chainMiddleware(ctx, m...)
337327
chain.ThenFunc(r.wrapController(ctx, activeRoute.fn, ctrlfn())).ServeHTTP(w, req)
338328
})
339329

330+
// register methods if any
331+
if len(activeRoute.methods) > 0 {
332+
route.Methods(activeRoute.methods...)
333+
334+
}
340335
return nil
341336
}
342337

view/view.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func NewSimpleView(viewDir string) (View, error) {
4545
func (s *SimpleView) load(dir string) (View, error) {
4646

4747
// supported is the list of file extensions that will be parsed as templates
48-
supported := []string{".tpl", ".html", ".tmpl"}
48+
supported := map[string]bool{".tpl": true, ".html": true, ".tmpl": true}
4949

5050
werr := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
5151
if err != nil {
@@ -56,14 +56,7 @@ func (s *SimpleView) load(dir string) (View, error) {
5656
}
5757

5858
extension := filepath.Ext(path)
59-
found := false
60-
for _, ext := range supported {
61-
if ext == extension {
62-
found = true
63-
break
64-
}
65-
}
66-
if !found {
59+
if _, ok := supported[extension]; !ok {
6760
return nil
6861
}
6962

@@ -87,8 +80,8 @@ func (s *SimpleView) load(dir string) (View, error) {
8780
name = strings.TrimSuffix(name, extension) // remove extension
8881

8982
t := s.tmpl.New(name)
90-
_, err = t.Parse(string(data))
91-
if err != nil {
83+
84+
if _, err = t.Parse(string(data)); err != nil {
9285
return err
9386
}
9487
return nil

0 commit comments

Comments
 (0)