virsas-mod-router
We reuse chi router in many projects. So the router creation is extracted to its own module.
Variables
- allowedOrigins ([]string) - list of allowed origins - []string{"*"}
- allowedMethods ([]string) - list of allowed methogs - []string{"GET", "POST", "PUT", "DELETE"}
- allowedHeaders ([]string) - list of allowed headers - []string{"Accept", "Authorization", "APIKey", "Content-Type", "X-CSRF-Token"}
- exposedHeaders ([]string) - list of safe headers to expose - []string{"link"}
- allowedCredentials (bool) - if request can include user credentials
- maxAge (int) - how long (in seconds) the results of a preflight request can be cached
- enableLogging (bool) - enable middleware logger. In case you dont want to log health, disable it here and enable it in routes
Environmental variables
- VSS_ROUTER_HEALTH - Enable / Disable /health for system check. Default true.
- VSS_ROUTER_RATE_PER_MINUTE - COnfigure a number of concurrent connections from single IP per minute. Default 100 connections.
- VSS_ROUTER_LOGGING - Enable Logging. In case you dont want to log health, disable it here and enable it in routes. Default true.
- VSS_ROUTER_TIMEOUT - Timeout that cancels ctx after a given timeout and return a 504 Gateway Timeout error to the client. Default value 60 seconds.
Example
package main
import (
"app/router"
...
vssrouter "github.com/virsas/virsas-mod-router"
...
)
func main () {
allowedOrigins := []string{"*"},
allowedMethods := []string{"GET", "POST", "PUT", "DELETE"},
allowedHeaders := []string{"Accept", "Authorization", "APIKey", "Content-Type", "X-CSRF-Token"},
exposedHeaders := []string{"Link"}
allowedCredentials := true
maxAge := 300
r, err := vssrouter.CreateRouter(allowedOrigins, allowedMethods, allowedHeaders, exposedHeaders, allowedCredentials, maxAge)
if err != nil {
log.Fatal(err)
}
router.AddRoutes(r)
...
log.Fatal(http.ListenAndServe(":3000", r))
}