11/*
22Package lars - Library Access/Retrieval System, is a fast radix-tree based, zero allocation, HTTP router for Go.
33
4+
45Usage
56
67Below is a simple example, for a full example see here https://github.com/go-playground/lars/blob/master/examples/all-in-one/main.go
@@ -17,8 +18,8 @@ Below is a simple example, for a full example see here https://github.com/go-pla
1718 func main() {
1819
1920 l := lars.New()
20- l.Use(mw.LoggingAndRecovery) // LoggingAndRecovery is just an example copy paste and modify to your needs
21-
21+ l.Use(mw.LoggingAndRecovery) // LoggingAndRecovery is just an example copy
22+ // paste and modify to your needs
2223 l.Get("/", HelloWorld)
2324
2425 http.ListenAndServe(":3007", l.Serve())
@@ -34,19 +35,28 @@ Below is a simple example, for a full example see here https://github.com/go-pla
3435
3536URL Params
3637
38+ example param usage
39+
3740 l := l.New()
3841 l.Get("/user/:id", UserHandler)
39- l.Get("/static/*", http.FileServer(http.Dir("static/"))) // serve css, js etc.. c.Param(lars.WildcardParam) will return the
40- // remaining path if you need to use it in a custom handler...
4142
42- NOTE: Since this router has only explicit matches, you can not register static routes and parameters for the same path segment.
43- For example you can not register the patterns /user/new and /user/:user for the same request method at the same time.
44- The routing of different request methods is independent from each other. I was initially against this, and this router allowed
45- 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
46- another static route and that's just too dangerous, so it is no longer allowed.
43+ // serve css, js etc.. c.Param(lars.WildcardParam) will return the
44+ // remaining path if you need to use it in a custom handler...
45+ l.Get("/static/*", http.FileServer(http.Dir("static/")))
46+
47+ NOTE: Since this router has only explicit matches, you can not register static routes
48+ and parameters for the same path segment. For example you can not register the patterns
49+ /user/new and /user/:user for the same request method at the same time. The routing of
50+ different request methods is independent from each other. I was initially against this,
51+ and this router allowed it in a previous version, however it nearly cost me in a big
52+ app where the dynamic param value say :type actually could have matched another static
53+ route and that's just too dangerous, so it is no longer allowed.
54+
4755
4856Groups
4957
58+ example group definitions
59+
5060 ...
5161 l.Use(LoggingAndRecovery)
5262 ...
@@ -61,15 +71,20 @@ Groups
6171 contactInfo := user.Group("/contact-info/:ciid")
6272 contactinfo.Delete("/delete", ...)
6373
64- // creates a group for others + inherits all middleware registered using l.Use() + adds OtherHandler to middleware
74+ // creates a group for others + inherits all middleware registered using l.Use() +
75+ // adds OtherHandler to middleware
6576 others := l.Group("/others", OtherHandler)
6677
6778 // creates a group for admin WITH NO MIDDLEWARE... more can be added using admin.Use()
6879 admin := l.Group("/admin",nil)
6980 admin.Use(SomeAdminSecurityMiddleware)
7081 ...
7182
72- Custom Context + Avoid Type Casting / Custom Handlers
83+
84+ Custom Context - Avoid Type Casting - Custom Handlers
85+
86+
87+ example context + custom handlers
7388
7489 ...
7590 // MyContext is a custom context
@@ -92,7 +107,8 @@ Custom Context + Avoid Type Casting / Custom Handlers
92107 mc.Ctx.RequestEnd() // MUST be called!
93108 }
94109
95- // CustomContextFunction is a function that is specific to your applications needs that you added
110+ // CustomContextFunction is a function that is specific to your applications
111+ // needs that you added
96112 func (mc *MyContext) CustomContextFunction() {
97113 // do something
98114 }
@@ -105,7 +121,8 @@ Custom Context + Avoid Type Casting / Custom Handlers
105121 }
106122 }
107123
108- // casts custom context and calls you custom handler so you don;t have to type cast lars.Context everywhere
124+ // casts custom context and calls you custom handler so you don't have to
125+ // type cast lars.Context everywhere
109126 func castCustomContext(c lars.Context, handler lars.Handler) {
110127
111128 // could do it in all one statement, but in long form for readability
@@ -127,8 +144,8 @@ Custom Context + Avoid Type Casting / Custom Handlers
127144 http.ListenAndServe(":3007", l.Serve())
128145 }
129146
130- // Home ...notice the receiver is *MyContext, castCustomContext handled the type casting for us
131- // quite the time saver if you ask me.
147+ // Home ...notice the receiver is *MyContext, castCustomContext handled the
148+ // type casting for us; quite the time saver if you ask me.
132149 func Home(c *MyContext) {
133150
134151 c.CustomContextFunction()
@@ -137,9 +154,12 @@ Custom Context + Avoid Type Casting / Custom Handlers
137154
138155Misc
139156
157+ misc examples and noteworthy features
158+
140159 ...
141- // can register multiple handlers, the last is considered the last in the chain and others
142- // considered middleware, but just for this route and not added to middleware like l.Use() does.
160+ // can register multiple handlers, the last is considered the last in the chain and
161+ // others considered middleware, but just for this route and not added to middleware
162+ // like l.Use() does.
143163 l.Get(/"home", AdditionalHandler, HomeHandler)
144164
145165 // set custom 404 ( not Found ) handler
@@ -154,11 +174,21 @@ Misc
154174 // register custom context
155175 l.RegisterContext(ContextFunc)
156176
157- // Register custom handler type, see util.go https://github.com/go-playground/lars/blob/master/util.go#L62 for example handler creation
177+ // Register custom handler type, see util.go
178+ // https://github.com/go-playground/lars/blob/master/util.go#L62 for example handler
179+ // creation
158180 l.RegisterCustomHandler(interface{}, CustomHandlerFunc)
159181
160- // Context has 2 methods of which you should be aware of ParseForm and ParseMulipartForm, they just call the default http functions but
161- // provide one more additional feature, they copy the URL params to the request Forms variables, just like Query parameters would have been.
162- // The functions are for convenience and are totally optional.
182+ // NativeChainHandler is used as a helper to create your own custom handlers, or use
183+ // custom handlers that already exist an example usage can be found here
184+ // https://github.com/go-playground/lars/blob/master/util.go#L86, below is an example
185+ // using nosurf CSRF middleware
186+ l.Use(nosurf.NewPure(lars.NativeChainHandler))
187+
188+ // Context has 2 methods of which you should be aware of ParseForm and
189+ // ParseMulipartForm, they just call the default http functions but provide one more
190+ // additional feature, they copy the URL params to the request Forms variables, just
191+ // like Query parameters would have been. The functions are for convenience and are
192+ // totally optional.
163193*/
164194package lars
0 commit comments