Routing
[[toc]]
Introduction
Goravel routing module can operated by facades.Route()
.
Default Routing File
All routing files are defined in the /routes
directory. The framework defaults to a sample route /routes/web.go
, in which the func Web()
method is registered in the app/providers/route_service_provider.go
file to achieve routing binding.
You can add routing files under the routes
directory to perform more fine-grained management, then register them in the app/providers/route_service_provider.go
file.
Start HTTP Server
Start the HTTP server in main.go
in the root directory. facades.Route().Run()
will automatically fetch the route.host
configuration.
Start HTTPS Server
Register Middleware
Framework has a general middleware built in, you can also customize it according to your own needs.
Start Server
facades.Route().RunTLS()
will automatically fetch the route.tls
configuration:
You can also use facades.Route().RunTLSWithCert()
method to customize host and certificate.
Routing Methods
Run
RunTLS
RunTLSWithCert
Group
Prefix
ServeHTTP
Get
Post
Put
Delete
Patch
Options
Any
Resource
Static
StaticFile
StaticFS
Middleware
Basic Routing
Resource Routing
Group Routing
Routing Prefix
File Routing
Routing Parameters
Middleware
Fallback Routes
Using the Fallback
method, you may define a route that will be executed when no other route matches the incoming request.
Rate Limiting
Defining Rate Limiters
Goravel includes powerful and customizable rate limiting services that you may utilize to restrict the amount of traffic for a given route or group of routes. To get started, you should define rate limiter configurations that meet your application's needs. Typically, this should be done within the configureRateLimiting
method of your application's app/providers/route_service_provider.go
class.
Rate limiters are defined using the facades.RateLimiter()
s For
method. The For
method accepts a rate limiter name and a closure that returns the limit configuration that should apply to routes that are assigned to the rate limiter. The rate limiter name may be any string you wish:
If the incoming request exceeds the specified rate limit, a response with a 429 HTTP status code will automatically be returned by Goravel. If you would like to define your own response that should be returned by a rate limit, you may use the response method:
Since rate limiter callbacks receive the incoming HTTP request instance, you may build the appropriate rate limit dynamically based on the incoming request or authenticated user:
Segmenting Rate Limits
Sometimes you may wish to segment rate limits by some arbitrary value. For example, you may wish to allow users to access a given route 100 times per minute per IP address. To accomplish this, you may use the By
method when building your rate limit:
To illustrate this feature using another example, we can limit access to the route to 100 times per minute per authenticated user ID or 10 times per minute per IP address for guests:
Multiple Rate Limits
If needed, you may return an array of rate limits for a given rate limiter configuration. Each rate limit will be evaluated for the route based on the order they are placed within the array:
Attaching Rate Limiters To Routes
Rate limiters may be attached to routes or route groups using the throttle middleware. The throttle middleware accepts the name of the rate limiter you wish to assign to the route:
Cross-Origin Resource Sharing (CORS)
Goravel has CORS enabled by default, the configuration can be modified in config/cors.go
, the funciton is registered in app/http/kernel.go
as global middleware.
Testing Routing
Last updated