This Git documentation provides an overview of the "httpRouting" package, which is designed to facilitate HTTP routing in Go applications. The package contains three main files: "router.go," "route.go," and "middleware.go."
- Defines the
router
struct, which manages the routing logic and CORS headers. - Provides methods to configure CORS headers:
SetAllowOrigin
,SetAllowMethods
,SetAllowHeaders
, andSetCredantials
. - Implements route creation with
NewRoute
and request handling withServe
andServeWithCORS
functions. - Provides utility functions to retrieve URL parameters from requests.
- Defines the
route
struct, which represents a single route. - Contains fields for the regular expression, route handler, and middleware functions.
- Defines the
Middleware
function type used for HTTP middleware. - Contains an example of a middleware function called
HasAccess
.
The provided "main.go" file demonstrates the usage of the "httpRouting" package to create a simple HTTP server with a single route and a middleware function.
func main() {
r := httpRouting.NewRouterBuilder().
SetAllowOrigin("http://localhost:3000").
SetAllowMethods([]string{"POST", "GET", "OPTIONS", "PUT", "DELETE"}).
SetAllowHeaders([]string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token"}).
SetCredantials(true)
r.NewRoute("get", `/home/(?P<id>\d+)`, <Handler>, ...<Middlewares>)
http.HandleFunc("/", r.ServeWithCORS())
log.Fatal(http.ListenAndServe(":8080", nil))
}
To use the "httpRouting" package in your Go project, follow these steps:
- Ensure you have Go installed on your machine.
- Run the following command to add the package to your project:
go get github.com/Zewasik/go-router@v0.2.0
After installing the package, you can use it in your Go code by importing it as follows:
import httpRouting "github.com/Zewasik/go-router"
To create a new router instance and configure CORS headers, use the NewRouterBuilder
function along with the provided methods:
r := httpRouting.NewRouterBuilder().
SetAllowOrigin("http://localhost:3000").
SetAllowMethods([]string{"POST", "GET", "OPTIONS", "PUT", "DELETE"}).
SetAllowHeaders([]string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token"}).
SetCredantials(true)
To add a new route to the router, use the NewRoute
method. It requires the HTTP method, a regular expression, the handler function, and optional middleware functions:
r.NewRoute("get", `/home/(?P<id>\d+)`, <HandleFunc>, ...<Middleware>)
Middleware functions can be used to perform pre-processing or post-processing tasks for requests. The example includes a middleware function called HasAccess
:
func MiddlewareExample() httpRouting.Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// your code here
next.ServeHTTP(w, r)
})
}
}
To start the HTTP server, use the http.ListenAndServe
function:
log.Fatal(http.ListenAndServe(":8080", nil))
The "httpRouting" package simplifies HTTP routing in Go applications by providing a flexible and easy-to-use router. Its modular design allows for easy integration of middleware functions to handle various request processing tasks.
This documentation provides an overview of the package's main components and usage examples. For more detailed information, you can refer to the comments and code in the provided files.