rewrite

package
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 3 Imported by: 0

README

Rewrite Middleware

What it does

Rewrites request paths or hosts according to configured rules. It is useful for migrations, vanity URLs, and gateway routing.

How to implement

package main

import (
	"github.com/oarkflow/fh"
	"github.com/oarkflow/fh/mw/rewrite"
)

func main() {
	app := fh.New()
	app.Use(rewrite.New(rewrite.Rule{From: "/old", To: "/new"}))

	app.Get("/new", func(c fh.Ctx) error { return c.Status(fh.StatusOK).SendString("new") })
}

Impact

Low overhead per rule. Poorly designed rules can make routing confusing.

Ordering guidance

Run before router-dependent middleware/handlers if the rewritten path should drive route matching.

Production considerations

Keep rewrite rules explicit and tested. Avoid open redirect patterns and log rewrites during migrations.

Documentation

Overview

Package rewrite performs bounded internal request rewrites.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(rules ...Rule) fh.HandlerFunc

func WithConfig

func WithConfig(cfg Config) fh.HandlerFunc

Types

type Config

type Config struct {
	Rules         []Rule
	Next          func(fh.Ctx) bool
	PreserveQuery bool
}

type Rule

type Rule struct {
	From string
	To   string

	Methods []string
	Host    string            // exact host or *.example.com; port is optional
	Headers map[string]string // "*" means the header only needs to exist
	Query   map[string]string // "*" means the query key only needs to exist
	When    func(fh.Ctx) bool
}

Rule matches a request and internally reroutes it to To. From uses exactly the router's syntax and matcher: static paths, :named parameters, and terminal * or *named wildcards. Targets may reference captures as :name, {name}, *name, {name}, or * for an unnamed wildcard.

Jump to

Keyboard shortcuts

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