Skip to content

Commit 5c33cff

Browse files
Dean KarnDean Karn
authored andcommitted
Update README.md
1 parent 6964a94 commit 5c33cff

File tree

1 file changed

+107
-3
lines changed

1 file changed

+107
-3
lines changed

README.md

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ Key & Unique Features
2323
* 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
2424
- [x] lars uses a custom version of [httprouter](https://github.com/julienschmidt/httprouter) so incredibly fast and efficient.
2525

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.
28-
2926
Installation
3027
-----------
3128

@@ -64,6 +61,113 @@ func HelloWorld(c lars.Context) {
6461
}
6562
```
6663

64+
URL Params
65+
----------
66+
67+
```go
68+
l := l.New()
69+
l.Get("/user/:id", UserHandler)
70+
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+
type MyContext struct {
109+
*lars.Ctx // a little dash of Duck Typing....
110+
}
111+
112+
// RequestStart overriding
113+
func (mc *MyContext) RequestStart(w http.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+
func newContext(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
141+
func castCustomContext(c lars.Context, handler lars.Handler) {
142+
143+
// could do it in all one statement, but in long form for readability
144+
h := handler.(func(*MyContext))
145+
ctx := c.(*MyContext)
146+
147+
h(ctx)
148+
}
149+
150+
func main() {
151+
152+
l := lars.New()
153+
l.RegisterContext(newContext) // all gets cached in pools for you
154+
l.RegisterCustomHandler(func(*MyContext) {}, castCustomContext)
155+
l.Use(Logger)
156+
157+
l.Get("/", Home)
158+
159+
http.ListenAndServe(":3007", l.Serve())
160+
}
161+
162+
// Home ...notice the receiver is *MyContext, castCustomContext handled the type casting for us
163+
// quite the time saver if you ask me.
164+
func Home(c *MyContext) {
165+
166+
c.CustomContextFunction()
167+
...
168+
}
169+
```
170+
67171
Middleware
68172
-----------
69173
There are some pre-defined middlewares within the middleware folder; NOTE: that the middleware inside will

0 commit comments

Comments
 (0)