Releases: icbd/go_restful_routes
Releases · icbd/go_restful_routes
v0.1.4
v0.1.3
func RootController(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path != "/" {
static.New(writer, request, "public/", "/static/", "ico", "html")
return
}
_, _ = writer.Write([]byte("<h1>Welcome~</h1>"))
}
-
Using
Dir
to specify static resource directory; -
Using
Prefix
to replace routes prefix, ex, if request URL is/static/hello.png
, will Server the file in{Dir}/hello.png
; -
Using
Suffix
to specify static file types. If leave it blank, the default value is used:[ico, jpg, jpeg, png, gif, webp, html, js, css, md]
v0.1.2
v0.1.1
v0.1.0
Initialize a new routing table using NewRoutingTable
.
Registering routing entries by Register
.
The first parameter of Register
is the matching path, it has 5 types, and will be parsed in order:
- fast matching, exact string matching. ex,
/users
can be matched by/users
; - prefix matching. ex,
/users/
,/users/123
,/users/123/info
,/users/123/info/
can matched by/users/
; - params matching. ex,
/users/123
can matched by/users/{int:uid}
,/users/bob
can matched by/users/{:name}
; - regex matching. ex,
/users[123]
can matched by{^/users\[[0-9]+\]$}
; - root matching. Only match
/
.
The twice parameter of Register
is the handler function.
The third parameter of Register
is the slice of http methods. Empty slice represent any method allowed.
func Handler() http.Handler {
r := go_restful_routes.NewRoutingTable()
_, _ = r.Register("/", controllers.RootController, []string{})
_, _ = r.Register("/hi", controllers.HiController, []string{http.MethodGet, http.MethodPost})
_, _ = r.Register("/users/{int:uid}", controllers.ShowUser, []string{http.MethodGet})
return r
}