rewriter

package module
v0.0.0-...-c96df02 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2024 License: MIT Imports: 4 Imported by: 0

README

Rewriter

Rewriter is a lightweight middleware for rewriting request URL paths and returning a status code of http.StatusTemporaryRedirect. It can be seamlessly integrated with gin and orbit frameworks. This middleware is designed for redirect requests that require URL rewriting.

Why use http.StatusTemporaryRedirect instead of http.StatusMovedPermanently? By using http.StatusTemporaryRedirect, the response will not be cached by the browser, and subsequent requests will not be sent to the original URL again.

Rewriter is built on top of powerful Golang native packages and other libraries:

Installation

go get github.com/shengyanli1982/orbit-contrib/pkg/rewriter

Quick Start

Configuration

The Rewriter middleware provides a configuration object that allows you to customize its behavior. The configuration object offers the following methods for customization:

  • WithCallback: Sets the callback function. The default is &emptyCallback{}.
  • WithPathRewriteFunc: Sets the path rewrite function. The default is DefaultPathRewriteFunc.
  • WithMatchFunc: Sets the match function. The default is DefaultLimitMatchFunc.
  • WithIpWhitelist: Sets the IP whitelist. The default is DefaultIpWhitelist.
Methods
  • HandlerFunc: Returns a gin.HandlerFunc for orbit or gin.
  • Stop: Stops the rewriter. This is an empty function and does not need to be called.
Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"
	"time"

	"github.com/gin-gonic/gin"
	rw "github.com/shengyanli1982/orbit-contrib/pkg/rewriter"
)

var (
	// 测试URL
	// Test URL
	testUrl = "/test"
)

// testRewriteFunc 是一个测试重写函数
// testRewriteFunc is a test rewrite function
func testRewriteFunc(u *url.URL) (bool, string) {
	// 如果URL的路径等于测试URL
	// If the path of the URL equals the test URL
	if u.Path == testUrl {
		// 返回真和新的URL
		// Return true and the new URL
		return true, testUrl + "2"
	}
	// 否则返回假和空字符串
	// Otherwise return false and an empty string
	return false, ""
}

// testRequestFunc 是一个测试请求函数
// testRequestFunc is a test request function
func testRequestFunc(idx int, router *gin.Engine, conf *rw.Config, url string) {
	// 创建一个新的请求
	// Create a new request
	req := httptest.NewRequest(http.MethodGet, url, nil)

	// 创建一个新的响应记录器
	// Create a new response recorder
	resp := httptest.NewRecorder()

	// 使用路由器处理HTTP请求
	// Use the router to handle the HTTP request
	router.ServeHTTP(resp, req)

	// 打印请求的信息
	// Print the information of the request
	fmt.Println("[Request]", idx, resp.Code, url, req.URL.Path, resp.Body.String())
}

func main() {
	// 创建一个新的配置
	// Create a new Config
	conf := rw.NewConfig().WithPathRewriteFunc(testRewriteFunc)

	// 创建一个新的路径重写器
	// Create a new Path Rewriter
	compr := rw.NewPathRewriter(conf)

	// 在函数返回时停止路径重写器
	// Stop the Path Rewriter when the function returns
	defer compr.Stop()

	// 创建一个新的Gin路由器
	// Create a new Gin router
	router := gin.New()

	// 使用路径重写器的处理函数
	// Use the handler function of the Path Rewriter
	router.Use(compr.HandlerFunc())

	// 添加一个新的路由
	// Add a new route
	router.GET(testUrl, func(c *gin.Context) {
		// 当请求成功时,返回"OK"
		// Return "OK" when the request is successful
		c.String(http.StatusOK, "OK")
	})

	// 测试 10 次请求
	// Test 10 requests
	for i := 0; i < 10; i++ {
		// 调用测试请求函数,传入索引、路由器、配置和测试URL
		// Call the test request function, passing in the index, router, configuration, and test URL
		testRequestFunc(i, router, conf, testUrl)
	}

	// 等待所有请求任务执行完毕
	// Wait for all request tasks to complete
	time.Sleep(time.Second)
}

Result

$ go run demo.go
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /test                     --> main.main.func1 (2 handlers)
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 307 with 200
[Request] 0 307 /test /test2 <a href="/test2">Temporary Redirect</a>.

OK
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 307 with 200
[Request] 1 307 /test /test2 <a href="/test2">Temporary Redirect</a>.

OK
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 307 with 200
[Request] 2 307 /test /test2 <a href="/test2">Temporary Redirect</a>.

OK
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 307 with 200
[Request] 3 307 /test /test2 <a href="/test2">Temporary Redirect</a>.

OK
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 307 with 200
[Request] 4 307 /test /test2 <a href="/test2">Temporary Redirect</a>.

OK
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 307 with 200
[Request] 5 307 /test /test2 <a href="/test2">Temporary Redirect</a>.

OK
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 307 with 200
[Request] 6 307 /test /test2 <a href="/test2">Temporary Redirect</a>.

OK
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 307 with 200
[Request] 7 307 /test /test2 <a href="/test2">Temporary Redirect</a>.

OK
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 307 with 200
[Request] 8 307 /test /test2 <a href="/test2">Temporary Redirect</a>.

OK
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 307 with 200
[Request] 9 307 /test /test2 <a href="/test2">Temporary Redirect</a>.

OK

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultPathRewriteFunc = func(header *url.URL) (bool, string) {

	return false, ""
}

DefaultPathRewriteFunc 是一个默认的路径重写函数 DefaultPathRewriteFunc is a default function to rewrite the path

Functions

This section is empty.

Types

type Callback

type Callback interface {
	// OnPathRewrited 是一个路径重写回调函数,当路径被重写后,会调用这个函数
	// OnPathRewrited is a callback function for path rewriting. When the path is rewritten, this function will be called
	OnPathRewrited(new, old string)
}

Callback 是一个回调接口,用于定义路径重写后的回调函数 Callback is a callback interface, used to define the callback function after path rewriting

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config 是一个配置结构体 Config is a struct of config

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 创建一个默认的配置实例 DefaultConfig creates a default config instance

func NewConfig

func NewConfig() *Config

NewConfig 创建一个新的配置实例 NewConfig creates a new config instance

func (*Config) WithCallback

func (c *Config) WithCallback(callback Callback) *Config

WithCallback 设置回调函数 WithCallback sets the callback function

func (*Config) WithIpWhitelist

func (c *Config) WithIpWhitelist(whitelist []string) *Config

WithIpWhitelist 设置白名单 WithIpWhitelist sets the whitelist

func (*Config) WithMatchFunc

func (c *Config) WithMatchFunc(fn com.HttpRequestHeaderMatchFunc) *Config

WithMatchFunc 设置匹配函数 WithMatchFunc sets the match function

func (*Config) WithPathRewriteFunc

func (c *Config) WithPathRewriteFunc(fn PathRewriteFunc) *Config

WithPathRewriteFunc 设置路径重写函数 WithPathRewriteFunc sets the path rewrite function

type PathRewriteFunc

type PathRewriteFunc func(header *url.URL) (bool, string)

PathRewriteFunc 是一个路径重写函数 PathRewriteFunc is a function to rewrite the path

type PathRewriter

type PathRewriter struct {
	// contains filtered or unexported fields
}

PathRewriter 结构体用于实现路径重写功能 PathRewriter is a struct for implementing path rewriting

func NewPathRewriter

func NewPathRewriter(config *Config) *PathRewriter

NewPathRewriter 创建一个新的 PathRewriter 实例 NewPathRewriter creates a new PathRewriter instance

func (*PathRewriter) HandlerFunc

func (p *PathRewriter) HandlerFunc() gin.HandlerFunc

HandlerFunc 返回一个 gin.HandlerFunc,用于处理请求 HandlerFunc returns a gin.HandlerFunc for processing requests

func (*PathRewriter) Stop

func (p *PathRewriter) Stop()

Stop 停止压缩器 Stop stops the compressor

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL