You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+107-3Lines changed: 107 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,9 +23,6 @@ Key & Unique Features
23
23
* When Parsing a form call Context's ParseForm amd ParseMulipartForm functions and the URL params will be added into the Form object, just like query parameters are, so no extra work
24
24
-[x] lars uses a custom version of [httprouter](https://github.com/julienschmidt/httprouter) so incredibly fast and efficient.
25
25
26
-
27
-
**Note:** Since this router has only explicit matches, you can not register static routes and parameters for the same path segment. For example you can not register the patterns /user/new and /user/:user for the same request method at the same time. The routing of different request methods is independent from each other. I was initially against this, and this router allowed it in a previous version, however it nearly cost me in a big app where the dynamic param value say :type actually could have matched another static route and that's just too dangerous, so it is no longer allowed.
l.Get("/static/*", http.FileServer(http.Dir("static/"))) // serve css, js etc.. c.Param(lars.WildcardParam) will return the remaining path if you need to use it in a custom handler...
71
+
72
+
...
73
+
```
74
+
75
+
**Note:** Since this router has only explicit matches, you can not register static routes and parameters for the same path segment. For example you can not register the patterns /user/new and /user/:user for the same request method at the same time. The routing of different request methods is independent from each other. I was initially against this, and this router allowed it in a previous version, however it nearly cost me in a big app where the dynamic param value say :type actually could have matched another static route and that's just too dangerous, so it is no longer allowed.
76
+
77
+
Groups
78
+
-----
79
+
```go
80
+
81
+
l.Use(LoggingAndRecovery)
82
+
...
83
+
l.Post("/users/add", ...)
84
+
85
+
// creates a group for user + inherits all middleware registered using l.Use()
86
+
user:= l.Group("/user/:userid")
87
+
user.Get("", ...)
88
+
user.Post("", ...)
89
+
user.Delete("/delete", ...)
90
+
91
+
contactInfo:= user.Group("/contact-info/:ciid")
92
+
contactinfo.Delete("/delete", ...)
93
+
94
+
// creates a group for others + inherits all middleware registered using l.Use() + adds OtherHandler to middleware
95
+
others:= l.Group("/others", OtherHandler)
96
+
97
+
// creates a group for admin WITH NO MIDDLEWARE... more can be added using admin.Use()
98
+
admin:= l.Group("/admin",nil)
99
+
admin.Use(SomeAdminSecurityMiddleware)
100
+
...
101
+
```
102
+
103
+
Custom Context + Avoid Type Casting / Custom Handlers
104
+
------
105
+
```go
106
+
...
107
+
// MyContext is a custom context
108
+
typeMyContextstruct {
109
+
*lars.Ctx// a little dash of Duck Typing....
110
+
}
111
+
112
+
// RequestStart overriding
113
+
func(mc *MyContext) RequestStart(whttp.ResponseWriter, r *http.Request) {
114
+
mc.Ctx.RequestStart(w, r) // MUST be called!
115
+
116
+
// do whatever you need to on request start, db connections, variable init...
117
+
}
118
+
119
+
// RequestEnd overriding
120
+
func(mc *MyContext) RequestEnd() {
121
+
122
+
// do whatever you need on request finish, reset variables, db connections...
123
+
124
+
mc.Ctx.RequestEnd() // MUST be called!
125
+
}
126
+
127
+
// CustomContextFunction is a function that is specific to your applications needs that you added
128
+
func(mc *MyContext) CustomContextFunction() {
129
+
// do something
130
+
}
131
+
132
+
// newContext is the function that creates your custom context +
133
+
// contains lars's default context
134
+
funcnewContext(l *lars.LARS) lars.Context {
135
+
return &MyContext{
136
+
Ctx: lars.NewContext(l),
137
+
}
138
+
}
139
+
140
+
// casts custom context and calls you custom handler so you don;t have to type cast lars.Context everywhere
0 commit comments