Skip to content

Releases: icbd/go_restful_routes

v0.1.4

06 Oct 04:31
9430e12
Compare
Choose a tag to compare

Run Example

$ PUBLIC_DIR=example/public go run example/main.go

v0.1.3

04 Oct 15:08
Compare
Choose a tag to compare
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

04 Oct 09:35
b43911d
Compare
Choose a tag to compare
Merge pull request #1 from icbd/feat/static_server

feat: add Static routes

v0.1.1

03 Oct 04:18
Compare
Choose a tag to compare

You can also use HTTP method verb to register routes directly.

r.Any("/", controllers.RootController)
r.Get("/hi", controllers.HiController)

v0.1.0

02 Oct 15:57
5199735
Compare
Choose a tag to compare

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:

  1. fast matching, exact string matching. ex, /users can be matched by /users;
  2. prefix matching. ex, /users/, /users/123, /users/123/info, /users/123/info/ can matched by /users/;
  3. params matching. ex, /users/123 can matched by /users/{int:uid}, /users/bob can matched by /users/{:name};
  4. regex matching. ex, /users[123] can matched by {^/users\[[0-9]+\]$};
  5. 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
}