Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Frp | 68,101 | 9 | 5 days ago | 78 | July 10, 2022 | 97 | apache-2.0 | Go | ||
A fast reverse proxy to help you expose a local server behind a NAT or firewall to the internet. | ||||||||||
Caddy | 47,606 | 118 | 334 | 4 days ago | 85 | October 26, 2020 | 80 | apache-2.0 | Go | |
Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS | ||||||||||
Vapor | 22,858 | 9 days ago | November 16, 2021 | 113 | mit | Swift | ||||
💧 A server-side Swift HTTP web framework. | ||||||||||
Postgrest | 20,635 | 4 | a day ago | 37 | July 12, 2022 | 207 | mit | Haskell | ||
REST API for any Postgres database | ||||||||||
Aiohttp | 13,602 | 7,355 | 4,894 | 5 days ago | 220 | November 14, 2021 | 497 | other | Python | |
Asynchronous HTTP client/server framework for asyncio and Python | ||||||||||
Node Http Proxy | 13,363 | 398,065 | 2,932 | 5 days ago | 103 | May 17, 2020 | 579 | other | JavaScript | |
A full-featured http proxy for node.js | ||||||||||
Http Server | 12,688 | 46,303 | 7,932 | 2 months ago | 49 | May 31, 2022 | 135 | mit | JavaScript | |
a simple zero-configuration command-line http server | ||||||||||
Chisel | 9,197 | 2 | 21 | 2 days ago | 27 | February 03, 2022 | 186 | mit | Go | |
A fast TCP/UDP tunnel over HTTP | ||||||||||
Warp | 8,145 | 73 | 355 | 4 days ago | 34 | November 09, 2021 | 216 | mit | Rust | |
A super-easy, composable, web server framework for warp speeds. | ||||||||||
Proxygen | 7,829 | a day ago | 40 | other | C++ | |||||
A collection of C++ HTTP libraries including an easy to use HTTP server. |
Package router provides a lightning fast HTTP router.
Get package:
go get -u github.com/gowww/router
Import it in your code:
import "github.com/gowww/router"
Make a new router:
rt := router.New()
Make a route:
rt.Handle("GET", "/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello")
}))
Remember that HTTP methods are case-sensitive and uppercase by convention (RFC 7231 4.1).
So you can directly use the built-in shortcuts for standard HTTP methods: Router.Get, Router.Post, Router.Put, Router.Patch and Router.Delete.
Give the router to the server:
http.ListenAndServe(":8080", rt)
A named parameter begins with :
and matches any value until the next /
or end of path.
To retrieve the value (stored in request's context), ask Parameter.
It will return the value as a string.
Example, with a parameter id
:
rt.Get("/users/:id", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := router.Parameter(r, "id")
fmt.Fprintf(w, "Page of user #%s", id)
}))
A parameter can be used on the same level as a static route, without conflict:
rt.Get("/users/all", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "All users page")
}))
rt.Get("/users/:id", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := router.Parameter(r, "id")
fmt.Fprintf(w, "Page of user #%s", id)
}))
If a parameter must match an exact pattern (digits only, for example), you can also set a regular expression constraint just after the parameter name and another :
:
rt.Get(`/users/🆔^\d+$`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := router.Parameter(r, "id")
fmt.Fprintf(w, "Page of user #%s", id)
}))
If you don't need to retrieve the parameter value but only use a regular expression, you can omit the parameter name:
rt.Get(`/shows/::^prison-break-s06-.+`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Prison Break S06 Coming soon")
}))
Don't forget that regular expressions can significantly reduce performance.
A parameter with a regular expression can be used on the same level as a simple parameter, without conflict:
rt.Get(`/users/🆔^\d+$`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := router.Parameter(r, "id")
fmt.Fprintf(w, "Page of user #%s", id)
}))
rt.Get("/users/:name", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
name := router.Parameter(r, "name")
fmt.Fprintf(w, "Page of %s", name)
}))
A trailing slash in a route path is significant.
It behaves like a wildcard by matching the beginning of the request path.
The rest of the request path becomes the parameter value of *
:
rt.Get("/files/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
filepath := router.Parameter(r, "*")
fmt.Fprintf(w, "Get file %s", filepath)
}))
Note that a trailing slash in a request path is always trimmed and the client redirected.
For example, a request for /files/
will be redirected to /files
and will never match a /files/
route.
In other words, /files
and /files/
are two different routes.
Deeper route paths with the same prefix as the wildcard will take precedence, without conflict:
// Will match:
// /files/one
// /files/two
// ...
rt.Get("/files/:name", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {kv
name := router.Parameter(r, "name")
fmt.Fprintf(w, "Get root file #%s", name)
}))
// Will match:
// /files/one/...
// /files/two/...
// ...
rt.Get("/files/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
filepath := router.Parameter(r, "*")
fmt.Fprintf(w, "Get file %s", filepath)
}))
// Will match:
// /files/movies/one
// /files/movies/two
// ...
rt.Get("/files/movies/:name", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
name := router.Parameter(r, "name")
fmt.Fprintf(w, "Get movie #%s", name)
}))
For serving static files, like for other routes, just bring your own handler.
Example, with the standard net/http.FileServer:
rt.Get("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
When a request match no route, the response status is set to 404 and an empty body is sent by default.
But you can set your own "not found" handler.
In this case, it's up to you to set the response status code (normally 404):
rt.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
})