mirror of
https://github.com/labstack/echo.git
synced 2026-05-16 09:48:24 +02:00
03d9298e7d
Modernizes the codebase using the Go 1.26 gofix tool to adopt newer idioms and library features while maintaining compatibility with the current toolchain.
80 lines
2.3 KiB
Go
80 lines
2.3 KiB
Go
// SPDX-License-Identifier: MIT
|
|
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
|
|
|
|
package middleware
|
|
|
|
import (
|
|
"errors"
|
|
"maps"
|
|
"regexp"
|
|
|
|
"github.com/labstack/echo/v5"
|
|
)
|
|
|
|
// RewriteConfig defines the config for Rewrite middleware.
|
|
type RewriteConfig struct {
|
|
// Skipper defines a function to skip middleware.
|
|
Skipper Skipper
|
|
|
|
// Rules defines the URL path rewrite rules. The values captured in asterisk can be
|
|
// retrieved by index e.g. $1, $2 and so on.
|
|
// Example:
|
|
// "/old": "/new",
|
|
// "/api/*": "/$1",
|
|
// "/js/*": "/public/javascripts/$1",
|
|
// "/users/*/orders/*": "/user/$1/order/$2",
|
|
// Required.
|
|
Rules map[string]string
|
|
|
|
// RegexRules defines the URL path rewrite rules using regexp.Rexexp with captures
|
|
// Every capture group in the values can be retrieved by index e.g. $1, $2 and so on.
|
|
// Example:
|
|
// "^/old/[0.9]+/": "/new",
|
|
// "^/api/.+?/(.*)": "/v2/$1",
|
|
RegexRules map[*regexp.Regexp]string
|
|
}
|
|
|
|
// Rewrite returns a Rewrite middleware.
|
|
//
|
|
// Rewrite middleware rewrites the URL path based on the provided rules.
|
|
func Rewrite(rules map[string]string) echo.MiddlewareFunc {
|
|
c := RewriteConfig{}
|
|
c.Rules = rules
|
|
return RewriteWithConfig(c)
|
|
}
|
|
|
|
// RewriteWithConfig returns a Rewrite middleware or panics on invalid configuration.
|
|
//
|
|
// Rewrite middleware rewrites the URL path based on the provided rules.
|
|
func RewriteWithConfig(config RewriteConfig) echo.MiddlewareFunc {
|
|
return toMiddlewareOrPanic(config)
|
|
}
|
|
|
|
// ToMiddleware converts RewriteConfig to middleware or returns an error for invalid configuration
|
|
func (config RewriteConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
|
|
if config.Skipper == nil {
|
|
config.Skipper = DefaultSkipper
|
|
}
|
|
if config.Rules == nil && config.RegexRules == nil {
|
|
return nil, errors.New("echo rewrite middleware requires url path rewrite rules or regex rules")
|
|
}
|
|
|
|
if config.RegexRules == nil {
|
|
config.RegexRules = make(map[*regexp.Regexp]string)
|
|
}
|
|
maps.Copy(config.RegexRules, rewriteRulesRegex(config.Rules))
|
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
return func(c *echo.Context) (err error) {
|
|
if config.Skipper(c) {
|
|
return next(c)
|
|
}
|
|
|
|
if err := rewriteURL(config.RegexRules, c.Request()); err != nil {
|
|
return err
|
|
}
|
|
return next(c)
|
|
}
|
|
}, nil
|
|
}
|